public class

KeyEvent

extends InputEvent
implements Parcelable
java.lang.Object
   ↳ android.view.InputEvent
     ↳ android.view.KeyEvent

Class Overview

Object used to report key and button events.

Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up. If the key press is canceled, the key up event will have the FLAG_CANCELED flag set.

Key events are generally accompanied by a key code (getKeyCode()), scan code (getScanCode()) and meta state (getMetaState()). Key code constants are defined in this class. Scan code constants are raw device-specific codes obtained from the OS and so are not generally meaningful to applications unless interpreted using the KeyCharacterMap. Meta states describe the pressed state of key modifiers such as META_SHIFT_ON or META_ALT_ON.

When interacting with an IME, the framework may deliver key events with the special action ACTION_MULTIPLE that either specifies that single repeated key code or a sequence of characters to insert.

In general, the framework cannot guarantee that the key events it delivers to a view always constitute complete key sequences since some events may be dropped or modified by containing views before they are delivered. The view implementation should be prepared to handle FLAG_CANCELED and should tolerate anomalous situations such as receiving a new ACTION_DOWN without first having received an ACTION_UP for the prior key press.

Refer to InputDevice for more information about how different kinds of input devices and sources represent keys and buttons.

Summary

Nested Classes
interface KeyEvent.Callback  
class KeyEvent.DispatcherState Use with dispatch(Callback, DispatcherState, Object) for more advanced key dispatching, such as long presses. 
Constants
int ACTION_DOWN getAction() value: the key has been pressed down.
int ACTION_MULTIPLE getAction() value: multiple duplicate key events have occurred in a row, or a complex string is being delivered.
int ACTION_UP getAction() value: the key has been released.
Creator<KeyEvent> CREATOR
int FLAG_CANCELED When associated with up key events, this indicates that the key press has been canceled.
int FLAG_CANCELED_LONG_PRESS Set when a key event has FLAG_CANCELED set because a long press action was executed while it was down.
int FLAG_EDITOR_ACTION This mask is used for compatibility, to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done".
int FLAG_FROM_SYSTEM This mask is set if an event was known to come from a trusted part of the system.
int FLAG_KEEP_TOUCH_MODE This mask is set if we don't want the key event to cause us to leave touch mode.
int FLAG_LONG_PRESS This flag is set for the first key repeat that occurs after the long press timeout.
int FLAG_SOFT_KEYBOARD This mask is set if the key event was generated by a software keyboard.
int FLAG_TRACKING Set for ACTION_UP when this event's key code is still being tracked from its initial down.
int FLAG_VIRTUAL_HARD_KEY This key event was generated by a virtual (on-screen) hard key area.
int FLAG_WOKE_HERE This mask is set if the device woke because of this key event.
int KEYCODE_0 Key code constant: '0' key.
int KEYCODE_1 Key code constant: '1' key.
int KEYCODE_2 Key code constant: '2' key.
int KEYCODE_3 Key code constant: '3' key.
int KEYCODE_4 Key code constant: '4' key.
int KEYCODE_5 Key code constant: '5' key.
int KEYCODE_6 Key code constant: '6' key.
int KEYCODE_7 Key code constant: '7' key.
int KEYCODE_8 Key code constant: '8' key.
int KEYCODE_9 Key code constant: '9' key.
int KEYCODE_A Key code constant: 'A' key.
int KEYCODE_ALT_LEFT Key code constant: Left Alt modifier key.
int KEYCODE_ALT_RIGHT Key code constant: Right Alt modifier key.
int KEYCODE_APOSTROPHE Key code constant: ''' (apostrophe) key.
int KEYCODE_AT Key code constant: '@' key.
int KEYCODE_B Key code constant: 'B' key.
int KEYCODE_BACK Key code constant: Back key.
int KEYCODE_BACKSLASH Key code constant: '\' key.
int KEYCODE_BUTTON_A Key code constant: A Button key.
int KEYCODE_BUTTON_B Key code constant: B Button key.
int KEYCODE_BUTTON_C Key code constant: C Button key.
int KEYCODE_BUTTON_L1 Key code constant: L1 Button key.
int KEYCODE_BUTTON_L2 Key code constant: L2 Button key.
int KEYCODE_BUTTON_MODE Key code constant: Mode Button key.
int KEYCODE_BUTTON_R1 Key code constant: R1 Button key.
int KEYCODE_BUTTON_R2 Key code constant: R2 Button key.
int KEYCODE_BUTTON_SELECT Key code constant: Select Button key.
int KEYCODE_BUTTON_START Key code constant: Start Button key.
int KEYCODE_BUTTON_THUMBL Key code constant: Left Thumb Button key.
int KEYCODE_BUTTON_THUMBR Key code constant: Right Thumb Button key.
int KEYCODE_BUTTON_X Key code constant: X Button key.
int KEYCODE_BUTTON_Y Key code constant: Y Button key.
int KEYCODE_BUTTON_Z Key code constant: Z Button key.
int KEYCODE_C Key code constant: 'C' key.
int KEYCODE_CALL Key code constant: Call key.
int KEYCODE_CAMERA Key code constant: Camera key.
int KEYCODE_CLEAR Key code constant: Clear key.
int KEYCODE_COMMA Key code constant: ',' key.
int KEYCODE_D Key code constant: 'D' key.
int KEYCODE_DEL Key code constant: Backspace key.
int KEYCODE_DPAD_CENTER Key code constant: Directional Pad Center key.
int KEYCODE_DPAD_DOWN Key code constant: Directional Pad Down key.
int KEYCODE_DPAD_LEFT Key code constant: Directional Pad Left key.
int KEYCODE_DPAD_RIGHT Key code constant: Directional Pad Right key.
int KEYCODE_DPAD_UP Key code constant: Directional Pad Up key.
int KEYCODE_E Key code constant: 'E' key.
int KEYCODE_ENDCALL Key code constant: End Call key.
int KEYCODE_ENTER Key code constant: Enter key.
int KEYCODE_ENVELOPE Key code constant: Envelope special function key.
int KEYCODE_EQUALS Key code constant: '=' key.
int KEYCODE_EXPLORER Key code constant: Explorer special function key.
int KEYCODE_F Key code constant: 'F' key.
int KEYCODE_FOCUS Key code constant: Camera Focus key.
int KEYCODE_G Key code constant: 'G' key.
int KEYCODE_GRAVE Key code constant: '`' (backtick) key.
int KEYCODE_H Key code constant: 'H' key.
int KEYCODE_HEADSETHOOK Key code constant: Headset Hook key.
int KEYCODE_HOME Key code constant: Home key.
int KEYCODE_I Key code constant: 'I' key.
int KEYCODE_J Key code constant: 'J' key.
int KEYCODE_K Key code constant: 'K' key.
int KEYCODE_L Key code constant: 'L' key.
int KEYCODE_LEFT_BRACKET Key code constant: '[' key.
int KEYCODE_M Key code constant: 'M' key.
int KEYCODE_MEDIA_FAST_FORWARD Key code constant: Fast Forward media key.
int KEYCODE_MEDIA_NEXT Key code constant: Play Next media key.
int KEYCODE_MEDIA_PLAY_PAUSE Key code constant: Play/Pause media key.
int KEYCODE_MEDIA_PREVIOUS Key code constant: Play Previous media key.
int KEYCODE_MEDIA_REWIND Key code constant: Rewind media key.
int KEYCODE_MEDIA_STOP Key code constant: Stop media key.
int KEYCODE_MENU Key code constant: Menu key.
int KEYCODE_MINUS Key code constant: '-'.
int KEYCODE_MUTE Key code constant: Mute key.
int KEYCODE_N Key code constant: 'N' key.
int KEYCODE_NOTIFICATION Key code constant: Notification key.
int KEYCODE_NUM Key code constant: Number modifier key.
int KEYCODE_O Key code constant: 'O' key.
int KEYCODE_P Key code constant: 'P' key.
int KEYCODE_PAGE_DOWN Key code constant: Page Down key.
int KEYCODE_PAGE_UP Key code constant: Page Up key.
int KEYCODE_PERIOD Key code constant: '.' key.
int KEYCODE_PICTSYMBOLS Key code constant: Picture Symbols modifier key.
int KEYCODE_PLUS Key code constant: '+' key.
int KEYCODE_POUND Key code constant: '#' key.
int KEYCODE_POWER Key code constant: Power key.
int KEYCODE_Q Key code constant: 'Q' key.
int KEYCODE_R Key code constant: 'R' key.
int KEYCODE_RIGHT_BRACKET Key code constant: ']' key.
int KEYCODE_S Key code constant: 'S' key.
int KEYCODE_SEARCH Key code constant: Search key.
int KEYCODE_SEMICOLON Key code constant: ';' key.
int KEYCODE_SHIFT_LEFT Key code constant: Left Shift modifier key.
int KEYCODE_SHIFT_RIGHT Key code constant: Right Shift modifier key.
int KEYCODE_SLASH Key code constant: '/' key.
int KEYCODE_SOFT_LEFT Key code constant: Soft Left key.
int KEYCODE_SOFT_RIGHT Key code constant: Soft Right key.
int KEYCODE_SPACE Key code constant: Space key.
int KEYCODE_STAR Key code constant: '*' key.
int KEYCODE_SWITCH_CHARSET Key code constant: Switch Charset modifier key.
int KEYCODE_SYM Key code constant: Symbol modifier key.
int KEYCODE_T Key code constant: 'T' key.
int KEYCODE_TAB Key code constant: Tab key.
int KEYCODE_U Key code constant: 'U' key.
int KEYCODE_UNKNOWN Key code constant: Unknown key code.
int KEYCODE_V Key code constant: 'V' key.
int KEYCODE_VOLUME_DOWN Key code constant: Volume Down key.
int KEYCODE_VOLUME_UP Key code constant: Volume Up key.
int KEYCODE_W Key code constant: 'W' key.
int KEYCODE_X Key code constant: 'X' key.
int KEYCODE_Y Key code constant: 'Y' key.
int KEYCODE_Z Key code constant: 'Z' key.
int MAX_KEYCODE This constant is deprecated. There are now more than MAX_KEYCODE keycodes. Use getMaxKeyCode() instead.
int META_ALT_LEFT_ON

This mask is used to check whether the left ALT meta key is pressed.

int META_ALT_ON

This mask is used to check whether one of the ALT meta keys is pressed.

int META_ALT_RIGHT_ON

This mask is used to check whether the right the ALT meta key is pressed.

int META_SHIFT_LEFT_ON

This mask is used to check whether the left SHIFT meta key is pressed.

int META_SHIFT_ON

This mask is used to check whether one of the SHIFT meta keys is pressed.

int META_SHIFT_RIGHT_ON

This mask is used to check whether the right SHIFT meta key is pressed.

int META_SYM_ON

This mask is used to check whether the SYM meta key is pressed.

[Expand]
Inherited Constants
From class android.view.InputEvent
From interface android.os.Parcelable
Public Constructors
KeyEvent(int action, int code)
Create a new key event.
KeyEvent(long downTime, long eventTime, int action, int code, int repeat)
Create a new key event.
KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState)
Create a new key event.
KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode)
Create a new key event.
KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags)
Create a new key event.
KeyEvent(long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source)
Create a new key event.
KeyEvent(long time, String characters, int deviceId, int flags)
Create a new key event for a string of characters.
KeyEvent(KeyEvent origEvent)
Make an exact copy of an existing key event.
KeyEvent(KeyEvent origEvent, long eventTime, int newRepeat)
This constructor is deprecated. Use changeTimeRepeat(KeyEvent, long, int) instead.
Public Methods
static KeyEvent changeAction(KeyEvent event, int action)
Create a new key event that is the same as the given one, but whose action is replaced with the given value.
static KeyEvent changeFlags(KeyEvent event, int flags)
Create a new key event that is the same as the given one, but whose flags are replaced with the given value.
static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat)
Create a new key event that is the same as the given one, but whose event time and repeat count are replaced with the given value.
static KeyEvent changeTimeRepeat(KeyEvent event, long eventTime, int newRepeat, int newFlags)
Create a new key event that is the same as the given one, but whose event time and repeat count are replaced with the given value.
final boolean dispatch(KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target)
Deliver this key event to a KeyEvent.Callback interface.
final boolean dispatch(KeyEvent.Callback receiver)
This method is deprecated. Use dispatch(Callback, DispatcherState, Object) instead.
final int getAction()
Retrieve the action of this key event.
final String getCharacters()
For the special case of a ACTION_MULTIPLE event with key code of KEYCODE_UNKNOWN, this is a raw string of characters associated with the event.
static int getDeadChar(int accent, int c)
Get the character that is produced by putting accent on the character c.
char getDisplayLabel()
Get the primary character for this key.
final long getDownTime()
Retrieve the time of the most recent key down event, in the uptimeMillis() time base.
final long getEventTime()
Retrieve the time this event occurred, in the uptimeMillis() time base.
final int getFlags()
Returns the flags for this key event.
final int getKeyCode()
Retrieve the key code of the key event.
boolean getKeyData(KeyCharacterMap.KeyData results)
Get the characters conversion data for the key event..
char getMatch(char[] chars, int modifiers)
If one of the chars in the array can be generated by the keyCode of this key event, return the char; otherwise return '\0'.
char getMatch(char[] chars)
The same as getMatch(chars, 0).
static int getMaxKeyCode()
Returns the maximum keycode.
final int getMetaState()

Returns the state of the meta keys.

char getNumber()
Gets the number or symbol associated with the key.
final int getRepeatCount()
Retrieve the repeat count of the event.
final int getScanCode()
Retrieve the hardware key id of this key event. These values are not reliable and vary from device to device.
int getUnicodeChar()

Returns the Unicode character that the key would produce.

int getUnicodeChar(int meta)

Returns the Unicode character that the key would produce.

final boolean isAltPressed()

Returns the pressed state of the ALT meta key.

final boolean isCanceled()
For ACTION_UP events, indicates that the event has been canceled as per FLAG_CANCELED.
final boolean isLongPress()
For ACTION_DOWN events, indicates that the event has been canceled as per FLAG_LONG_PRESS.
static boolean isModifierKey(int keyCode)
Returns true if this key code is a modifier key.
boolean isPrintingKey()
Does the key code of this key produce a glyph?
final boolean isShiftPressed()

Returns the pressed state of the SHIFT meta key.

final boolean isSymPressed()

Returns the pressed state of the SYM meta key.

final boolean isSystem()
Is this a system key? System keys can not be used for menu shortcuts.
final boolean isTracking()
For ACTION_UP events, indicates that the event is still being tracked from its initial down event as per FLAG_TRACKING.
final void startTracking()
Call this during onKeyDown(int, KeyEvent) to have the system track the key through its final up (possibly including a long press).
String toString()
Returns a string containing a concise, human-readable description of this object.
void writeToParcel(Parcel out, int flags)
Flatten this object in to a Parcel.
[Expand]
Inherited Methods
From class android.view.InputEvent
From class java.lang.Object
From interface android.os.Parcelable

Constants

public static final int ACTION_DOWN

Since: API Level 1

getAction() value: the key has been pressed down.

Constant Value: 0 (0x00000000)

public static final int ACTION_MULTIPLE

Since: API Level 1

getAction() value: multiple duplicate key events have occurred in a row, or a complex string is being delivered. If the key code is not {#link KEYCODE_UNKNOWN then the {#link getRepeatCount() method returns the number of times the given key code should be executed. Otherwise, if the key code is KEYCODE_UNKNOWN, then this is a sequence of characters as returned by getCharacters().

Constant Value: 2 (0x00000002)

public static final int ACTION_UP

Since: API Level 1

getAction() value: the key has been released.

Constant Value: 1 (0x00000001)

public static final Creator<KeyEvent> CREATOR

Since: API Level 1

public static final int FLAG_CANCELED

Since: API Level 5

When associated with up key events, this indicates that the key press has been canceled. Typically this is used with virtual touch screen keys, where the user can slide from the virtual key area on to the display: in that case, the application will receive a canceled up event and should not perform the action normally associated with the key. Note that for this to work, the application can not perform an action for a key until it receives an up or the long press timeout has expired.

Constant Value: 32 (0x00000020)

public static final int FLAG_CANCELED_LONG_PRESS

Since: API Level 5

Set when a key event has FLAG_CANCELED set because a long press action was executed while it was down.

Constant Value: 256 (0x00000100)

public static final int FLAG_EDITOR_ACTION

Since: API Level 3

This mask is used for compatibility, to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done". This allows TextView to dispatch these as normal enter keys for old applications, but still do the appropriate action when receiving them.

Constant Value: 16 (0x00000010)

public static final int FLAG_FROM_SYSTEM

Since: API Level 3

This mask is set if an event was known to come from a trusted part of the system. That is, the event is known to come from the user, and could not have been spoofed by a third party component.

Constant Value: 8 (0x00000008)

public static final int FLAG_KEEP_TOUCH_MODE

Since: API Level 3

This mask is set if we don't want the key event to cause us to leave touch mode.

Constant Value: 4 (0x00000004)

public static final int FLAG_LONG_PRESS

Since: API Level 5

This flag is set for the first key repeat that occurs after the long press timeout.

Constant Value: 128 (0x00000080)

public static final int FLAG_SOFT_KEYBOARD

Since: API Level 3

This mask is set if the key event was generated by a software keyboard.

Constant Value: 2 (0x00000002)

public static final int FLAG_TRACKING

Since: API Level 5

Set for ACTION_UP when this event's key code is still being tracked from its initial down. That is, somebody requested that tracking started on the key down and a long press has not caused the tracking to be canceled.

Constant Value: 512 (0x00000200)

public static final int FLAG_VIRTUAL_HARD_KEY

Since: API Level 5

This key event was generated by a virtual (on-screen) hard key area. Typically this is an area of the touchscreen, outside of the regular display, dedicated to "hardware" buttons.

Constant Value: 64 (0x00000040)

public static final int FLAG_WOKE_HERE

Since: API Level 1

This mask is set if the device woke because of this key event.

Constant Value: 1 (0x00000001)

public static final int KEYCODE_0

Since: API Level 1

Key code constant: '0' key.

Constant Value: 7 (0x00000007)

public static final int KEYCODE_1

Since: API Level 1

Key code constant: '1' key.

Constant Value: 8 (0x00000008)

public static final int KEYCODE_2

Since: API Level 1

Key code constant: '2' key.

Constant Value: 9 (0x00000009)

public static final int KEYCODE_3

Since: API Level 1

Key code constant: '3' key.

Constant Value: 10 (0x0000000a)

public static final int KEYCODE_4

Since: API Level 1

Key code constant: '4' key.

Constant Value: 11 (0x0000000b)

public static final int KEYCODE_5

Since: API Level 1

Key code constant: '5' key.

Constant Value: 12 (0x0000000c)

public static final int KEYCODE_6

Since: API Level 1

Key code constant: '6' key.

Constant Value: 13 (0x0000000d)

public static final int KEYCODE_7

Since: API Level 1

Key code constant: '7' key.

Constant Value: 14 (0x0000000e)

public static final int KEYCODE_8

Since: API Level 1

Key code constant: '8' key.

Constant Value: 15 (0x0000000f)

public static final int KEYCODE_9

Since: API Level 1

Key code constant: '9' key.

Constant Value: 16 (0x00000010)

public static final int KEYCODE_A

Since: API Level 1

Key code constant: 'A' key.

Constant Value: 29 (0x0000001d)

public static final int KEYCODE_ALT_LEFT

Since: API Level 1

Key code constant: Left Alt modifier key.

Constant Value: 57 (0x00000039)

public static final int KEYCODE_ALT_RIGHT

Since: API Level 1

Key code constant: Right Alt modifier key.

Constant Value: 58 (0x0000003a)

public static final int KEYCODE_APOSTROPHE

Since: API Level 1

Key code constant: ''' (apostrophe) key.

