Pikkola keycode, tool No. 97

What the browser actually sent.

Press a key and read every field of the event: key, code, keyCode, which, location, the four modifier flags, and whether it repeats. Then take the listener for exactly that combination, with the modifiers you did not hold refused as well as the ones you did.

9
event fields
4
modifier flags
0
keys stored

The switchboard

Press something

Click the pad and type. The pads below it are examples rather than key presses, because a page cannot press a key for you.

Click here, then press a key ?

Off by default, so the page keeps scrolling.

Event fields
Listener for this combination

    
History

Three fields, three different questions

Most keyboard bugs come from using one of them to answer another one's question.

01

key is what the layout says

event.key is the character the key produces, so the same physical key gives a on a US layout and q on a French one. For a shortcut that should follow the layout, key is right; for a game or a text editor that means the position, it is wrong.

02

code is where the key sits

event.code names the physical key, KeyA or Digit1 or ArrowLeft, independent of layout and of the shift state. It is what a game reads, and what a shortcut reads when the shortcut is about a position on the board rather than a character.

03

keyCode is what 2009 needed

keyCode is a number that predates both, differs between browsers for punctuation, and is deprecated but not gone. Old code still branches on it, so this page prints the live value; new code should not add another branch.

What is captured, and what is left alone

A page that listens for every key is a page that can break every key. This one asks first.

Part What this page does What it will not do
Capture Keys pressed while the pad has focus, and every key on the page only when the toggle is on Listen before you click, or keep listening in a field where you are typing
Default action Stopped inside the pad, except Tab, so focus can leave Swallow Tab, or stop the browser's own shortcuts outside the pad
Modifiers All four flags, plus a combo string in the order browsers print Guess the platform, or promise that Meta means Command
IME Reports isComposing so a half-typed Korean or Japanese syllable is visible Interpret a composing key as the shortcut you meant
History The last twelve events, newest first, cleared by one button Keep the history after a reload, or send it anywhere
Storage Nothing: a reload returns to the first example Keep a keystroke, a count, a cookie, or make a request after the page loads

The pad listens for keydown only. keypress is deprecated and does not fire for most keys, and keyup fires for a key that may have been consumed on the way down, which is why a shortcut usually belongs on keydown.

Field guide

key, code, keyCode: three answers to three questions

A keyboard event carries three descriptions of the same key, and each one answers a different question. Picking the wrong one is the most common shortcut bug there is.

key follows the layout, code follows the board

event.key is the character the key produces: "a", "A", "Enter", " " . It changes with the layout and with Shift. event.code is the physical key: KeyA, Digit1, Enter, Space, and it does not change with either. A shortcut meant to be found on the keyboard by its letter uses key; a game that means "the key under the left hand" uses code.

The trap is mixing them: a handler on code === "KeyZ" that then compares key === "z" breaks the moment Shift is held, and a handler on key === "z" stops responding on a QWERTZ keyboard where Z sits where Y sits here. Choose one axis and stay on it.

keyCode is a number with a history

keyCode predates both and was never standardised: it is 65 to 90 for letters, 48 to 57 for digits, 186 to 222 for punctuation with several disagreements between browsers, and 0 or missing for many keys. It is deprecated, it still works, and a lot of code in the world branches on it. New code should use key and code; the live value is printed here so that old code can be checked against the browser it will actually run in.

Modifiers, platform and the truth about Command

Four booleans arrive with every event: ctrlKey, altKey, shiftKey, metaKey. On macOS, Meta is Command. On Windows and Linux, Meta is the Windows or Super key, which browsers often keep for themselves, so a shortcut that should feel native accepts ctrlKey || metaKey and labels itself accordingly. This page prints the flags as they arrived and never guesses the platform.

A modifier pressed on its own also fires a keydown, with key equal to "Shift" and so on. That is why a handler that only looks at key can fire on the modifier before the letter arrives, and why the generated listener checks the exact modifier set instead of "any of these".

location, and the two Enter keys

event.location is 0 for the main block, 1 for the left modifier, 2 for the right, 3 for the numpad. A numpad 5 is code: "Numpad5", key: "5", location: 3, and it fires as a digit even with Num Lock off on some systems, where it fires as Clear. A form that treats numpad Enter as submit usually wants key === "Enter" and not a location check.

Composition, and the shortcut that fires while typing

A Korean or Japanese input method composes a syllable from several key presses before it commits. During composition, event.isComposing is true and key is often "Process". A shortcut handler that ignores this can fire in the middle of a word, which is why real editors check isComposing and often the keyCode === 229 that older browsers send for the same reason.

The same care applies to preventDefault: it stops the browser's default action, not the operating system's. Ctrl+W, Ctrl+T, Cmd+Tab and the Windows key cannot be intercepted from a page, and a page that listens to every key outside a focused field will eventually break a scroll, a back gesture or a screen reader command.

When the key produces a character and the question is which code point it is, Pikkola Unicode shows the code points. When the question is what runs after the handler, Pikkola Event shows the timer and the queue.

Questions

Does this record what I type?

It shows the event while the pad has focus or while the capture toggle is on, and it keeps the last twelve in memory. Nothing is stored, nothing is sent, and a reload empties the list. The page makes one request after it loads, the cookieless page-view count, and keystrokes are never part of it.

Why is keyCode sometimes 0 or undefined?

Because it is deprecated. Some browsers fill it in for compatibility, some fill it only for keys that used to have a code, and a synthetic event may not have it at all. The page shows empty rather than inventing a value. If your code needs a number, event.code is the field to move to.

What is the difference between Meta and Ctrl on a Mac?

On a Mac, Meta is the Command key and Ctrl is Control; on Windows and Linux, Meta is the Windows or Super key. A shortcut that should feel native usually accepts either: check ctrlKey || metaKey and say so in the label. The combo string here prints Meta and Control separately, because that is what the event carries.

Why does a Korean or Japanese key show nothing useful?

Because of the input method. While a syllable is being composed, key events report isComposing true and the key is often Process rather than a letter. That is the correct behaviour and the reason a shortcut handler should ignore events with isComposing, or a shortcut can fire in the middle of typing.

Can I stop the browser's own shortcuts with this?

Not all of them. A page can preventDefault on most keydown events, but some combinations belong to the browser or the operating system and cannot be intercepted: Ctrl+W, Ctrl+T, the Windows key, and on most systems Cmd+Tab. That is a safety property of the platform, not a failure of this page.