Skip to main content
Back to all articles

How Keyboard Input Works: A Technical Look for Curious People

When you press a key on your keyboard, a lot happens between the physical press and the character appearing on your screen. Most people never need to know any of this, but if you are curious about…

When you press a key on your keyboard, a lot happens between the physical press and the character appearing on your screen. Most people never need to know any of this, but if you are curious about how computers actually work at a low level, the journey from physical key press to visible character is an interesting story.

The keyboard is fundamentally an electrical device. Under each key is a switch mechanism. When the key is pressed far enough to actuate, the switch closes an electrical circuit. In older keyboards and many current mechanical keyboards, this is a physical contact where two metal components touch. In capacitive keyboards, no contact is made. Instead, the change in electrical capacitance between two surfaces is detected without physical contact. Optical keyboards use an infrared beam that is interrupted when the key is pressed.

The keyboard controller is a small microprocessor inside the keyboard that monitors all the key switches continuously. This monitoring happens in a process called scanning. The controller scans all the keys many times per second, checking whether any switch state has changed. The rate at which this scanning occurs is part of what determines the polling rate of the keyboard, which is how frequently the keyboard reports its state to the computer.

When the controller detects a key press, it generates a scan code. A scan code is simply a number that identifies which physical key was pressed. Importantly, scan codes are specific to the physical key position, not the character that key represents. The number 30 might represent the physical key in a certain position regardless of whether that key is labeled A in an English layout or Q in a French layout.

The scan code is sent from the keyboard to the computer via the connection, which is typically USB today but was historically PS/2 or older connector types. The computer's operating system receives the scan code through a device driver, which is software that handles communication between the operating system and the keyboard hardware.

The operating system translates the scan code into a virtual key code, which is a more abstract identifier for the key. This translation depends on the current keyboard layout setting. If you have a US English layout active, scan code 30 becomes the virtual key for A. If you switch to a French AZERTY layout, the same scan code becomes Q. This is how keyboard language switching works without needing a different physical keyboard.

The virtual key code is then translated again into a character code, which represents the actual text character to be produced. This translation depends on modifier key states. If Shift is held when you press the A key, the character produced is uppercase A rather than lowercase a. If Caps Lock is on, the same thing happens. The operating system tracks modifier key states and applies them during this translation.

The character code finally reaches the application that has focus, meaning the one that is currently receiving keyboard input. The application interprets the character and either displays it in a text field, treats it as a command, or passes it along to some other processing. In a word processor, the character is inserted into the document. In a game, it might trigger a character movement or action.

The keyboard simulator hooks into this process at the virtual key code level. It listens for the key events that the browser receives from the operating system and uses those events to trigger the key animations. This is why the simulator responds to your keyboard regardless of which application is in the foreground: the browser itself is capturing the keyboard events and the simulator JavaScript receives them from the browser.