Try to answer before you open the answers. Guessing is fine; checking your guess is how you learn.
QUICK REVIEW (5 minutes)
- Why are the keys of a keyboard wired in a matrix?
- What is debouncing, and what would happen without it?
- What is the difference between a make code and a break code?
- Who turns a scan code into the letter "A": the keyboard or the OS?
- How does USB get a keyboard's report: does the keyboard send it by itself, or does the PC ask?
- What does the common boot-protocol keyboard report contain (how many bytes, and what are they)?
- Which IRQs belonged to the PS/2 keyboard and the PS/2 mouse?
- What does an optical mouse sensor really measure?
- What does CPI (DPI) mean?
- When you click, which window receives the click: the focused one or the one under the pointer?
ANSWER KEY — DO NOT READ UNTIL YOU'VE ANSWERED
Click to reveal answers
1. Matrix
It needs far fewer wires. About 100 keys can be read with about two dozen wires, because each key joins one row to one column. The microcontroller scans row by row.
2. Debouncing
A contact bounces for a few milliseconds when it closes. Debouncing makes the firmware ignore quick changes until the state is stable. Without it one press could type several letters.
3. Make and break
A make code is sent when a key goes down. A break code is sent when it comes up. The PC needs both to know when keys are released.
4. Layout
The OS. The keyboard only names the physical key; the layout file (for example KBDUS.DLL for US English, hedge on the name) maps it to a character.
5. USB polling
The PC asks. The host controller polls at an interval (about 8 ms = 125 Hz for basic devices, 1 ms or less for gaming ones). The device answers with a report.
6. Boot-protocol report
8 bytes: modifier bits, one reserved byte, and up to six key codes.
7. IRQs
IRQ 1 keyboard, IRQ 12 PS/2 mouse.
8. Optical sensor
Movement. It takes many tiny pictures per second and compares them to find a shift (dx, dy). It does not know position.
9. CPI
Counts per inch: how many counts the mouse reports for one inch of physical movement.
10. Click target
The window under the pointer (found by hit-testing against the Z-order). Keyboard keys go to the focused window.
UNDERSTANDING QUESTIONS
Q1. Why does the keyboard send a scan code instead of the letter?
Answer
The same hardware must work for any language. If the keyboard sent "A", every language would need different hardware. By sending the physical key and letting the OS apply a layout, one keyboard works with US, French, German and any other layout, and the user can switch layouts without touching it.Q2. Why can a basic USB keyboard report only six normal keys at once, and how do N-key rollover keyboards get past it?
Answer
The common boot-protocol report has just six slots for key codes. N-key rollover keyboards use a different report (described by their report descriptor), for example one bit per key, so any number can be held. They also need diodes in the matrix to avoid ghosting.Q3. A USB keyboard needs no interrupt per key press. Why is the PC still not "polling all the time" like the wasteful CPU in Chapter 4?
Answer
The polling is done by the USB host controller hardware, not by the CPU. The controller asks the device on a schedule and copies the answer into RAM by DMA. The CPU is only interrupted when a transfer completes, so the CPU's own time is not spent asking.Q4. Why does a mouse report relative changes instead of an absolute position?
Answer
An optical sensor can only compare successive pictures, so the only thing it can know is how far the pattern moved. It has no map of the desk. Relative reporting also lets you lift and reposition the mouse without the cursor jumping.Q5. Why does Windows have to keep an extra step (virtual key) between the scan code and the character?
Answer
Programs want to ask "was the A key pressed?" without caring about physical position or layout. The virtual key names the key's function and stays stable across layouts. The character step then depends on Shift, Caps Lock, dead keys and AltGr, so it is done last.SCENARIO QUESTIONS (Troubleshooting)
S1. You type "z" and "y" appears (and symbols are in odd places) on a perfectly fine keyboard. What happened?
Answer
A different keyboard layout is active (for example German instead of US). The hardware is fine; Windows is using another translation table. Press Win + Space or change the layout in Settings.S2. Your USB keyboard works in the UEFI/BIOS screen but not in Windows after a driver update. Where do you look?
Answer
The firmware uses the simple boot-protocol report and needs no OS drivers, so the hardware works. The fault is in the Windows driver stack (HID or keyboard class drivers, or a maker driver). Use Device Manager: roll back the driver or uninstall the device and rescan so Windows reinstalls the standard HID driver.S3. In a game, pressing four keys together makes the fourth one not register. What may be the cause, and what could help?
Answer
Limited rollover or ghosting protection on a cheap keyboard (matrix without diodes) or a 6-key report limit. A keyboard advertising N-key rollover (anti-ghosting) helps. Different keys sharing the same matrix lines may block each other.S4. A wireless mouse moves jerkily only when the PC is busy and a USB 3 hard drive is attached right next to the dongle. What would you try?
Answer
Radio interference in the 2.4 GHz band, which USB 3 devices and cables can disturb. Move the dongle away (use a short extension cable), change its port, and check the battery. The report path in Windows is the same as for a wired mouse.S5. The mouse pointer moves too far when you flick, but is fine when you move slowly. What setting is responsible?
Answer
**Enhance pointer precision** (acceleration), which multiplies fast movements more than slow ones. Turn it off in the Mouse settings (Pointer Options) if you want a consistent distance for the same movement.INTERVIEW QUESTIONS
I1. Walk me through what happens from pressing a key to a letter on screen.
Answer
Switch closes; the keyboard's microcontroller finds it in the matrix scan and debounces it; it puts the key into a HID report; the USB host controller polls the keyboard and gets the report by DMA; the HID class stack (hidusb, hidclass, kbdhid, kbdclass) turns it into key-down and key-up events; the win32k raw input thread processes it; the keyboard layout turns the scan code into a virtual key and a character; Windows posts WM_KEYDOWN and WM_CHAR to the focused window; the application updates its text and repaints; the compositor and GPU show the result.I2. What is HID and why is it useful?
Answer
Human Interface Device is a USB (and Bluetooth) class where devices describe their own reports with a report descriptor. One generic driver can then support keyboards, mice, game controllers and other devices from any maker without custom drivers.I3. How does USB polling differ from PS/2 interrupts, and what does each mean for latency?
Answer
PS/2 devices send a byte as soon as it is ready and raise an interrupt (IRQ 1 or 12), so the delay is tiny. USB devices cannot speak first: the host asks at a fixed interval (about 8 ms typical, down to 1 ms for gaming devices), so the worst-case delay is up to one interval. Higher polling rates reduce it.I4. How does Windows decide which window gets a mouse click and which gets a key press?
Answer
Mouse clicks go to the window under the pointer, found by hit-testing against the Z-order (WM_NCHITTEST decides which part). Key presses go to the window with keyboard focus, which is usually the active window. Mouse capture can temporarily send all mouse messages to one window.I5. What are the two places pointer speed can be changed, and how do they differ?
Answer
In the mouse itself (CPI, in hardware: counts reported per inch) and in Windows (pointer speed and Enhance pointer precision, in software: how counts are scaled into cursor movement). The mouse CPI changes what is reported; Windows changes what is done with the report.PRACTICAL TASK
Draw "the life of one key press" for your own PC. Run Get-PnpDevice -Class Keyboard and look at Devices by connection in Device Manager. Draw one box for each real layer on your PC (keyboard, USB port or radio, host controller, HID or PS/2 driver, class driver, Windows input, layout, window, screen). Write the real device and driver names from your PC in the boxes. Add the polling rate or interrupt type you believe applies.
Why This Matters
When a keyboard or mouse misbehaves, a map of the layers tells you where to look: hardware, cable or radio, driver, layout or application. It also helps you see that "typing a letter" is a long, well-organised chain.
CHAPTER SUMMARY
- A keyboard is a small computer. Keys sit in a matrix, a microcontroller scans it, debounces contact bounce, and sends codes. Rollover and ghosting depend on the wiring and the report format.
- Scan codes name physical keys (make on press, break on release). The layout is applied by Windows.
- USB HID: the device describes itself with a report descriptor; the host controller polls it (about 8 ms typical, 1 ms for gaming); a boot keyboard report is 8 bytes. Bluetooth and 2.4 GHz devices use the same HID idea. PS/2 is interrupt-driven (IRQ 1 and IRQ 12).
- The chain on Windows: matrix scan, HID report, xHCI, HID class drivers, kbdclass, raw input thread, layout (scan code to virtual key to character), WM_KEYDOWN and WM_CHAR to the focused window, app draws.
- A mouse reports relative changes (dx, dy), buttons and wheel. The optical sensor is a tiny camera. CPI and polling rate are hardware settings; pointer speed and acceleration are Windows settings.
- Clicks go to the window under the pointer using hit-testing against the Z-order.