Table of Contents

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

KeyboardEvent events 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.

-Code.
-Key.
-GetModifierState(string)

See also on MDN

Constructors

KeyboardEvent()

public KeyboardEvent()

KeyboardEvent(string, KeyboardEventInit)

The KeyboardEvent() constructor creates a new
KeyboardEvent object.

public KeyboardEvent(string type, KeyboardEventInit eventInitDict = null)

Parameters

type string
eventInitDict KeyboardEventInit

Remarks

-KeyboardEvent, the interface of the objects it constructs.

See also on MDN

Fields

DOM_KEY_LOCATION_LEFT

[Value("DOM_KEY_LOCATION_LEFT")]
public const ulong DOM_KEY_LOCATION_LEFT = 1

Field Value

ulong

DOM_KEY_LOCATION_NUMPAD

[Value("DOM_KEY_LOCATION_NUMPAD")]
public const ulong DOM_KEY_LOCATION_NUMPAD = 3

Field Value

ulong

DOM_KEY_LOCATION_RIGHT

[Value("DOM_KEY_LOCATION_RIGHT")]
public const ulong DOM_KEY_LOCATION_RIGHT = 2

Field Value

ulong

DOM_KEY_LOCATION_STANDARD

[Value("DOM_KEY_LOCATION_STANDARD")]
public const ulong DOM_KEY_LOCATION_STANDARD = 0

Field Value

ulong

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
Deprecated
The charCode read-only property of the
KeyboardEvent 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.

See also on MDN

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.

See also on MDN

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

See also on MDN

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
Deprecated
The deprecated KeyboardEvent.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

ulong

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 keyCode attribute for printable characters when handling keydown and keyup events. As described above, the keyCode attribute is not useful for printable characters, especially those input with the <kbd>Shift</kbd> or <kbd>Alt</kbd> key pressed.

See also on MDN

Location

The KeyboardEvent.location read-only property returns an
unsigned 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

Possible values are:

-KeyboardEvent

See also on MDN

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.metaKey was
false when the <kbd>⊞ Windows</kbd> key was pressed.

-KeyboardEvent

See also on MDN

Repeat

The repeat read-only property of the
KeyboardEvent interface returns a boolean value that is
true 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".

-KeyboardEvent

See also on MDN

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

keyArg string

Returns

bool

A boolean.

Remarks

InitKeyboardEvent(string, bool, bool, Window?, string, ulong, bool, bool, bool, bool)

IMPORTANT
Deprecated
The KeyboardEvent.initKeyboardEvent() method initializes
the 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

typeArg string
bubblesArg bool
cancelableArg bool
viewArg Window
keyArg string
locationArg ulong
ctrlKey bool
altKey bool
shiftKey bool
metaKey bool

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks