Keyboard Ghosting & N-Key Rollover (NKRO): Matrix Scanning & Anti-Ghosting Guide
Keyboard ghosting occurs in row-column switch matrices when three pressed keys form three corners of a rectangle in the grid, causing electrical current to back-feed into an unpressed switch and registering a false "ghost" fourth key. Membrane keyboards prevent ghosting via "jamming" (blocking additional simultaneous presses), while mechanical keyboards implement individual isolation diodes on each switch to achieve true N-Key Rollover (NKRO), allowing all 104+ keys to register simultaneously over USB HID.
Pressed
Pressed
Pressed
Ghost Blocked
Step 1: Switch Matrix Architecture: Rows, Columns, and Sneak Paths
Keyboards do not connect 104 separate wires to the microcontroller; instead, switches are arranged in a grid of rows and columns (e.g. 6 rows × 18 columns = 108 intersections). The microcontroller strobes electrical pulses along rows and samples columns. When three keys sharing row and column intersections are pressed simultaneously, current travels backward through the closed circuits—creating a "sneak path" that falsely triggers the fourth switch at the fourth intersection.
<!-- Electrical Matrix Sneak Path Problem -->
Row 1 ----[Switch A (Closed)]----+----> Column 1
| |
Row 2 ----[Switch B (Closed)]----+----> Column 2
| |
[Switch C (Closed)] [Switch D (OPEN, BUT SNEAK PATH SENSES CLOSED!)]
Step 2: Anti-Ghosting Diodes: How Mechanical Keyboards Solve Sneak Paths
High-end mechanical and gaming keyboards place a fast-switching diode (such as a surface-mount 1N4148) in series with every single key switch. Diodes allow current to travel in only one direction, physically blocking sneak currents from back-feeding into adjacent columns. This eliminates electrical ghosting entirely at the hardware PCB layer.
/* Hardware Solution: 1 Diode per Switch */
Row [n] --->|--- [Switch] ---> Column [k]
- Diode conducts forward current during scan stroke
- Diode blocks reverse reverse current from intersecting active rows
- Allows infinite simultaneous switch actuations (Full NKRO)
Step 3: USB HID Protocol Limitations: 6KRO vs True NKRO Descriptors
Even if the keyboard PCB has diodes on every key, standard USB HID specifications limit simultaneous reports. The standard USB Boot Protocol report descriptor is 8 bytes: 1 byte for modifier keys (Shift, Ctrl, Alt, GUI), 1 reserved byte, and exactly 6 bytes for keycodes (6-Key Rollover / 6KRO). Modern gaming keyboards bypass this by declaring a custom USB HID report with a full bitfield bitmap (104+ bits) or multiplexing multiple virtual HID keyboard instances.
// Standard 8-Byte USB HID Report (6KRO)
struct standard_keyboard_report {
uint8_t modifiers; // Bit 0: LCtrl, Bit 1: LShift, etc.
uint8_t reserved; // Vendor byte
uint8_t keycodes[6]; // Up to 6 simultaneous non-modifier keys
};
// True NKRO Bitfield Report (128-Key Rollover)
struct nkro_keyboard_report {
uint8_t report_id;
uint8_t key_bitmask[16]; // 16 bytes * 8 bits = 128 simultaneous keys!
};
Step 4: Detecting Multi-Key Rollover in JavaScript
In web applications and browser games, track active keys by storing e.code into a Set on keydown and removing them on keyup. Always use e.code (physical key position) rather than e.key (which changes with Shift or keyboard layout modifications) to evaluate hardware rollover.
const activeKeys = new Set();
window.addEventListener('keydown', (e) => {
// Ignore automatic OS key-repeat events
if (e.repeat) return;
activeKeys.add(e.code);
console.log(`Simultaneous keys held: ${activeKeys.size}`, Array.from(activeKeys));
});
window.addEventListener('keyup', (e) => {
activeKeys.delete(e.code);
});
// Clear active keys when window loses focus
window.addEventListener('blur', () => {
activeKeys.clear();
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the difference between keyboard ghosting and keyboard jamming?
Ghosting occurs when pressing three keys causes a non-pressed fourth key to mistakenly fire. Jamming (blocking) is the firmware defense mechanism on inexpensive keyboards that purposefully ignores additional key presses when a potential ghosting matrix intersection is detected.
Why do cheap office keyboards fail when pressing Shift + W + Space simultaneously?
Membrane keyboards group commonly used keys into shared matrix traces without isolation diodes. When gaming keys (like W, A, S, D, Shift, Space) share common row/column lines, the keyboard controller jams the input to avoid ghosting, failing to register jumping while sprinting.
How does true NKRO bypass the standard 6-key USB HID limit?
Standard USB keyboards adhere to the USB HID Boot Protocol report (6 key bytes). True NKRO keyboards either present a custom HID report descriptor containing a 16-to-32-byte bitmask (each bit representing one of 128+ physical keys) or expose multiple virtual USB keyboard endpoints simultaneously.
Can browser JavaScript detect more than 6 simultaneous key presses?
Yes! The browser receives key events directly from the OS HID driver. If the hardware keyboard supports NKRO, JavaScript will fire `keydown` events for 10, 20, or even all 100+ keys simultaneously without dropping any.
What is the difference between e.key and e.code in multi-key rollover tracking?
`e.code` represents the physical switch location on the keyboard matrix (e.g. `KeyW`, `Digit1`, `ShiftLeft`) regardless of language layout. `e.key` represents the localized character output (e.g. "W", "w", or "Z" on AZERTY layouts). Matrix diagnostics should always rely on `e.code`.