Constant Value: 75 (0x0000004b)

public static final int KEYCODE_AT

Since: API Level 1

Key code constant: '@' key.

Constant Value: 77 (0x0000004d)

public static final int KEYCODE_B

Since: API Level 1

Key code constant: 'B' key.

Constant Value: 30 (0x0000001e)

public static final int KEYCODE_BACK

Since: API Level 1

Key code constant: Back key.

Constant Value: 4 (0x00000004)

public static final int KEYCODE_BACKSLASH

Since: API Level 1

Key code constant: '\' key.

Constant Value: 73 (0x00000049)

public static final int KEYCODE_BUTTON_A

Since: API Level 9

Key code constant: A Button key. On a game controller, the A button should be either the button labeled A or the first button on the upper row of controller buttons.

Constant Value: 96 (0x00000060)

public static final int KEYCODE_BUTTON_B

Since: API Level 9

Key code constant: B Button key. On a game controller, the B button should be either the button labeled B or the second button on the upper row of controller buttons.

Constant Value: 97 (0x00000061)

public static final int KEYCODE_BUTTON_C

Since: API Level 9

Key code constant: C Button key. On a game controller, the C button should be either the button labeled C or the third button on the upper row of controller buttons.

Constant Value: 98 (0x00000062)

public static final int KEYCODE_BUTTON_L1

