Compartilhar via


KeyEvent Class

Definition

Object used to report key and button events.

[Android.Runtime.Register("android/view/KeyEvent", DoNotGenerateAcw=true)]
public class KeyEvent : Android.Views.InputEvent, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("android/view/KeyEvent", DoNotGenerateAcw=true)>]
type KeyEvent = class
    inherit InputEvent
    interface IParcelable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Attributes
Implements

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 #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.

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.view.inputmethod.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 #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.

Java documentation for android.view.KeyEvent.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

KeyEvent(Int64, Int64, KeyEventActions, Keycode, Int32, MetaKeyStates, Int32, Int32, KeyEventFlags, InputSourceType)

Create a new key event.

KeyEvent(Int64, Int64, KeyEventActions, Keycode, Int32, MetaKeyStates, Int32, Int32, KeyEventFlags)

Create a new key event.

KeyEvent(Int64, Int64, KeyEventActions, Keycode, Int32, MetaKeyStates, Int32, Int32)

Create a new key event.

KeyEvent(Int64, Int64, KeyEventActions, Keycode, Int32, MetaKeyStates)

Create a new key event.

KeyEvent(Int64, Int64, KeyEventActions, Keycode, Int32)

Create a new key event.

KeyEvent(Int64, String, Int32, KeyEventFlags)

Create a new key event for a string of characters.

KeyEvent(IntPtr, JniHandleOwnership)

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

KeyEvent(KeyEvent, Int64, Int32)
Obsolete.

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

KeyEvent(KeyEvent)

Make an exact copy of an existing key event.

KeyEvent(KeyEventActions, Keycode)

Create a new key event.

Fields

KeycodeDemoApp1
Obsolete.

Key code constant: Demo Application key #1.

KeycodeDemoApp2
Obsolete.

Key code constant: Demo Application key #2.

KeycodeDemoApp3
Obsolete.

Key code constant: Demo Application key #3.

KeycodeDemoApp4
Obsolete.

Key code constant: Demo Application key #4.

KeycodeFeaturedApp1
Obsolete.

Key code constant: Featured Application key #1.

KeycodeFeaturedApp2
Obsolete.

Key code constant: Featured Application key #2.

KeycodeFeaturedApp3
Obsolete.

Key code constant: Featured Application key #3.

KeycodeFeaturedApp4
Obsolete.

Key code constant: Featured Application key #4.

KeycodeKeyboardBacklightDown
Obsolete.

Key code constant: Keyboard backlight down

KeycodeKeyboardBacklightToggle
Obsolete.

Key code constant: Keyboard backlight toggle

KeycodeKeyboardBacklightUp
Obsolete.

Key code constant: Keyboard backlight up

KeycodeMacro1
Obsolete.

Key code constant: A button whose usage can be customized by the user through the system.

KeycodeMacro2
Obsolete.

Key code constant: A button whose usage can be customized by the user through the system.

KeycodeMacro3
Obsolete.

Key code constant: A button whose usage can be customized by the user through the system.

KeycodeMacro4
Obsolete.

Key code constant: A button whose usage can be customized by the user through the system.

KeycodeRecentApps
Obsolete.

