07 November 2003

Appendix A: Keyboard events and key identifiers

Philippe Le Hégaret, W3C

Table of contents

This section contains necessary information regarding keyboard events:

Note: This section uses serbian and kanji characters which are not always available (or are misrepresented) in the alternative versions or printed versions of this specification.

A.1 Introduction

Each keyboard event references a key using a DOMString key identifier. The set contained in this appendix is based on the sets of keycodes from:

While implementations are recommended to use the most relevant identifier for a key independently of the platform or keyboard layout mappings, DOM applications should not make assumption on the ability of keyboard devices to generate them. When using keyboard events, "consider using numbers and function keys (F4, F5, and so on) instead of letters in shortcut-key combinations" ([DWW95]) given that most keyboard layouts will provide keys for those.

"U+0000", "U+0001", ..., "U+FFFFFF" are Unicode based key identifiers ([Unicode]). When a key cannot be mapped to Unicode, a specific identifier is used (see also Guidelines for defining key identifiers). In any case, no assumption should be made between the sequence of keyboard events and the text events. The following three examples illustrate the concept of keyboard layout mappings and its relation with keyboard events (following the Guidelines for defining key identifiers, the 'Q' key is mapped to the Latin Capital Letter Q key).

The keystroke "U+0051" (Latin Capital Letter Q key) will produce (on a PC/AT US keyboard using a US keyboard layout mapping and without any modifier activated) the Unicode character q (Latin Small Letter Q):

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0051" (Latin Capital Letter Q key)
  2. {"http://www.w3.org/2001/xml-events", "textInput"}: "q"
  3. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0051"

If the keyboard layout mapping is switched to a french mapping, pressing the same key will produce:

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0041" (Latin Capital Letter A key)
  2. {"http://www.w3.org/2001/xml-events", "textInput"}: "a"
  3. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0041"

If the keyboard layout mapping is switched to a serbian (cyrillic) mapping, pressing the same key will produce:

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0409" (Cyrillic Capital Letter LJE)
  2. {"http://www.w3.org/2001/xml-events", "textInput"}: "љ"
  3. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0409"

Note: The order between the text event and keyboard events may differ depending on the keyboard devices.

A.1.1 Modifier keys

Keyboard input uses modifier keys to change the normal behavior of a key. Keys associated with modifiers generate, like other keys, "keydown" and "keyup" events as shown in the example below. Some modifiers are activated while the key is being pressed down or maintained pressed such as "Alt", "Control", "Shift", "AltGraph", or "Meta". Others modifiers are activated depending on their state such as "CapsLock", "NumLock", or "Scroll". Change in the state happens when the modifier key is being pressed down. The KeyboardEvent interface provides convenient attributes for some common modifiers keys: KeyboardEvent.ctrlKey, KeyboardEvent.shiftKey, KeyboardEvent.altKey, KeyboardEvent.metaKey. Some operating systems simulate the "AltGraph" modifier key with the combination of the "Alt and "Control" modifier keys. Implementations are encouraged to use the "AltGraph" modifier key.

The following example describes a possible sequence of keys to generate the Unicode character Q (Latin Capital Letter Q) on a PC/AT US keyboard using a US mapping:

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "Shift", shiftKey
  2. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0051" (Latin Capital Letter Q key), shiftKey
  3. {"http://www.w3.org/2001/xml-events", "textInput"}: "Q"
  4. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0051", shiftKey
  5. {"http://www.w3.org/2001/xml-events", "keyup"}: "Shift"

The following example describes a possible sequence of keys that does not generate a Unicode character (using the same configuration):

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "Control", ctrlKey
  2. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0056" (Latin Capital Letter V key), ctrlKey
  3. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0056", ctrlKey
  4. {"http://www.w3.org/2001/xml-events", "keyup"}: "Control"

Preventing a keydown or keyup event with a modifier does affect the subsequent

A.1.2 Dead keys

