Click in the box below and press any key to inspect its properties.
Modern web applications rely heavily on keyboard input for commands,
navigation, accessibility shortcuts, and text entry. When a user
presses a key, browsers fire keyboard events that contain several
pieces of information. The most widely used properties today are
event.key, which represents the character or action
produced, and event.code, which corresponds to the
physical key position on the keyboard. Older code often references the
numeric event.keyCode, though this is considered
deprecated. This utility displays all three values simultaneously so
developers can understand how different browsers and layouts report
key presses.
Consider the relationship between character codes and their numeric
representations. Characters in computing are ultimately represented as
numbers. For example, the letter A corresponds to the
decimal value in the ASCII table. Conversion
between characters and their numeric codes can be described
mathematically as
, where
is the numeric value of each byte in a multi-byte character. While
JavaScript handles these conversions internally with functions like
charCodeAt, the formula demonstrates how textual data
maps to numerical codes.
When the user presses a key in the focusable display area, the script
captures the keydown event. The resulting output shows
the value of event.key, which may be a single character
such as “a,” a word like “Enter,” or a description like “ArrowUp.” The
event.code property refers to the physical key identifier
such as “KeyA” or “ArrowLeft,” remaining constant regardless of
keyboard layout. Legacy code often relied on keyCode,
which returns a number like 65 for “a.” Although modern development
favors key and code, understanding
keyCode helps when maintaining older projects.
Keyboard handling has evolved across browser generations. Early
implementations were inconsistent, with some browsers using
which, others using keyCode, and few
offering access to the actual character produced. The standardization
process through the W3C DOM Level 3 Events specification introduced
event.key and event.code to provide
cross-platform consistency. The key property represents
the meaning of the key press, taking into account modifiers like Shift
or Alt, while code reflects the physical key location
independent of modifiers. This distinction is vital for
internationalization: on a French AZERTY keyboard, pressing the key
labeled “A” produces “Q,” yet the code remains “KeyA” while the key
value is “a” or “A.”
Developers often need to react to specific keys. For instance, gaming
applications detect arrow keys for movement, while productivity
software might map shortcuts like Ctrl+S for saving. Knowing the
difference between key and code ensures
reliable behavior across layouts. If an application expects the “WASD”
keys for movement, checking code ensures that the
physical keys are used regardless of letter arrangement. Conversely,
text editors care about the resulting character and therefore rely on
key.
The viewer below lists a handful of common key values and their typical codes. It serves as a quick reference and demonstrates how this information can be presented in a structured format.
| Key | Code | KeyCode | Description |
|---|---|---|---|
| A | KeyA | 65 | Letter A on most layouts |
| Enter | Enter | 13 | Return or newline key |
| Space | Space | 32 | Spacebar |
| ArrowUp | ArrowUp | 38 | Up arrow navigation key |
| Escape | Escape | 27 | Escape or cancel key |
Behind the scenes, the script adds an event listener to the display
element. Every time a key is pressed, the listener updates the box
with the latest values. To prevent the page from scrolling when the
spacebar or arrow keys are pressed,
event.preventDefault() is invoked. This ensures the focus
remains on the viewer, allowing rapid experimentation without
unintended side effects. Because the tool runs entirely on the client
side, it poses no security risk and can be saved for offline use.
Understanding keyboard events aids accessibility. Assistive technologies such as screen readers rely on predictable keyboard interactions, and web developers are encouraged to design interfaces that are fully navigable without a mouse. By inspecting the properties of various key presses, developers can craft shortcuts that do not conflict with common accessibility conventions. For example, screen readers often reserve combinations like Alt+Ctrl+Arrow for navigation, so testing with this viewer helps ensure compatibility.
The mathematics of key repeat rates also plays a role. Operating
systems generate repeated keydown events when a key is
held down, typically after a delay followed by
a repeat interval . The effective number of events in time is
approximately
for . Understanding this behavior helps when implementing features like
key-based movement or continuous scrolling. It also informs debouncing
strategies to avoid overwhelming the application with rapid events.
Keyboard layouts vary widely across languages. While QWERTY is common
in English-speaking countries, many nations use QWERTZ, AZERTY, or
other layouts. Some languages incorporate additional characters like
umlauts or cedillas, and others use completely different scripts such
as Cyrillic or Arabic. Because event.code reflects
physical keys, it remains constant even as
event.key changes. This viewer allows users to experiment
by switching system layouts and observing how the reported values
adapt, making it a valuable learning tool for internationalization.
Finally, the historical context of keyboard codes sheds light on their
quirks. The earliest personal computers mapped keys to specific
numeric values based on hardware design. As standards evolved, these
codes were preserved for backward compatibility, leading to the
somewhat arbitrary keyCode values seen today. While
modern APIs offer more expressive properties, legacy systems still
rely on the old numbers. This viewer exposes both modern and legacy
metrics, bridging the gap between past and present.
Event propagation is another important concept. Keyboard events bubble
from the focused element up through ancestors, allowing global
shortcuts when listeners are placed on document or window. Developers
can stop propagation using event.stopPropagation(). This
viewer attaches its listener directly to the focusable div so that
bubbling can be observed separately if desired.
Security considerations arise with keylogging concerns. Because this page runs entirely locally and does not transmit data, it offers a safe environment for experimentation. In real applications, developers should avoid storing or transmitting raw keystrokes unless necessary, and users should be cautious about granting keyboard permissions in embedded contexts.
Combining keys introduces modifier states. The event object exposes
boolean properties such as shiftKey,
ctrlKey, altKey, and metaKey.
When multiple keys are pressed, the reported key may differ; for
example, holding Shift while pressing “a” yields key “A” but code
“KeyA.” The viewer can be extended to show these flags, illustrating
how shortcuts are detected programmatically.
Finally, because all logic is encapsulated in a single HTML file, educators can easily distribute it to students learning JavaScript. The script demonstrates event handling, DOM manipulation, and real-time UI updates in fewer than fifty lines of code, yet the underlying explanation spans topics from binary representations to international usability. This blend of simplicity and depth typifies the philosophy of the broader utility collection.
Use the copy button to transfer the last key event into bug reports or
documentation. Having the exact key, code,
and keyCode values handy helps teams reproduce issues
across browsers and keyboard layouts.