Class KeyboardEvent
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type (Elementkeydown, Elementkeypress, or Elementkeyup) identifies what kind of keyboard activity occurred.
[Value("KeyboardEvent")]
public class KeyboardEvent : UIEvent
- Inheritance
-
KeyboardEvent
- Inherited Members
Remarks
NOTE
KeyboardEventevents just indicate what interaction the user had with a key on the keyboard at a low level, providing no contextual meaning to that interaction. When you need to handle text input, use the Elementinput event instead. Keyboard events may not be fired if the user is using an alternate means of entering text, such as a handwriting system on a tablet or graphics tablet.
Constructors
KeyboardEvent()
public KeyboardEvent()
KeyboardEvent(string, KeyboardEventInit)
The KeyboardEvent() constructor creates a new
KeyboardEvent object.
public KeyboardEvent(string type, KeyboardEventInit eventInitDict = null)
Parameters
typestringeventInitDictKeyboardEventInit
Remarks
-KeyboardEvent, the interface of the objects it constructs.
Fields
DOM_KEY_LOCATION_LEFT
[Value("DOM_KEY_LOCATION_LEFT")]
public const ulong DOM_KEY_LOCATION_LEFT = 1
Field Value
DOM_KEY_LOCATION_NUMPAD
[Value("DOM_KEY_LOCATION_NUMPAD")]
public const ulong DOM_KEY_LOCATION_NUMPAD = 3
Field Value
DOM_KEY_LOCATION_RIGHT
[Value("DOM_KEY_LOCATION_RIGHT")]
public const ulong DOM_KEY_LOCATION_RIGHT = 2
Field Value
DOM_KEY_LOCATION_STANDARD
[Value("DOM_KEY_LOCATION_STANDARD")]
public const ulong DOM_KEY_LOCATION_STANDARD = 0
Field Value
Properties
AltKey
The KeyboardEvent.altKey read-only property is a
boolean value that indicates if the <kbd>alt</kbd> key (<kbd>Option</kbd> or <kbd>⌥</kbd> on macOS) was pressed (true) or not (false) when
the event occurred.
[Value("altKey")]
public bool AltKey { get; }
Property Value
- bool
A boolean value.
Remarks
CharCode
IMPORTANT
DeprecatedcharCode read-only property of theKeyboardEvent interface returns the Unicode value of a character key
pressed during a Elementkeypress event.
[Value("charCode")]
public ulong CharCode { get; }
Property Value
- ulong
A number that represents the Unicode value of the character key that was pressed.
Remarks
WARNING
Do not use this property, as it is deprecated. Instead, get the
Unicode value of the character using the Key
property.
Code
The KeyboardEvent.code property represents a physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys.
[Value("code")]
public string Code { get; }
Property Value
- string
The code values for Windows, Linux, and macOS are listed on the KeyboardEvent: code values page.
Remarks
If the input device isn't a physical keyboard, but is instead a virtual keyboard or accessibility device, the returned value will be set by the browser to match as closely as possible to what would happen with a physical keyboard, to maximize compatibility between physical and virtual input devices.
This property is useful when you want to handle keys based on their physical positions on the input device rather than the characters associated with those keys; this is especially common when writing code to handle input for games that simulate a gamepad-like environment using keys on the keyboard. Be aware, however, that you can't use the value reported by KeyboardEvent.code to determine the character generated by the keystroke, because the keycode's name may not match the actual character that's printed on the key or that's generated by the computer when the key is pressed.
For example, the code returned is "KeyQ" for the <kbd>Q</kbd> key on a QWERTY layout keyboard, but the same code value also represents the <kbd>'</kbd> key on Dvorak keyboards and the <kbd>A</kbd> key on AZERTY keyboards. That makes it impossible to use the value of code to determine what the name of the key is to users if they're not using an anticipated keyboard layout.
To determine what character corresponds with the key event, use the Key property instead.
CtrlKey
The KeyboardEvent.ctrlKey read-only property returns a
boolean value that indicates if the <kbd>control</kbd> key was pressed
(true) or not (false) when the event occurred.
[Value("ctrlKey")]
public bool CtrlKey { get; }
Property Value
- bool
A boolean value.
Remarks
IsComposing
The KeyboardEvent.isComposing read-only property returns
a boolean value indicating if the event is fired within a composition
session, i.e., after Elementcompositionstart
and before Elementcompositionend.
[Value("isComposing")]
public bool IsComposing { get; }
Property Value
- bool
A boolean.
Remarks
-Elementcompositionstart and Elementcompositionend
-KeyboardEvent
Key
The KeyboardEvent interface's key read-only property returns the value of the key pressed by the user, taking into consideration the state of modifier keys such as <kbd>Shift</kbd> as well as the keyboard locale and layout.
[Value("key")]
public string Key { get; }
Property Value
- string
A string.Its value is determined as follows:
See a full list of key values.
Remarks
KeyCode
IMPORTANT
DeprecatedKeyboardEvent.keyCode read-only property represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
[Value("keyCode")]
public ulong KeyCode { get; }
Property Value
Remarks
This is usually the decimal ASCII ({{RFC(20)}}) or Windows 1252 code corresponding to the key. If the key can't be identified, this value is 0.
You should avoid using this if possible; it's been deprecated for some time. Instead, you should use Code (for the physical key pressed) or Key (for the character the key maps to). Check compatibility for either property if you target very old browsers.
NOTE
Web developers shouldn't use the
keyCodeattribute for printable characters when handlingkeydownandkeyupevents. As described above, thekeyCodeattribute is not useful for printable characters, especially those input with the <kbd>Shift</kbd> or <kbd>Alt</kbd> key pressed.
Location
The KeyboardEvent.location read-only property returns anunsigned long representing the location of the key on the keyboard or other
input device.
[Value("location")]
public ulong Location { get; }
Property Value
- ulong
A number.
Remarks
MetaKey
The KeyboardEvent.metaKey read-only property returning a
boolean value that indicates if the <kbd>Meta</kbd> key was pressed
(true) or not (false) when the event occurred. Some operating
systems may intercept the key so it is never detected.
[Value("metaKey")]
public bool MetaKey { get; }
Property Value
- bool
A boolean value.
Remarks
NOTE
On Macintosh keyboards, this is the <kbd>⌘ Command</kbd> key.
NOTE
Before Firefox 118, the <kbd>⊞ Windows</kbd> key was handled
as an "OS" key rather than the "Meta" key.KeyboardEvent.metaKeywasfalsewhen the <kbd>⊞ Windows</kbd> key was pressed.
Repeat
The repeat read-only property of the
KeyboardEvent interface returns a boolean value that istrue if the given key is being held down such that it is automatically
repeating.
[Value("repeat")]
public bool Repeat { get; }
Property Value
- bool
A boolean value.
Remarks
ShiftKey
The KeyboardEvent.shiftKey read-only property is a
boolean value that indicates if the <kbd>shift</kbd> key was pressed
(true) or not (false) when the event occurred.
[Value("shiftKey")]
public bool ShiftKey { get; }
Property Value
- bool
A boolean value.
Remarks
The pressing of the shift key may change the KeyboardEventkey of the event too. For example, pressing <kbd>B</kbd> generates key: "b", while simultaneously pressing <kbd>Shift</kbd> generates key: "B".
Methods
GetModifierState(string)
The KeyboardEvent.getModifierState() method returns the
current state of the specified modifier key: true if the modifier is active
(that is the modifier key is pressed or locked), otherwise, false.
[Value("getModifierState")]
public bool GetModifierState(string keyArg)
Parameters
keyArgstring
Returns
- bool
A boolean.
Remarks
-The KeyboardEvent this method belongs to.
-GetModifierState(string)
InitKeyboardEvent(string, bool, bool, Window?, string, ulong, bool, bool, bool, bool)
IMPORTANT
DeprecatedKeyboardEvent.initKeyboardEvent() method initializesthe attributes of a keyboard event object. This method was introduced in draft of DOM
Level 3 Events, but deprecated in newer draft. Gecko won't support this feature since
implementing this method as experimental broke existing web apps (see Firefox bug 999645).
Web applications should use constructor instead of this if it's available.
[Value("initKeyboardEvent")]
public GlobalObject.Undefined InitKeyboardEvent(string typeArg, bool bubblesArg = false, bool cancelableArg = false, Window? viewArg = null, string keyArg = null, ulong locationArg = 0, bool ctrlKey = false, bool altKey = false, bool shiftKey = false, bool metaKey = false)
Parameters
typeArgstringbubblesArgboolcancelableArgboolviewArgWindowkeyArgstringlocationArgulongctrlKeyboolaltKeyboolshiftKeyboolmetaKeybool