Keyboard input uses dead keys for the input of composed character sequences. Unlike the handwriting sequence, in which users enter the base character first, keyboard input requires to enter a special state when a dead key is pressed and emit the character(s) only when one of a limited number of "legal" base character is entered.

The dead keys are represented in the key identifiers set using combining diacritical marks. The sequence of keystrokes "U+0302" (Combining Circumflex Accent key) and "U+0045" (Latin Capital Letter E key) will likely produce (on a PC/AT french keyboard using a french mapping and without any modifier activated) the Unicode character ê (Latin Small Letter E With Circumflex), as preferred by the Unicode Normalization Form NFC:

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0302" (Combining Circumflex Accent key)
  2. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0302"
  3. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0045" (Latin Capital Letter E key)
  4. {"http://www.w3.org/2001/xml-events", "textInput"}: "é"
  5. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0045"

A.1.3 Input Method Editors

Also known as front end processor, an input method editor (IME) is an application that performs the conversion between keystrokes and ideographs or other characters, usually by user-guided dictionary lookup.

This specification does not provide a representation of the input method editor (IME) events, e.g. representing the input context, i.e. the IME's functions are not represented in this set. As an example, receiving a keydown for the "Accept" key identifier does not necessarily imply that the text currently selected in the IME is being accepted. It only indicates that a keystroke happened, disconnected from the IME Accept functionality. Depending on the device in use, the IME Accept functionality can be obtain using the Accept key or the Return key. Keyboard events cannot be used to determine the current state of the input method editor.

Keyboard events correspond to the events generated by the input device after the keyboard layout mapping but before the processing of the input method editor.

The following example describes a possible sequence of keys to generate the Unicode character 市 (Kanji character, part of CJK Unified Ideographs) using Japanese input methods. This example assumes that the input method editor is activated and in the Japanese-Romaji input mode. The keys "Convert" and "Accept" may be replaced by others depending on the input device in use and the configuration of the IME, e.g. it could be respectively "U+0020" (Space key) and "Enter".

  1. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0053" (Latin Capital Letter S key)
  2. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0053" (Latin Capital Letter S key)
  3. {"http://www.w3.org/2001/xml-events", "keydown"}: "U+0049" (Latin Capital Letter I key)
  4. {"http://www.w3.org/2001/xml-events", "keyup"}: "U+0049" (Latin Capital Letter I key)
  5. {"http://www.w3.org/2001/xml-events", "keydown"}: "Convert"
  6. {"http://www.w3.org/2001/xml-events", "keyup"}: "Convert"
  7. {"http://www.w3.org/2001/xml-events", "keydown"}: "Accept"
  8. {"http://www.w3.org/2001/xml-events", "textInput"}: "市"
  9. {"http://www.w3.org/2001/xml-events", "keyup"}: "Accept"

A.1.4 Guidelines for defining key identifiers

Note: This section is informative.

The list of key identifiers contained in this appendix is not exhaustive and input devices may have to define their own key identifiers. Here is a algorithm to determine which key identifier to use:

  1. Determine a representation for the key by looking at the keyboard layout mapping in use (and not the keyboard device in use). This representation should be unique, as human friendly as possible, platform independent, and consistent. For example, on PC/AT US keyboards with a US mapping, the 'Q' key is mapped to the key identifier "U+0051" (Latin Capital Letter Q key), the '1/!' key is mapped to the key identifier "U+0031" (Digit One key), the key '`/~' is mapped to the key identifier "U+0060" (Grave Accent key), and the 'Enter' key is mapped to the key identifier "Enter".
  2. Find an appropriate mapping in the Unicode character set. There might not always be an appropriate and obvious mapping: the Unicode set contains characters and symbols, the key might generate different characters depending on the operating system, ... In general, unless the representation of the key can be mapped to a unique Unicode character, it is better to create a new one.
  3. If no appropriate mapping was found, create a key identifier as human friendly as possible. The key identifier must not contain white spaces. As an example, the Enter key is mapped to the key identifier "Enter" and not to "U+000A" (Line Feed), given that this key generates the character 00000A on Unix operating systems and the characters 00000D and 00000A on Windows operating systems.