Since: API Level 9

Key code constant: L1 Button key. On a game controller, the L1 button should be either the button labeled L1 (or L) or the top left trigger button.

Constant Value: 102 (0x00000066)

public static final int KEYCODE_BUTTON_L2

Since: API Level 9

Key code constant: L2 Button key. On a game controller, the L2 button should be either the button labeled L2 or the bottom left trigger button.

Constant Value: 104 (0x00000068)

public static final int KEYCODE_BUTTON_MODE

Since: API Level 9

Key code constant: Mode Button key. On a game controller, the button labeled Mode.

Constant Value: 110 (0x0000006e)

public static final int KEYCODE_BUTTON_R1

Since: API Level 9

Key code constant: R1 Button key. On a game controller, the R1 button should be either the button labeled R1 (or R) or the top right trigger button.

Constant Value: 103 (0x00000067)

public static final int KEYCODE_BUTTON_R2

Since: API Level 9

Key code constant: R2 Button key. On a game controller, the R2 button should be either the button labeled R2 or the bottom right trigger button.

Constant Value: 105 (0x00000069)

public static final int KEYCODE_BUTTON_SELECT

Since: API Level 9

Key code constant: Select Button key. On a game controller, the button labeled Select.

Constant Value: 109 (0x0000006d)

public static final int KEYCODE_BUTTON_START

Since: API Level 9

Key code constant: Start Button key. On a game controller, the button labeled Start.

Constant Value: 108 (0x0000006c)

public static final int KEYCODE_BUTTON_THUMBL

Since: API Level 9

Key code constant: Left Thumb Button key. On a game controller, the left thumb button indicates that the left (or only) joystick is pressed.

Constant Value: 106 (0x0000006a)

public static final int KEYCODE_BUTTON_THUMBR

Since: API Level 9

Key code constant: Right Thumb Button key. On a game controller, the right thumb button indicates that the right joystick is pressed.

Constant Value: 107 (0x0000006b)

public static final int KEYCODE_BUTTON_X

Since: API Level 9

Key code constant: X Button key. On a game controller, the X button should be either the button labeled X or the first button on the lower row of controller buttons.

Constant Value: 99 (0x00000063)

public static final int KEYCODE_BUTTON_Y

Since: API Level 9

Key code constant: Y Button key. On a game controller, the Y button should be either the button labeled Y or the second button on the lower row of controller buttons.

Constant Value: 100 (0x00000064)

public static final int KEYCODE_BUTTON_Z

Since: API Level 9

Key code constant: Z Button key. On a game controller, the Z button should be either the button labeled Z or the third button on the lower row of controller buttons.

Constant Value: 101 (0x00000065)