Key code constant: To open recent apps view (a.

KeycodeStylusButtonPrimary
Obsolete.

Key code constant: The primary button on the barrel of a stylus.

KeycodeStylusButtonSecondary
Obsolete.

Key code constant: The secondary button on the barrel of a stylus.

KeycodeStylusButtonTail
Obsolete.

Key code constant: A button on the tail end of a stylus.

KeycodeStylusButtonTertiary
Obsolete.

Key code constant: The tertiary button on the barrel of a stylus.

KeycodeVideoApp1
Obsolete.

Key code constant: Video Application key #1.

KeycodeVideoApp2
Obsolete.

Key code constant: Video Application key #2.

KeycodeVideoApp3
Obsolete.

Key code constant: Video Application key #3.

KeycodeVideoApp4
Obsolete.

Key code constant: Video Application key #4.

KeycodeVideoApp5
Obsolete.

Key code constant: Video Application key #5.

KeycodeVideoApp6
Obsolete.

Key code constant: Video Application key #6.

KeycodeVideoApp7
Obsolete.

Key code constant: Video Application key #7.

KeycodeVideoApp8
Obsolete.

Key code constant: Video Application key #8.

MaxKeycode
Obsolete.

This member is deprecated.

Properties

Action

Retrieve the action of this key event.

Characters

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.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Creator
Device

Gets the device that this event came from.

(Inherited from InputEvent)
DeviceId

To be added

DisplayLabel

Gets the primary character for this key.

DownTime

Retrieve the time of the most recent key down event, in the android.os.SystemClock#uptimeMillis time base.

EventTime

Retrieve the time this event occurred, in the android.os.SystemClock#uptimeMillis time base.

Flags

Returns the flags for this key event.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
HasNoModifiers

Returns true if no modifier keys are pressed.

IsAltPressed

Returns the pressed state of the ALT meta key.

IsCanceled

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

IsCapsLockOn

Returns the locked state of the CAPS LOCK meta key.

IsCtrlPressed

Returns the pressed state of the CTRL meta key.

IsFunctionPressed

Returns the pressed state of the FUNCTION meta key.

IsLongPress

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

IsMetaPressed

Returns the pressed state of the META meta key.

IsNumLockOn

Returns the locked state of the NUM LOCK meta key.

IsPrintingKey

Returns true if this key produces a glyph.

IsScrollLockOn

Returns the locked state of the SCROLL LOCK meta key.

IsShiftPressed

Returns the pressed state of the SHIFT meta key.

IsSymPressed

Returns the pressed state of the SYM meta key.

IsSystem

Is this a system key? System keys can not be used for menu shortcuts.

IsTracking

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

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
KeyCharacterMap

Gets the KeyCharacterMap associated with the keyboard device.

KeyCode

Retrieve the key code of the key event.

MaxKeyCode

Returns the maximum keycode.

MetaState

Returns the state of the meta keys.

ModifierMetaStateMask

Gets a mask that includes all valid modifier key meta state bits.

Modifiers

Returns the state of the modifier keys.

Number

Gets the number or symbol associated with the key.

PeerReference (Inherited from Object)
RepeatCount

Retrieve the repeat count of the event.

ScanCode

Retrieve the hardware key id of this key event.

Source

To be added

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

UnicodeChar

Gets the Unicode character generated by the specified key and meta key state combination.

Methods

ChangeAction(KeyEvent, KeyEventActions)

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

ChangeFlags(KeyEvent, KeyEventFlags)

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

ChangeTimeRepeat(KeyEvent, Int64, Int32, KeyEventFlags)

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.

ChangeTimeRepeat(KeyEvent, Int64, Int32)

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.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
DescribeContents()

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

(Inherited from InputEvent)
Dispatch(KeyEvent+ICallback, KeyEvent+DispatcherState, Object)

Deliver this key event to a KeyEvent Callback interface.

Dispatch(KeyEvent+ICallback)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetDeadChar(Int32, Int32)

Get the character that is produced by putting accent on the character c.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetKeyData(KeyCharacterMap+KeyData)
Obsolete.

Get the character conversion data for a given key code.

GetMatch(Char[], Int32)
GetMatch(Char[], MetaKeyStates)

Gets the first character in the character array that can be generated by the specified key code.

GetMatch(Char[])

Gets the first character in the character array that can be generated by the specified key code.

GetUnicodeChar(Int32)
GetUnicodeChar(MetaKeyStates)

Gets the Unicode character generated by the specified key and meta key state combination.

HasModifiers(MetaKeyStates)

Returns true if only the specified modifiers keys are pressed.

IsFromSource(InputSourceType)

Determines whether the event is from the given source.

(Inherited from InputEvent)
IsGamepadButton(Keycode)

Returns true if the specified keycode is a gamepad button.

IsMediaSessionKey(Int32)

Returns whether this key will be sent to the android.media.session.MediaSession.Callback if not handled.

IsModifierKey(Keycode)

Returns true if this key code is a modifier key.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
KeyCodeFromString(String)

Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent numeric constant such as "29".

KeyCodeToString(Keycode)

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.

MetaStateHasModifiers(MetaKeyStates, MetaKeyStates)

Returns true if only the specified modifier keys are pressed according to the specified meta state.

MetaStateHasNoModifiers(MetaKeyStates)

Returns true if no modifiers keys are pressed according to the specified meta state.

NormalizeMetaState(MetaKeyStates)

Normalizes the specified meta state.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetSource(InputSourceType)

To be added

StartTracking()

Call this during Callback#onKeyDown to have the system track the key through its final up (possibly including a long press).

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
WriteToParcel(Parcel, ParcelableWriteFlags)

Flatten this object in to a Parcel.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to