code property
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.
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 Q key on a
QWERTY layout keyboard, but the same code
value also represents the
' key on Dvorak keyboards and the A 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 KeyboardEvent.key property instead.
Implementation
external String get code;