public static final int KEYCODE_C

Since: API Level 1

Key code constant: 'C' key.

Constant Value: 31 (0x0000001f)

public static final int KEYCODE_CALL

Since: API Level 1

Key code constant: Call key.

Constant Value: 5 (0x00000005)

public static final int KEYCODE_CAMERA

Since: API Level 1

Key code constant: Camera key. Used to launch a camera application or take pictures.

Constant Value: 27 (0x0000001b)

public static final int KEYCODE_CLEAR

Since: API Level 1

Key code constant: Clear key.

Constant Value: 28 (0x0000001c)

public static final int KEYCODE_COMMA

Since: API Level 1

Key code constant: ',' key.

Constant Value: 55 (0x00000037)

public static final int KEYCODE_D

Since: API Level 1

Key code constant: 'D' key.

Constant Value: 32 (0x00000020)

public static final int KEYCODE_DEL

Since: API Level 1

Key code constant: Backspace key. Deletes characters before the insertion point.

Constant Value: 67 (0x00000043)

public static final int KEYCODE_DPAD_CENTER

Since: API Level 1

Key code constant: Directional Pad Center key. May also be synthesized from trackball motions.

Constant Value: 23 (0x00000017)

public static final int KEYCODE_DPAD_DOWN

Since: API Level 1

Key code constant: Directional Pad Down key. May also be synthesized from trackball motions.

Constant Value: 20 (0x00000014)

public static final int KEYCODE_DPAD_LEFT

Since: API Level 1

Key code constant: Directional Pad Left key. May also be synthesized from trackball motions.

Constant Value: 21 (0x00000015)

public static final int KEYCODE_DPAD_RIGHT

Since: API Level 1

Key code constant: Directional Pad Right key. May also be synthesized from trackball motions.

Constant Value: 22 (0x00000016)

public static final int KEYCODE_DPAD_UP

Since: API Level 1

Key code constant: Directional Pad Up key. May also be synthesized from trackball motions.

Constant Value: 19 (0x00000013)

public static final int KEYCODE_E

Since: API Level 1

Key code constant: 'E' key.

Constant Value: 33 (0x00000021)

public static final int KEYCODE_ENDCALL

Since: API Level 1

Key code constant: End Call key.

Constant Value: 6 (0x00000006)

public static final int KEYCODE_ENTER

Since: API Level 1

Key code constant: Enter key.

Constant Value: 66 (0x00000042)

public static final int KEYCODE_ENVELOPE

Since: API Level 1

Key code constant: Envelope special function key. Used to launch a mail application.

Constant Value: 65 (0x00000041)

public static final int KEYCODE_EQUALS

Since: API Level 1

Key code constant: '=' key.

Constant Value: 70 (0x00000046)

public static final int KEYCODE_EXPLORER

Since: API Level 1

Key code constant: Explorer special function key. Used to launch a browser application.

Constant Value: 64 (0x00000040)

public static final int KEYCODE_F

Since: API Level 1

Key code constant: 'F' key.

Constant Value: 34 (0x00000022)

public static final int KEYCODE_FOCUS

Since: API Level 1

Key code constant: Camera Focus key. Used to focus the camera.

Constant Value: 80 (0x00000050)

public static final int KEYCODE_G

Since: API Level 1

Key code constant: 'G' key.

Constant Value: 35 (0x00000023)

public static final int KEYCODE_GRAVE

Since: API Level 1

Key code constant: '`' (backtick) key.

Constant Value: 68 (0x00000044)

public static final int KEYCODE_H

Since: API Level 1

Key code constant: 'H' key.

Constant Value: 36 (0x00000024)

public static final int KEYCODE_HEADSETHOOK

Since: API Level 1

Key code constant: Headset Hook key. Used to hang up calls and stop media.

Constant Value: 79 (0x0000004f)

public static final int KEYCODE_HOME

Since: API Level 1

Key code constant: Home key. This key is handled by the framework and is never delivered to applications.

Constant Value: 3 (0x00000003)

public static final int KEYCODE_I

Since: API Level 1

Key code constant: 'I' key.

Constant Value: 37 (0x00000025)

public static final int KEYCODE_J

Since: API Level 1

Key code constant: 'J' key.

Constant Value: 38 (0x00000026)

public static final int KEYCODE_K

Since: API Level 1

Key code constant: 'K' key.

Constant Value: 39 (0x00000027)

public static final int KEYCODE_L

Since: API Level 1

Key code constant: 'L' key.

Constant Value: 40 (0x00000028)

public static final int KEYCODE_LEFT_BRACKET

Since: API Level 1

Key code constant: '[' key.

Constant Value: 71 (0x00000047)

public static final int KEYCODE_M

Since: API Level 1

Key code constant: 'M' key.

Constant Value: 41 (0x00000029)

public static final int KEYCODE_MEDIA_FAST_FORWARD

Since: API Level 3

Key code constant: Fast Forward media key.

Constant Value: 90 (0x0000005a)

public static final int KEYCODE_MEDIA_NEXT

Since: API Level 3

Key code constant: Play Next media key.

Constant Value: 87 (0x00000057)

public static final int KEYCODE_MEDIA_PLAY_PAUSE

Since: API Level 3

Key code constant: Play/Pause media key.

Constant Value: 85 (0x00000055)

public static final int KEYCODE_MEDIA_PREVIOUS

Since: API Level 3

Key code constant: Play Previous media key.

Constant Value: 88 (0x00000058)

public static final int KEYCODE_MEDIA_REWIND

Since: API Level 3

Key code constant: Rewind media key.

Constant Value: 89 (0x00000059)

public static final int KEYCODE_MEDIA_STOP

Since: API Level 3

Key code constant: Stop media key.

Constant Value: 86 (0x00000056)

public static final int KEYCODE_MENU

Since: API Level 1

Key code constant: Menu key.

Constant Value: 82 (0x00000052)

public static final int KEYCODE_MINUS

Since: API Level 1

Key code constant: '-'.

Constant Value: 69 (0x00000045)

public static final int KEYCODE_MUTE

Since: API Level 3

Key code constant: Mute key.

Constant Value: 91 (0x0000005b)

public static final int KEYCODE_N

Since: API Level 1

Key code constant: 'N' key.

Constant Value: 42 (0x0000002a)

public static final int KEYCODE_NOTIFICATION

Since: API Level 1

Key code constant: Notification key.

Constant Value: 83 (0x00000053)

public static final int KEYCODE_NUM

Since: API Level 1

Key code constant: Number modifier key. Used to enter numeric symbols. This key is not Num Lock; it is more like KEYCODE_ALT_LEFT and is interpreted as an ALT key by MetaKeyKeyListener.

Constant Value: 78 (0x0000004e)

public static final int KEYCODE_O

Since: API Level 1

Key code constant: 'O' key.

Constant Value: 43 (0x0000002b)

public static final int KEYCODE_P

Since: API Level 1

Key code constant: 'P' key.

Constant Value: 44 (0x0000002c)

public static final int KEYCODE_PAGE_DOWN

Since: API Level 9

Key code constant: Page Down key.

Constant Value: 93 (0x0000005d)

public static final int KEYCODE_PAGE_UP

Since: API Level 9

Key code constant: Page Up key.

Constant Value: 92 (0x0000005c)

public static final int KEYCODE_PERIOD

Since: API Level 1

Key code constant: '.' key.

Constant Value: 56 (0x00000038)

public static final int KEYCODE_PICTSYMBOLS

Since: API Level 9

Key code constant: Picture Symbols modifier key. Used to switch symbol sets (Emoji, Kao-moji).

Constant Value: 94 (0x0000005e)

public static final int KEYCODE_PLUS

Since: API Level 1

Key code constant: '+' key.

Constant Value: 81 (0x00000051)

public static final int KEYCODE_POUND

Since: API Level 1

Key code constant: '#' key.

Constant Value: 18 (0x00000012)

public static final int KEYCODE_POWER

Since: API Level 1

Key code constant: Power key.

Constant Value: 26 (0x0000001a)

public static final int KEYCODE_Q

Since: API Level 1

Key code constant: 'Q' key.

Constant Value: 45 (0x0000002d)

public static final int KEYCODE_R

Since: API Level 1

Key code constant: 'R' key.

Constant Value: 46 (0x0000002e)

public static final int KEYCODE_RIGHT_BRACKET

Since: API Level 1

Key code constant: ']' key.

Constant Value: 72 (0x00000048)

public static final int KEYCODE_S

Since: API Level 1

Key code constant: 'S' key.

Constant Value: 47 (0x0000002f)

public static final int KEYCODE_SEARCH

Since: API Level 1

Key code constant: Search key.

Constant Value: 84 (0x00000054)

public static final int KEYCODE_SEMICOLON

Since: API Level 1

Key code constant: ';' key.

Constant Value: 74 (0x0000004a)

public static final int KEYCODE_SHIFT_LEFT

Since: API Level 1

Key code constant: Left Shift modifier key.

Constant Value: 59 (0x0000003b)

public static final int KEYCODE_SHIFT_RIGHT

Since: API Level 1

Key code constant: Right Shift modifier key.

Constant Value: 60 (0x0000003c)

public static final int KEYCODE_SLASH

Since: API Level 1

Key code constant: '/' key.

Constant Value: 76 (0x0000004c)

public static final int KEYCODE_SOFT_LEFT

Since: API Level 1

Key code constant: Soft Left key. Usually situated below the display on phones and used as a multi-function feature key for selecting a software defined function shown on the bottom left of the display.

Constant Value: 1 (0x00000001)

public static final int KEYCODE_SOFT_RIGHT

Since: API Level 1

Key code constant: Soft Right key. Usually situated below the display on phones and used as a multi-function feature key for selecting a software defined function shown on the bottom right of the display.

Constant Value: 2 (0x00000002)

public static final int KEYCODE_SPACE

Since: API Level 1

Key code constant: Space key.

Constant Value: 62 (0x0000003e)

public static final int KEYCODE_STAR

Since: API Level 1

Key code constant: '*' key.

Constant Value: 17 (0x00000011)

public static final int KEYCODE_SWITCH_CHARSET

Since: API Level 9

Key code constant: Switch Charset modifier key. Used to switch character sets (Kanji, Katakana).

Constant Value: 95 (0x0000005f)

public static final int KEYCODE_SYM

Since: API Level 1

Key code constant: Symbol modifier key. Used to enter alternate symbols.

Constant Value: 63 (0x0000003f)

public static final int KEYCODE_T

Since: API Level 1

Key code constant: 'T' key.

Constant Value: 48 (0x00000030)

public static final int KEYCODE_TAB

Since: API Level 1

Key code constant: Tab key.

Constant Value: 61 (0x0000003d)

public static final int KEYCODE_U

Since: API Level 1

Key code constant: 'U' key.

Constant Value: 49 (0x00000031)

public static final int KEYCODE_UNKNOWN

Since: API Level 1

Key code constant: Unknown key code.

Constant Value: 0 (0x00000000)

public static final int KEYCODE_V

Since: API Level 1

Key code constant: 'V' key.

Constant Value: 50 (0x00000032)

public static final int KEYCODE_VOLUME_DOWN

Since: API Level 1

Key code constant: Volume Down key.

Constant Value: 25 (0x00000019)

public static final int KEYCODE_VOLUME_UP

Since: API Level 1

Key code constant: Volume Up key.

Constant Value: 24 (0x00000018)

public static final int KEYCODE_W

Since: API Level 1

Key code constant: 'W' key.

Constant Value: 51 (0x00000033)

public static final int KEYCODE_X

Since: API Level 1

Key code constant: 'X' key.

Constant Value: 52 (0x00000034)

public static final int KEYCODE_Y

Since: API Level 1

Key code constant: 'Y' key.

Constant Value: 53 (0x00000035)

public static final int KEYCODE_Z

Since: API Level 1

