"Scan code" is not a single standardized word but a technical term used in computing to refer to the unique identifier generated by a keyboard when a key is pressed. It is a standard term in low-level system programming and hardware interaction, not slang or informal language. Scan codes originate from the physical keyboard’s hardware design and are sent to the operating system before being translated into characters by the keyboard driver. They are essential for distinguishing between different keys—even those that produce the same character—such as left and right Shift keys.
What Is a Scan Code?
A scan code is a numeric value (usually hexadecimal) that represents a specific physical key on a keyboard. When a key is pressed, the keyboard controller sends a scan code to the computer’s central processing unit (CPU) or input controller. This happens before the operating system or application processes the keypress. For example, pressing the ‘A’ key on a U.S. QWERTY keyboard produces scan code 0x1E (hexadecimal 1E), regardless of whether Caps Lock is on or whether Shift is held. The operating system then uses its keyboard layout and driver settings to map that scan code to a character (e.g., ‘a’ or ‘A’) and sends the result to the active application.
The scan code is hardware-agnostic in concept but varies by keyboard layout and manufacturer. IBM PC-compatible keyboards use a standard set known as “set 1” scan codes, while older PS/2 systems used “set 2” and “set 3” in some configurations. Modern USB keyboards typically translate PS/2 scan codes into USB HID (Human Interface Device) usage IDs, but the low-level concept remains similar.
How Scan Codes Work
When a key is pressed or released, the keyboard hardware generates two scan codes: one for the key press (make code) and one for the key release (break code). For most keys, the break code is the make code with the most significant bit set (e.g., make code 0x1E becomes break code 0x9E). The operating system’s keyboard driver interprets these codes and updates the internal keyboard state (e.g., tracking which modifier keys are held).
The translation chain is:
- Physical key press → keyboard controller sends scan code
- Operating system receives raw scan code via interrupt or polling
- Keyboard driver maps scan code to virtual key code (e.g., VK_SHIFT, VK_A)
- Application receives virtual key code and/or Unicode character
This layered approach allows the same physical keyboard to support multiple languages and layouts. For instance, the same scan code (0x2C for the ‘Z’ key on a U.S. keyboard) maps to ‘Z’ in U.S. English but to ‘Y’ in French AZERTY layouts.
Common Use Cases
Scan codes are rarely visible to end users but are critical in several technical contexts:
- Key remapping tools: Programs like AutoHotkey (Windows), xmodmap (Linux), or Karabiner-Elements (macOS) use scan codes to override default behavior at a low level, ensuring remaps work even when modifier keys or layout changes interfere.
- Bootloading and firmware: Operating system installers and BIOS/UEFI utilities often rely on raw scan codes because high-level drivers (e.g., for Unicode or multi-language support) aren’t yet loaded.
- Accessibility and gaming: Some specialized input devices (e.g., foot pedals, custom gamepads) report scan codes directly for precise timing and minimal latency.
- Security and forensics: Low-level keyloggers capture scan codes to bypass encryption or input method editors (IMEs) that mask keystrokes at higher levels.
How to View or Test Scan Codes
Direct scan code access requires third-party tools or developer utilities, as modern operating systems abstract them away. Here are verified methods:
- Windows: Use the free tool Keyboard Scanner (by NirSoft) or Microsoft PowerToys (Keyboard Manager module). In PowerShell, run
Get-Content -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layouts"to see layout mappings, but raw scan codes require hardware-level tools likeWinObjor debuggers. - Linux: Run
showkey -sin a virtual console (Ctrl+Alt+F2) to see scan codes in octal or hex. Note: This does not work in a graphical desktop session—switch to a TTY first. - macOS: Use
hidutilin Terminal for remapping, but raw scan codes require developer tools likeIORegistryExploreror third-party apps like Event Viewer (from the macOS Developer Tools). - Web browsers: JavaScript’s
KeyboardEvent.codeproperty returns physical key identifiers (e.g.,KeyA,ShiftLeft), which are based on USB HID usage codes, not raw scan codes—but they serve a similar purpose for web apps.
Important note: On USB keyboards, scan codes are not transmitted directly; instead, the keyboard controller converts them to USB HID usage IDs. Tools reporting “scan codes” often display these usage IDs as proxies.
Common Problems and Misconceptions
Several myths and issues surround scan codes:
- Misconception: “Scan codes = key codes”. False. Key codes (e.g., Windows virtual key codes like VK_A) are OS-level abstractions. Scan codes are hardware-specific and predate OS interpretation.
- Problem: Non-standard keyboards. Some gaming or multimedia keyboards send extra scan codes for proprietary features (e.g., macro keys), which may not map correctly in standard tools. Check the manufacturer’s SDK for documentation.
- Problem: Layout mismatches. If your keyboard layout is set to QWERTY but you’re using a DVORAK physical keyboard, scan codes remain unchanged—but the OS maps them incorrectly. Always match layout settings to physical hardware.
- Problem: Break codes ignored. Applications that don’t handle key release events (e.g., some web games) may misinterpret repeated keypresses or fail to register held keys correctly.
Comparison: Scan Codes vs. Virtual Key Codes vs. Unicode
Understanding the differences helps clarify why scan codes matter:
- Scan codes: Hardware-generated, key-physical-location based. Example: 0x1E for the ‘A’ key.
- Virtual key codes (VK): OS-level identifiers. Windows uses decimal/hex VK codes (e.g., VK_A = 0x41). They’re layout-independent but not hardware-specific.
- Unicode characters: Final output (e.g., ‘a’ = U+0061). Affected by modifiers, layout, and IME state.
For example, pressing ‘A’ with Caps Lock on produces scan code 0x1E, virtual key VK_A (0x41), and Unicode ‘A’ (U+0041). Pressing ‘A’ with Shift on a U.S. keyboard produces the same scan code and virtual key, but Unicode ‘A’. This shows why scan codes are foundational but not sufficient alone.
Alternatives and Modern Replacements
While scan codes persist in low-level systems, newer standards have emerged:
- USB HID Usage Pages: USB keyboards use usage IDs (e.g., 0x04 for ‘A’) defined in the USB HID specification. These are standardized across vendors and include modifiers, media keys, and gamepad buttons.
- Linux input subsystem: Uses
EV_KEYevents withKEY_constants (e.g., KEY_A) based on Linux’s internal keymap. - Android/HID: Mobile devices often bypass scan codes entirely, using touch or gesture APIs. Physical keyboards on Android report HID usage IDs via the
KeyEventclass. - Web APIs: Modern browsers use
KeyboardEvent.code(physical location) andKeyboardEvent.key(logical key value), abstracting away scan codes for security and portability.
These alternatives improve cross-platform compatibility but don’t eliminate the need for scan codes in embedded systems, drivers, or forensic tools.
Is It Safe to Work With Scan Codes?
Yes, with caveats. Reading scan codes is safe and read-only. However, manipulating them (e.g., remapping keys at the driver level) carries risks:
- System instability: Incorrect remapping in kernel-mode tools can cause unresponsive keyboards or boot failures.
- Security risks: Low-level keyloggers using scan codes can bypass antivirus if they hook into the OS kernel. Use only trusted tools from reputable developers.
- Accessibility trade-offs: Remapping critical keys (e.g., Escape or Ctrl) may interfere with emergency shortcuts in games or productivity software.
Best practice: Test remaps in a non-production environment first. On Windows, prefer user-mode tools (e.g., PowerToys) over registry edits or driver-level changes unless absolutely necessary.
Frequently Asked Questions
Are scan codes the same across all keyboards?
No. While PC-compatible keyboards largely follow IBM’s “set 1” scan codes, proprietary or gaming keyboards may use custom codes for extra features. USB keyboards translate internal scan codes to USB HID usage IDs, which are standardized—but the original scan code set varies by manufacturer.
Can I see scan codes in a web browser?
No. Browsers hide scan codes for security reasons. Instead, they expose KeyboardEvent.code, which reflects physical key position (e.g., “KeyA” for the top-left letter key), but this is not a raw scan code. You cannot access hardware-level scan codes via JavaScript.
Do laptop keyboards use different scan codes?
Yes, due to space constraints. Laptops often use a compressed scan code set or share scan codes across keys (e.g., Fn key combinations generate different codes). Some laptops (e.g., ThinkPads) document scan codes in developer guides, but full compatibility with desktop standards isn’t guaranteed.
Why do some programs ignore my key remaps?
Games and security-sensitive apps (e.g., password managers) often bypass the OS’s keyboard driver and read hardware directly using raw input APIs. If your remap tool only hooks at the user-mode level (e.g., Windows message queue), it won’t affect these programs. Use tools that support raw input (e.g., AutoHotkey v2 with InputHook) for broader coverage.
Do mobile devices use scan codes?
Not in the traditional sense. On Android and iOS, physical keyboards (via Bluetooth or USB OTG) report HID usage IDs. Touch input uses entirely different APIs (e.g., MotionEvent on Android). Scan codes are irrelevant for on-screen keyboards.
How do I find scan codes for my specific keyboard?
Use a dedicated tool like Keyboard Scanner (Windows), showkey -s (Linux), or Event Viewer (macOS). Alternatively, consult the keyboard’s technical reference manual—if available—under sections like “Scan Code Set” or “HID Report Descriptors.” For generic keyboards, assume USB HID usage IDs (e.g., 0x07 for ‘A’) unless specified otherwise.