Keyboard Input

Keyboard input is the primary means by which humans enter data and programs into a computer — translating physical key presses into binary codes that the processor can read.

Why This Matters

Interactive computing requires a way to enter commands and data in real time. Toggle switches (the original computer input) are functional but slow. A keyboard, even a crude hand-built one, enables interactive programming, command entry, and operator control at human typing speed.

Understanding keyboard hardware is also useful for maintaining or repairing any existing keyboards, as well as for building improvised input devices. A keyboard is ultimately just a matrix of switches with encoding logic — straightforward to design and build.

The keyboard also introduces important hardware concepts: debouncing (mechanical contacts bounce), scanning (reading a matrix efficiently), and character encoding (mapping physical key to ASCII or other code).

Key Switch Matrix

Physical keyboards use a switch matrix to minimize the number of wires needed. N keys could be connected individually (N wires) but instead are arranged in a grid of R rows × C columns where R×C ≥ N.

For an 8×8 matrix (64 keys, needs only 8+8 = 16 wires):

  • 8 column wires connect to outputs of the scanning circuit
  • 8 row wires connect to inputs with pull-up resistors to +5V
  • Each key connects one row to one column when pressed

Scanning: drive each column wire low in turn (all others high). Read all row wires. If any row wire reads low, a key is pressed at the intersection of that row and the currently driven column. Record the column number and row number — this is the key’s matrix position.

The scanner cycles through all 8 columns, completing one full scan in 8 steps. Scan frequency: at 1 MHz CPU, scan one column per 100 µs → full scan in 800 µs → 1,250 scans per second. Humans type at 10–15 keys per second — the scan rate is thousands of times faster than necessary.

Matrix scanning requires: 8-bit shift register or counter to drive column outputs sequentially, 8 tri-state input buffers to read row lines, and a lookup table (ROM or encoded logic) to convert (row, column) to ASCII.

Contact Debouncing

Mechanical key switches bounce: when pressed, the contacts make and break contact several times over 1–20 ms before settling. Without debouncing, one key press generates multiple keypresses detected by the scanning logic.

Hardware debounce with RC + Schmitt trigger:

  • Connect 10kΩ resistor from row line to key contact
  • 100nF capacitor from key contact to ground
  • Route through 74HC14 Schmitt trigger inverter
  • RC charges/discharges slowly (RC = 1ms), smoothing bounce
  • Schmitt trigger has hysteresis (~0.8V) preventing multiple transitions

Software debounce (simpler, no extra hardware):

  • On detecting a key state change, wait 20 ms before registering the key
  • During the wait, ignore further state changes on that key
  • After 20 ms, confirm the key state and register the press

The software approach is recommended for hand-built keyboards: implement in the keyboard scanning routine, requiring only a timer (or a simple counter that increments each scan cycle, counting up to the debounce threshold).

ASCII Encoding Lookup Table

The keyboard controller must map the (row, column) position of a pressed key to its ASCII code. A small ROM or EPROM (256 bytes is more than enough for a full keyboard) indexed by row×8 + column gives the ASCII character.

For a minimal keyboard with ASCII printable characters (space through tilde, 32–126) plus control keys:

Populate the lookup table:

  • Matrix position 0,0 → ASCII 32 (space)
  • Position 0,1 → ASCII 65 (‘A’)
  • Position 0,2 → ASCII 66 (‘B’)
  • etc.

Shift key handling: maintain a shift state register (1 bit). When shift is held, look up the shifted version (second table or offset). The 74LS/HC EPROM or a small MCU with flash both work as lookup tables.

Alternatively, use a standard keyboard encoder IC. The MM74C923 is a 20-key encoder; larger keyboards use the AY-5-2376 (64-key ASCII encoder) or similar. These historic ICs contain complete scan, debounce, and encode logic in one package.

PS/2 and Serial Keyboard Interfaces

Rather than building a matrix keyboard from scratch, existing PC keyboards (PS/2 or USB) can be interfaced to a hand-built computer with adapter circuits.

PS/2 keyboard protocol: serial communication at ~10-20 kHz. Each keypress sends an 11-bit frame: start bit (0), 8 data bits (scan code), odd parity bit, stop bit (1). Two wires: CLK (generated by keyboard) and DATA.

Interface to a simple CPU:

  1. Connect PS/2 CLK to an external interrupt pin or monitor with polling
  2. Shift in 8 data bits on the CLK signal using a shift register (74HC595 or software bit-banging)
  3. After 8 bits received, look up the scan code in a translation table to get ASCII
  4. Store ASCII in keyboard buffer

PS/2 scan codes are not directly ASCII: each key has a unique code independent of shift state. A translation table (typically 256 bytes, indexed by scan code) converts to ASCII. Separate tables for unshifted, shifted, and control states.

USB keyboards are much more complex to interface (requires USB host controller hardware or a purpose-built MCU with USB support) and are not recommended for early-stage rebuilding.

A PS/2 keyboard connected to a hand-built system through a shift-register interface provides immediate full-keyboard input without building the keyboard hardware from scratch — an excellent practical shortcut.