Key code constant: 'Z' key.

Constant Value: 54 (0x00000036)

public static final int MAX_KEYCODE

Since: API Level 1

This constant is deprecated.
There are now more than MAX_KEYCODE keycodes. Use getMaxKeyCode() instead.

Constant Value: 84 (0x00000054)

public static final int META_ALT_LEFT_ON

Since: API Level 1

This mask is used to check whether the left ALT meta key is pressed.

Constant Value: 16 (0x00000010)

public static final int META_ALT_ON

Since: API Level 1

This mask is used to check whether one of the ALT meta keys is pressed.

Constant Value: 2 (0x00000002)

public static final int META_ALT_RIGHT_ON

Since: API Level 1

This mask is used to check whether the right the ALT meta key is pressed.

Constant Value: 32 (0x00000020)

public static final int META_SHIFT_LEFT_ON

Since: API Level 1

This mask is used to check whether the left SHIFT meta key is pressed.

Constant Value: 64 (0x00000040)

public static final int META_SHIFT_ON

Since: API Level 1

This mask is used to check whether one of the SHIFT meta keys is pressed.

Constant Value: 1 (0x00000001)

public static final int META_SHIFT_RIGHT_ON

Since: API Level 1

This mask is used to check whether the right SHIFT meta key is pressed.

Constant Value: 128 (0x00000080)

public static final int META_SYM_ON

Since: API Level 1

This mask is used to check whether the SYM meta key is pressed.

Constant Value: 4 (0x00000004)

Public Constructors

public KeyEvent (int action, int code)

Since: API Level 1

Create a new key event.

Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.

public KeyEvent (long downTime, long eventTime, int action, int code, int repeat)

Since: API Level 1

Create a new key event.

Parameters
downTime The time (in uptimeMillis()) at which this key code originally went down.
eventTime The time (in uptimeMillis()) at which this event happened.
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.
repeat A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.

public KeyEvent (long downTime, long eventTime, int action, int code, int repeat, int metaState)

Since: API Level 1

Create a new key event.

Parameters
downTime The time (in uptimeMillis()) at which this key code originally went down.
eventTime The time (in uptimeMillis()) at which this event happened.
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.
repeat A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
metaState Flags indicating which meta keys are currently pressed.

public KeyEvent (long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode)

Since: API Level 1

Create a new key event.

Parameters
downTime The time (in uptimeMillis()) at which this key code originally went down.
eventTime The time (in uptimeMillis()) at which this event happened.
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.
repeat A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
metaState Flags indicating which meta keys are currently pressed.
deviceId The device ID that generated the key event.
scancode Raw device scan code of the event.

public KeyEvent (long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags)

Since: API Level 1

Create a new key event.

Parameters
downTime The time (in uptimeMillis()) at which this key code originally went down.
eventTime The time (in uptimeMillis()) at which this event happened.
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.
repeat A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
metaState Flags indicating which meta keys are currently pressed.
deviceId The device ID that generated the key event.
scancode Raw device scan code of the event.
flags The flags for this key event

public KeyEvent (long downTime, long eventTime, int action, int code, int repeat, int metaState, int deviceId, int scancode, int flags, int source)

Since: API Level 9

Create a new key event.

Parameters
downTime The time (in uptimeMillis()) at which this key code originally went down.
eventTime The time (in uptimeMillis()) at which this event happened.
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code.
repeat A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
metaState Flags indicating which meta keys are currently pressed.
deviceId The device ID that generated the key event.
scancode Raw device scan code of the event.
flags The flags for this key event
source The input source such as SOURCE_KEYBOARD.

public KeyEvent (long time, String characters, int deviceId, int flags)

Since: API Level 3

Create a new key event for a string of characters. The key code, action, repeat count and source will automatically be set to KEYCODE_UNKNOWN, ACTION_MULTIPLE, 0, and SOURCE_KEYBOARD for you.

Parameters
time The time (in uptimeMillis()) at which this event occured.
characters The string of characters.
deviceId The device ID that generated the key event.
flags The flags for this key event

public KeyEvent (KeyEvent origEvent)

Since: API Level 3

Make an exact copy of an existing key event.

public KeyEvent (KeyEvent origEvent, long eventTime, int newRepeat)

Since: API Level 1

This constructor is deprecated.
Use changeTimeRepeat(KeyEvent, long, int) instead.

Copy an existing key event, modifying its time and repeat count.

Parameters
origEvent The existing event to be copied.
eventTime The new event time (in uptimeMillis()) of the event.
newRepeat The new repeat count of the event.

Public Methods

public static KeyEvent changeAction (KeyEvent event, int action)

Since: API Level 3

Create a new key event that is the same as the given one, but whose action is replaced with the given value.

Parameters
event The existing event to be copied. This is not modified.
action The new action code of the event.

public static KeyEvent changeFlags (KeyEvent event, int flags)

Since: API Level 3

Create a new key event that is the same as the given one, but whose flags are replaced with the given value.

Parameters
event The existing event to be copied. This is not modified.
flags The new flags constant.

public static KeyEvent changeTimeRepeat (KeyEvent event, long eventTime, int newRepeat)

Since: API Level 3

Create a new key event that is the same as the given one, but whose event time and repeat count are replaced with the given value.

Parameters
event The existing event to be copied. This is not modified.
eventTime The new event time (in uptimeMillis()) of the event.
newRepeat The new repeat count of the event.

public static KeyEvent changeTimeRepeat (KeyEvent event, long eventTime, int newRepeat, int newFlags)

Since: API Level 5

Create a new key event that is the same as the given one, but whose event time and repeat count are replaced with the given value.

Parameters
event The existing event to be copied. This is not modified.
eventTime The new event time (in uptimeMillis()) of the event.
newRepeat The new repeat count of the event.
newFlags New flags for the event, replacing the entire value in the original event.

public final boolean dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target)

Since: API Level 5

Deliver this key event to a KeyEvent.Callback interface. If this is an ACTION_MULTIPLE event and it is not handled, then an attempt will be made to deliver a single normal event.