A.2 Key identifiers set

Note: The keycodes Multiply, Add, Substract, Decimal, Separator, Divide, NumPad0, NumPad1, NumPad2, NumPad3, NumPad4, NumPad5, NumPad6, NumPad7, NumPad8, and NumPad9 are not part of this set. Use KeyBoard.keyLocation to know if a key originated from the numeric keypad.

The Accept (Commit) key.
The Again key.
The All Candidates key.
The Alphanumeric key.
The Alt (Menu) key.
The Alt-Graph key.
The Application key.
The ATTN key.
The Browser Back key.
The Browser Favorites key.
The Browser Forward key.
The Browser Home key.
The Browser Refresh key.
The Browser Search key.
The Browser Stop key.
The Caps Lock (Capital) key.
The Clear key.
The Code Input key.
The Compose key.
The Control (Ctrl) key.
The Crsel key.
The Convert key.
The Copy key.
The Cut key.
The Down Arrow key.
The End key.
The Enter key.

Note: This key identifier is also used for the Return (Macintosh numpad) key.

The Erase EOF key.
The Execute key.
The Exsel key.
The F1 key.
The F2 key.
The F3 key.
The F4 key.
The F5 key.
The F6 key.
The F7 key.
The F8 key.
The F9 key.
The F10 key.
The F11 key.
The F12 key.
The F13 key.
The F14 key.
The F15 key.
The F16 key.
The F17 key.
The F18 key.
The F19 key.
The F20 key.
The F21 key.
The F22 key.
The F23 key.
The F24 key.
The Final Mode (Final) key used on some asian keyboards.
The Find key.
The Full-Width Characters key.
The Half-Width Characters key.
The Hangul (Korean characters) Mode key.
The Hanja (Korean characters) Mode key.
The Help key.
The Hiragana (Japanese Kana characters) key.
The Home key.
The Insert (Ins) key.
The Japanese-Hiragana key.
The Japanese-Katakana key.
The Japanese-Romaji key.
The Junja Mode key.
The Kana Mode (Kana Lock) key.
The Kanji (Japanese name for ideographic characters of Chinese origin) Mode key.
The Katakana (Japanese Kana characters) key.
The Start Application One key.
The Start Application Two key.
The Start Mail key.
The Left Arrow key.
The Meta key.
The Media Next Track key.
The Media Play Pause key.
The Media Previous Track key.
The Media Stok key.
The Mode Change key.
The Nonconvert (Don't Convert) key.
The Num Lock key.
The Page Down (Next) key.
The Page Up key.
The Paste key.
The Pause key.
The Play key.
The Previous Candidate function key.
The Print Screen (PrintScrn, SnapShot) key.
The Process key.
The Props key.
The Right Arrow key.
The Roman Characters function key.
The Scroll Lock key.
The Select key.
The Select Media key.
The Shift key.
The Stop key.
The Up Arrow key.
The Undo key.
The Volume Down key.
The Volume Mute key.
The Volume Up key.
The Windows Logo key.
The Zoom key.
The Backspace (Back) key.
The Horizontal Tabulation (Tab) key.
The Cancel key.
The Escape (Esc) key.
The Space (Spacebar) key.
The Exclamation Mark (Factorial, Bang) key (!).
The Quotation Mark (Quote Double) key (").
The Number Sign (Pound Sign, Hash, Crosshatch, Octothorpe) key (#).
The Dollar Sign (milreis, escudo) key ($).
The Ampersand key (&).
The Apostrophe (Apostrophe-Quote, APL Quote) key (').
The Left Parenthesis (Opening Parenthesis) key (().
The Right Parenthesis (Closing Parenthesis) key ()).
The Asterix (Star) key (*).
The Plus Sign (Plus) key (+).
The Comma (decimal separator) sign key (,).
The Hyphen-minus (hyphen or minus sign) key (-).
The Full Stop (period, dot, decimal point) key (.).
The Solidus (slash, virgule, shilling) key (/).
The Digit Zero key (0).
The Digit One key (1).
The Digit Two key (2).
The Digit Three key (3).
The Digit Four key (4).
The Digit Five key (5).
The Digit Six key (6).
The Digit Seven key (7).
The Digit Eight key (8).
The Digit Nine key (9).
The Colon key (:).
The Semicolon key (;).
The Less-Than Sign key (<).
The Equals Sign key (=).
The Greater-Than Sign key (>).
The Question Mark key (?).
The Commercial At (@) key.
The Latin Capital Letter A key (A).
The Latin Capital Letter B key (B).
The Latin Capital Letter C key (C).
The Latin Capital Letter D key (D).
The Latin Capital Letter E key (E).
The Latin Capital Letter F key (F).
The Latin Capital Letter G key (G).
The Latin Capital Letter H key (H).
The Latin Capital Letter I key (I).
The Latin Capital Letter J key (J).
The Latin Capital Letter K key (K).
The Latin Capital Letter L key (L).
The Latin Capital Letter M key (M).
The Latin Capital Letter N key (N).
The Latin Capital Letter O key (O).
The Latin Capital Letter P key (P).
The Latin Capital Letter Q key (Q).
The Latin Capital Letter R key (R).
The Latin Capital Letter S key (S).
The Latin Capital Letter T key (T).
The Latin Capital Letter U key (U).
The Latin Capital Letter V key (V).
The Latin Capital Letter W key (W).
The Latin Capital Letter X key (X).
The Latin Capital Letter Y key (Y).
The Latin Capital Letter Z key (Z).
The Left Square Bracket (Opening Square Bracket) key ([).
The Reverse Solidus (Backslash) key (\).
The Right Square Bracket (Closing Square Bracket) key (]).
The Circumflex Accent key (^).
The Low Sign (Spacing Underscore, Underscore) key (_).
The Grave Accent (Back Quote) key (`).
The Left Curly Bracket (Opening Curly Bracket, Opening Brace, Brace Left) key ({).
The Vertical Line (Vertical Bar, Pipe) key (|).
The Right Curly Bracket (Closing Curly Bracket, Closing Brace, Brace Right) key (}).
The Delete (Del) Key.
The Inverted Exclamation Mark key (¡).
The Combining Grave Accent (Greek Varia, Dead Grave) key.
The Combining Acute Accent (Stress Mark, Greek Oxia, Tonos, Dead Eacute) key.
The Combining Circumflex Accent (Hat, Dead Circumflex) key.
The Combining Tilde (Dead Tilde) key.
The Combining Macron (Long, Dead Macron) key.
The Combining Breve (Short, Dead Breve) key.
The Combining Dot Above (Derivative, Dead Above Dot) key.
The Combining Diaeresis (Double Dot Abode, Umlaut, Greek Dialytika, Double Derivative, Dead Diaeresis) key.
The Combining Ring Above (Dead Above Ring) key.
The Combining Double Acute Accent (Dead Doubleacute) key.
The Combining Caron (Hacek, V Above, Dead Caron) key.
The Combining Cedilla (Dead Cedilla) key.
The Combining Ogonek (Nasal Hook, Dead Ogonek) key.
The Combining Greek Ypogegrammeni (Greek Non-Spacing Iota Below, Iota Subscript, Dead Iota) key.
The Euro Currency Sign key (€).
The Combining Katakana-Hiragana Voiced Sound Mark (Dead Voiced Sound) key.
The Combining Katakana-Hiragana Semi-Voiced Sound Mark (Dead Semivoiced Sound) key.