Android Guides | Samples

Android.Views.KeyEvent Class

Object used to report key and button events.

See Also: KeyEvent

Syntax

[Android.Runtime.Register("android/view/KeyEvent", DoNotGenerateAcw=true)]
public class KeyEvent : InputEvent, IDisposable

Remarks

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 KeyEvent.ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with KeyEvent.ACTION_DOWN and a non-zero value for KeyEvent.RepeatCount. The last key event is a KeyEvent.ACTION_UP for the key up. If the key press is canceled, the key up event will have the KeyEvent.FLAG_CANCELED flag set.

Key events are generally accompanied by a key code (KeyEvent.KeyCode), scan code (KeyEvent.ScanCode) and meta state (KeyEvent.MetaState). 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 KeyEvent.META_SHIFT_ON or KeyEvent.META_ALT_ON.

Key codes typically correspond one-to-one with individual keys on an input device. Many keys and key combinations serve quite different functions on different input devices so care must be taken when interpreting them. Always use the KeyCharacterMap associated with the input device when mapping keys to characters. Be aware that there may be multiple key input devices active at the same time and each will have its own key character map.

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like Android.Views.InputMethods.EditorInfo.IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses.

When interacting with an IME, the framework may deliver key events with the special action KeyEvent.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 KeyEvent.FLAG_CANCELED and should tolerate anomalous situations such as receiving a new KeyEvent.ACTION_DOWN without first having received an KeyEvent.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.

[Android Documentation]

Requirements

Namespace: Android.Views
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Android.Views.KeyEvent are listed below.

See Also: InputEvent

Public Constructors

Make an exact copy of an existing key event.
Create a new key event.
Create a new key event.
Copy an existing key event, modifying its time and repeat count.
Create a new key event for a string of characters.
Create a new key event.
Create a new key event.
Create a new key event.
Create a new key event.

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
MaxKeycodeInt32 (84).

Public Properties

[read-only]
ActionKeyEventActions. Retrieve the action of this key event.
[read-only]
CharactersString. For the special case of a KeyEvent.ACTION_MULTIPLE event with key code of KeyEvent.KEYCODE_UNKNOWN, this is a raw string of characters associated with the event.
[read-only]
static
CreatorIParcelableCreator.
[read-only]
override
DeviceIdInt32. Gets the id for the device that this event came from.
[read-only]
DisplayLabelChar. Gets the primary character for this key.
[read-only]
DownTimeInt64. Retrieve the time of the most recent key down event, in the SystemClock.UptimeMillis time base.
[read-only]
override
EventTimeInt64. Retrieve the time this event occurred, in the SystemClock.UptimeMillis time base.
[read-only]
FlagsKeyEventFlags. Returns the flags for this key event.
[read-only]
HasNoModifiersBoolean. Returns true if no modifier keys are pressed.
[read-only]
IsAltPressedBoolean.
[read-only]
IsCanceledBoolean. For KeyEvent.ACTION_UP events, indicates that the event has been canceled as per KeyEvent.FLAG_CANCELED.
[read-only]
IsCapsLockOnBoolean.
[read-only]
IsCtrlPressedBoolean.
[read-only]
IsFunctionPressedBoolean.
[read-only]
IsLongPressBoolean. For KeyEvent.ACTION_DOWN events, indicates that the event has been canceled as per KeyEvent.FLAG_LONG_PRESS.
[read-only]
IsMetaPressedBoolean.
[read-only]
IsNumLockOnBoolean.
[read-only]
IsPrintingKeyBoolean. Returns true if this key produces a glyph.
[read-only]
IsScrollLockOnBoolean.
[read-only]
IsShiftPressedBoolean.
[read-only]
IsSymPressedBoolean.
[read-only]
IsSystemBoolean. Is this a system key? System keys can not be used for menu shortcuts.
[read-only]
IsTrackingBoolean. For KeyEvent.ACTION_UP events, indicates that the event is still being tracked from its initial down event as per KeyEvent.FLAG_TRACKING.
[read-only]
KeyCharacterMapKeyCharacterMap. Gets the KeyCharacterMap associated with the keyboard device.
[read-only]
KeyCodeKeycode. Retrieve the key code of the key event.
[read-only]
static
MaxKeyCodeKeycode. Returns the maximum keycode.
[read-only]
MetaStateMetaKeyStates.
[read-only]
static
ModifierMetaStateMaskMetaKeyStates. Gets a mask that includes all valid modifier key meta state bits.
[read-only]
ModifiersMetaKeyStates. Returns the state of the modifier keys.
[read-only]
NumberChar. Gets the number or symbol associated with the key.
[read-only]
RepeatCountInt32. Retrieve the repeat count of the event.
[read-only]
ScanCodeInt32. Retrieve the hardware key id of this key event.
[read-only]
override
SourceInputSourceType. Gets the source of the event.
[read-only]
UnicodeCharInt32. Gets the Unicode character generated by the specified key and meta key state combination.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

