Skip to content

Most visited

Recently visited

added in API level 1


public static interface View.OnKeyListener


Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.


Public methods

abstract boolean onKey(View v, int keyCode, KeyEvent event)

Called when a hardware key is dispatched to a view.

Public methods


added in API level 1
boolean onKey (View v, 
                int keyCode, 
                KeyEvent event)

Called when a hardware key is dispatched to a view. This allows listeners to get a chance to respond before the target view.

Key presses in software keyboards will generally NOT trigger this method, although some may elect to do so in some situations. Do not assume a software input method has to be key-based; even if it is, it may use key presses in a different way than you expect, so there is no way to reliably catch soft input key presses.

v View: The view the key has been dispatched to.

keyCode int: The code for the physical key that was pressed

event KeyEvent: The KeyEvent object containing full information about the event.

boolean True if the listener has consumed the event, false otherwise.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)