Parameters
receiver The Callback that will be given the event.
state State information retained across events.
target The target of the dispatch, for use in tracking.
Returns
  • The return value from the Callback method that was called.

public final boolean dispatch (KeyEvent.Callback receiver)

Since: API Level 1

This method is deprecated.
Use dispatch(Callback, DispatcherState, Object) instead.

public final int getAction ()

Since: API Level 1

Retrieve the action of this key event. May be either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.

Returns
  • The event action: ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.

public final String getCharacters ()

Since: API Level 3

For the special case of a ACTION_MULTIPLE event with key code of KEYCODE_UNKNOWN, this is a raw string of characters associated with the event. In all other cases it is null.

Returns
  • Returns a String of 1 or more characters associated with the event.

public static int getDeadChar (int accent, int c)

Since: API Level 1

Get the character that is produced by putting accent on the character c. For example, getDeadChar('`', 'e') returns è.

public char getDisplayLabel ()

Since: API Level 1

Get the primary character for this key. In other words, the label that is physically printed on it.

public final long getDownTime ()

Since: API Level 1

Retrieve the time of the most recent key down event, in the uptimeMillis() time base. If this is a down event, this will be the same as getEventTime(). Note that when chording keys, this value is the down time of the most recently pressed key, which may not be the same physical key of this event.

Returns
  • Returns the most recent key down time, in the uptimeMillis() time base

public final long getEventTime ()

Since: API Level 1

Retrieve the time this event occurred, in the uptimeMillis() time base.

Returns
  • Returns the time this event occurred, in the uptimeMillis() time base.

public final int getFlags ()

Since: API Level 1

Returns the flags for this key event.

See Also

public final int getKeyCode ()

Since: API Level 1

Retrieve the key code of the key event. This is the physical key that was pressed, not the Unicode character.

Returns
  • The key code of the event.

public boolean getKeyData (KeyCharacterMap.KeyData results)

Since: API Level 1

Get the characters conversion data for the key event..

Parameters
results a KeyCharacterMap.KeyData that will be filled with the results.
Returns
  • whether the key was mapped or not. If the key was not mapped, results is not modified.

public char getMatch (char[] chars, int modifiers)

Since: API Level 1

If one of the chars in the array can be generated by the keyCode of this key event, return the char; otherwise return '\0'.

Parameters
chars the characters to try to find
modifiers the modifier bits to prefer. If any of these bits are set, if there are multiple choices, that could work, the one for this modifier will be set.

public char getMatch (char[] chars)

Since: API Level 1

The same as getMatch(chars, 0).

public static int getMaxKeyCode ()

Since: API Level 3

Returns the maximum keycode.

public final int getMetaState ()

Since: API Level 1

Returns the state of the meta keys.

Returns
  • an integer in which each bit set to 1 represents a pressed meta key

public char getNumber ()

Since: API Level 1

Gets the number or symbol associated with the key. The character value is returned, not the numeric value. If the key is not a number, but is a symbol, the symbol is retuned.

public final int getRepeatCount ()

Since: API Level 1

Retrieve the repeat count of the event. For both key up and key down events, this is the number of times the key has repeated with the first down starting at 0 and counting up from there. For multiple key events, this is the number of down/up pairs that have occurred.

Returns
  • The number of times the key has repeated.

public final int getScanCode ()

Since: API Level 1

Retrieve the hardware key id of this key event. These values are not reliable and vary from device to device.

Mostly this is here for debugging purposes.

public int getUnicodeChar ()

Since: API Level 1

Returns the Unicode character that the key would produce.

Returns 0 if the key is not one that is used to type Unicode characters.

If the return value has bit COMBINING_ACCENT set, the key is a "dead key" that should be combined with another to actually produce a character -- see getDeadChar(int, int) -- after masking with COMBINING_ACCENT_MASK.

public int getUnicodeChar (int meta)

Since: API Level 1

Returns the Unicode character that the key would produce.

Returns 0 if the key is not one that is used to type Unicode characters.

If the return value has bit COMBINING_ACCENT set, the key is a "dead key" that should be combined with another to actually produce a character -- see getDeadChar(int, int) -- after masking with COMBINING_ACCENT_MASK.

public final boolean isAltPressed ()

Since: API Level 1

Returns the pressed state of the ALT meta key.

Returns
  • true if the ALT key is pressed, false otherwise

public final boolean isCanceled ()

Since: API Level 5

For ACTION_UP events, indicates that the event has been canceled as per FLAG_CANCELED.

public final boolean isLongPress ()

Since: API Level 5

For ACTION_DOWN events, indicates that the event has been canceled as per FLAG_LONG_PRESS.

public static boolean isModifierKey (int keyCode)

Since: API Level 1

Returns true if this key code is a modifier key.

Returns

public boolean isPrintingKey ()

Since: API Level 1

Does the key code of this key produce a glyph?

public final boolean isShiftPressed ()

Since: API Level 1

Returns the pressed state of the SHIFT meta key.

Returns
  • true if the SHIFT key is pressed, false otherwise

public final boolean isSymPressed ()

Since: API Level 1

Returns the pressed state of the SYM meta key.

Returns
  • true if the SYM key is pressed, false otherwise

public final boolean isSystem ()

Since: API Level 1

Is this a system key? System keys can not be used for menu shortcuts. TODO: this information should come from a table somewhere. TODO: should the dpad keys be here? arguably, because they also shouldn't be menu shortcuts

public final boolean isTracking ()

Since: API Level 5

For ACTION_UP events, indicates that the event is still being tracked from its initial down event as per FLAG_TRACKING.

public final void startTracking ()

Since: API Level 5

Call this during onKeyDown(int, KeyEvent) to have the system track the key through its final up (possibly including a long press). Note that only one key can be tracked at a time -- if another key down event is received while a previous one is being tracked, tracking is stopped on the previous event.

public String toString ()

Since: API Level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public void writeToParcel (Parcel out, int flags)

Since: API Level 1

Flatten this object in to a Parcel.

Parameters
out The Parcel in which the object should be written.
flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.