static
ChangeAction(KeyEvent, KeyEventActions) : KeyEvent
Create a new key event that is the same as the given one, but whose action is replaced with the given value.
static
ChangeFlags(KeyEvent, KeyEventFlags) : KeyEvent
Create a new key event that is the same as the given one, but whose flags are replaced with the given value.
static
ChangeTimeRepeat(KeyEvent, Int64, Int32) : KeyEvent
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
ChangeTimeRepeat(KeyEvent, Int64, Int32, KeyEventFlags) : KeyEvent
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.
override
DescribeContents() : Int32
Dispatch(KeyEvent+ICallback) : Boolean
Dispatch(KeyEvent+ICallback, KeyEvent+DispatcherState, Object) : Boolean
Deliver this key event to a NoType:android/view/KeyEvent$Callback;Href=../../../reference/android/view/KeyEvent.Callback.html interface.
static
GetDeadChar(Int32, Int32) : Int32
Get the character that is produced by putting accent on the character c.
GetKeyData(KeyCharacterMap+KeyData) : Boolean
Get the character conversion data for a given key code.
GetMatch(Char[]) : Char
Gets the first character in the character array that can be generated by the specified key code.
GetMatch(Char[], MetaKeyStates) : Char
Gets the first character in the character array that can be generated by the specified key code.
GetMatch(Char[], Int32) : Char
GetUnicodeChar(MetaKeyStates) : Int32
Gets the Unicode character generated by the specified key and meta key state combination.
HasModifiers(MetaKeyStates) : Boolean
Returns true if only the specified modifiers keys are pressed.
static
IsGamepadButton(Keycode) : Boolean
Returns true if the specified keycode is a gamepad button.
static
IsModifierKey(Keycode) : Boolean
Returns true if this key code is a modifier key.
static
KeyCodeFromString(String) : Keycode
Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent numeric constant such as "1001".
static
KeyCodeToString(Keycode) : String
Returns a string that represents the symbolic name of the specified keycode such as "KEYCODE_A", "KEYCODE_DPAD_UP", or an equivalent numeric constant such as "1001" if unknown.
static
MetaStateHasModifiers(MetaKeyStates, MetaKeyStates) : Boolean
Returns true if only the specified modifier keys are pressed according to the specified meta state.
static
MetaStateHasNoModifiers(MetaKeyStates) : Boolean
Returns true if no modifiers keys are pressed according to the specified meta state.
static
NormalizeMetaState(MetaKeyStates) : MetaKeyStates
Normalizes the specified meta state.
SetSource(InputSourceType)
Modifies the source of the event.
StartTracking()
Call this during NoType:android/view/KeyEvent$Callback;Href=../../../reference/android/view/KeyEvent.Callback.html#onKeyDown(int, android.view.KeyEvent) to have the system track the key through its final up (possibly including a long press).
override
WriteToParcel(Parcel, ParcelableWriteFlags)
Flatten this object in to a Parcel.