Keyboard
Reference for keyboard scan, key numbering, and MOS keyboard calls. For the hardware path (System VIA CA2 IRQ + slow bus matrix scan), see system-via.
Three key numbering schemes
Every key has three numbers, used by different APIs:
| Scheme | Range | Used by | What it represents |
|---|---|---|---|
| ASCII | 0-255 | OSRDCH, character input | Translated value (after SHIFT/CTRL/CAPS apply) |
| INKEY (negative) | -1 to -128 | BASIC INKEY(-n), OSBYTE &81 scan-key mode | Raw key identity |
| Internal Key Number (IKN) | 0-127 | OSBYTE &78/&79/&7A | Lowest level matrix code |
Conversion: INKEY_value = IKN EOR &FF.
So INKEY(-1) (= &FF) tests IKN 0 (SHIFT). INKEY(-2) (= &FE) tests IKN 1 (CTRL).
Common keys — quick reference
| Key | ASCII | INKEY | IKN |
|---|---|---|---|
| SHIFT | — | -1 | 0 |
| CTRL | — | -2 | 1 |
| Caps Lock | — | -65 | 64 |
| Shift Lock | — | -81 | 80 |
| Return | 13 | -74 | 73 |
| Escape | 27 | -113 | 112 |
| Tab | 9 | -97 | 96 |
| Delete | 127 | -90 | 89 |
| Copy | 135 | -106 | 105 |
| Cursor Up | 139 | -58 | 57 |
| Cursor Down | 138 | -42 | 41 |
| Cursor Left | 136 | -26 | 25 |
| Cursor Right | 137 | -122 | 121 |
| Space | 32 | -99 | 98 |
| 0 | 48 | -40 | 39 |
| 1-9 | 49-57 | varies | varies |
| A-Z | 65-90 | varies | varies |
| f0 | — | -33 (&DF) | 32 |
| f1 | — | -114 (&8E) | 113 |
| f2 | — | -115 (&8D) | 114 |
| f3 | — | -116 (&8C) | 115 |
| f4 | — | -21 (&EB) | 234 — out of sequence (matrix wiring) |
| f5 | — | -117 (&8B) | 116 |
| f6 | — | -118 (&8A) | 117 |
| f7 | — | -23 (&E9) | 232 — out of sequence (matrix wiring) |
| f8 | — | -119 (&89) | 118 |
| f9 | — | -120 (&88) | 119 |
Full table in NAUG §14.1 p219-220. The IKN ordering doesn’t follow ASCII — it’s matrix-determined.
Measured internal key numbers (2026)
The table below was measured, not recalled, on a Master 128 in jsbeeb by Edge Grinder (2026-09-06, layer-9h-keyredef.md); Paradroid’s own table, measured on a Model B, agrees on every key it has and is the cross-check, not the source (beeb-port-kit-hardware-facts §7). Every key that appears in the quick-reference table above agrees with it — SHIFT 0, CTRL 1, RETURN 73, ESCAPE 112, SPACE 98, cursor up/down/left/right 57/41/25/121.
| Key | IKN | Key | IKN | Key | IKN | Key | IKN |
|---|---|---|---|---|---|---|---|
| A | 65 | K | 70 | U | 53 | UP | 57 |
| B | 100 | L | 86 | V | 99 | DOWN | 41 |
| C | 82 | M | 101 | W | 33 | LEFT | 25 |
| D | 50 | N | 85 | X | 66 | RIGHT | 121 |
| E | 34 | O | 54 | Y | 68 | SHIFT | 0 |
| F | 67 | P | 55 | Z | 97 | CTRL | 1 |
| G | 83 | Q | 16 | / | 104 | RETURN | 73 |
| H | 84 | R | 51 | : | 72 | SPACE | 98 |
| I | 37 | S | 81 | ESCAPE | 112 | ||
| J | 69 | T | 35 |
Method: A%=121:X%=16:Y%=0 (OSBYTE &79 scanning from key 16) in a BASIC loop with the key held, proved against a number the port already had (Z = 97) before it was trusted. The MOS scan will not report keys 0-2 (SHIFT, CTRL), and OSBYTE &7A starts at 16 for the same reason; those were measured through INKEY and calibrated on a known key — internal = (-INKEY) - 1, the same thing as the IKN = INKEY EOR &FF conversion above (Z is INKEY -98, internal 97). INKEY codes confirmed in Paradroid’s Layer 3 through OSBYTE &81: Z -98, X -67, K -71, M -102, UP -58, DOWN -42; CTRL is INKEY -2. When measuring from a BASIC session: *FX 4,1 first or the cursor keys edit the line; *FX 229,1 or BASIC eats ESCAPE.
Read a character (blocking) — OSRDCH
JSR &FFE0 ; A = ASCII char read from input stream
BCS error ; C=1 = read error (e.g. ESCAPE)Blocks until a char arrives. Honours the currently-selected input stream (keyboard or RS423).
Read a character with timeout — OSBYTE &81
LDA #&81
LDX #time_lo ; centiseconds, low byte
LDY #time_hi ; centiseconds, high byte (max &7FFF total ≈ 5.5 min)
JSR &FFF4
; on exit:
; C=0, X = ASCII char, Y = 0 → got a char
; C=1, Y = &FF → timeout
; C=1, Y = &1B (27) → ESCAPETest if a specific key is pressed — OSBYTE &81 scan mode
LDA #&81
LDX #INKEY_value ; e.g. &FF for SHIFT (= -1 negated to unsigned)
LDY #&FF
JSR &FFF4
; on exit:
; X = &FF, Y = &FF → key pressed
; X = 0, Y = 0 → not pressedFor polling multiple keys in a tight loop, this is the fastest MOS-blessed path.
Test if any key is pressed — OSBYTE &79/&7A
LDA #&7A ; scan from IKN &10 upwards
LDX #0
LDY #0
JSR &FFF4
; X = IKN of first pressed key, or 0 if noneFlagged 2026-09-07: “or 0 if none” disagrees with NAUG’s own
&FFfor&79(see naug-ch14-keyboard) and with a measurement — the MOS in jsbeeb returnsX = &FFfromOSBYTE &7Awhen no key is down (Paradroid, 2026-08-26; beeb-port-kit-hardware-facts §7). Treat both 0 and&FFas idle. Text kept for the record.
Model B caveat: OSBYTE &79 scans in a strange order (&10, &20, &30, …, &70, &11, &21, …). Master / Compact / Electron scan ascending.
Direct matrix scan — bypassing MOS
Two different jobs here, and they need different code. Testing a key you can name is one write and one read — no iteration, and it is what a game’s control loop wants. Discovering what is pressed is the scan proper. Take the first unless you actually need the second.
Test one known key — the useful one
The internal key number is the address of the key in the matrix: PA0-PA3 carry the column, PA4-PA6 the row. So you write the IKN to port A and read the answer back on PA7.
\ X = INKEY value (e.g. &9E for Z). Returns Z set if the key is down.
.test_key
TXA
EOR #&FF \ INKEY value -> internal key number (0-127)
LDX #&03 \ addressable latch: line 3, value 0
LDY #&0B \ addressable latch: line 3, value 1
PHP
SEI \ the MOS owns the slow bus; do not be interrupted
STX &FE40 \ ORB: keyboard /WE low — stop the free-run scan
LDX #&7F
STX &FE43 \ DDRA: PA0-PA6 output, PA7 input
STA &FE4F \ port A, NO HANDSHAKE — the key number
LDA &FE4F \ read it back: PA7 is the key's state
STY &FE40 \ ORB: keyboard /WE high — free-run scan resumes
PLP
LDX #0
ROL A \ PA7 -> carry: set = pressed
BCC key_up
LDX #&FF
.key_up
CPX #&FF \ Z set = pressed
RTSFour details that are easy to get wrong:
&FE4F, not&FE41. The no-handshake port A register.&FE41strobes CA2 and disturbs the MOS’s own keyboard handling.- IKN = INKEY EOR &FF, which is 0-127 — so the bit 7 you write is always 0 and PA7 stays a clean input.
PHP/PLP, notSEI/CLI, so a caller that was already masked stays masked.- DDRA is not restored and does not need to be: the MOS sets it in its own scan, and any well-behaved sound driver saves and restores it around its own writes.
You can watch it work: writing IKN &61 (Z) and reading back gives &61 with the key up and
&E1 with it down — the same seven bits, PA7 carrying the answer.
Cost: ~55-70 cycles against ~240 for OSBYTE &81 — measured on a Model B, JSR to RTS
inclusive, with the SHEILA accesses’ 1 MHz bus stretching included. Worth it for any loop testing
several keys per frame. The masked window is 26 cycles, which matters if you are running
cycle-critical raster code.
Working example: test_inkey in Thrust (Superior Software, 1986).
Scan for whatever is pressed
Only when you genuinely need to discover keys rather than test them — chord detection with unknown
keys, a redefine-keys screen. Same setup, then iterate: write each (row, column) pair to PA0-PA6
and read PA7, over columns 0-14 and rows 0-7. The MOS does exactly this from its CA2 interrupt,
which is why nKBEN has to be low throughout.
Detailed matrix layout differs between Model B, Master, Master Compact (NAUG §14 opening note).
For the Model B the mapping is IKN = row * 16 + column — verified against P (row 3, col 7 =
&37, INKEY -56), [ (row 3, col 8 = &38, -57), cursor up (row 3, col 9 = &39, -58) and the
constants in the table above.
Auto-repeat
| OSBYTE | Function | Default |
|---|---|---|
&0B (11) | Initial delay (centiseconds, 0 = disable repeat) | 50 |
&0C (12) | Repeat period | 8 |
&C4 (196) | R/W initial delay | — |
&C5 (197) | R/W period | — |
For games / responsive UI: *FX 11,0 disables repeat. Keys still scan on press/release.
Soft keys (function keys + others)
Function keys f0-f9 expand into user-defined strings stored at &B00-&BFF (os-workspace).
| OSBYTE | Function |
|---|---|
&12 (18) | Reset all soft-key definitions |
&E1-&E4 (225-228) | R/W function-key status: alone / SHIFT / CTRL / SHIFT+CTRL. 0=ignore, 1=expand, ≥2=ASCII offset |
&DD-&E0 (221-224) | R/W character-status for &C0-&FF (treat as soft key / ASCII / ignore) |
&D8 (216) | R/W remaining soft-key buffer length |
&F4 (244) | R/W consistency flag |
Defaults: fn alone = expand. SHIFT+fn = ASCII &80+n. CTRL+fn = &90+n. SHIFT+CTRL+fn = ignored.
Cursor-key behaviour
OSBYTE &04:
- X=0: cursor keys = edit (default).
- X=1: ASCII 135-139.
- X=2: act as soft keys 11-15.
- X=3: Master Compact joystick emulation.
Status byte (modifier keys) — OSBYTE &CA
Read the current modifier state:
LDA #&CA : LDX #0 : LDY #&FF : JSR &FFF4
; X = status byteModel B layout:
| Bit | Set when |
|---|---|
| 3 | SHIFT pressed |
| 4 | 0 = CAPS LOCK engaged (inverted) |
| 5 | 0 = SHIFT LOCK engaged (inverted) |
| 6 | CTRL pressed |
| 7 | SHIFT enable (locks reverse on SHIFT) |
Electron: bits 4 (CAPS), 5 (FUNC), 6 (SHIFT), 7 (CTRL).
After writing the status byte directly, call OSBYTE &76 to update the keyboard LEDs accordingly.
Disable / suppress
| OSBYTE | Function |
|---|---|
&B2 (178) | Keyboard semaphore — 0 = ignore IRQs, &FF = normal (its variable is &0242) |
&C9 (201) | Keyboard disable — non-zero = ignore all except BREAK (Econet use only) |
&76 (118) | Refresh CAPS/SHIFT LOCK LEDs |
Special character mappings
| OSBYTE | Function | Default |
|---|---|---|
&DB (219) | TAB character — set to &80+n to make TAB a soft key | 9 |
&DC (220) | ESCAPE character | &1B (27) |
Performance / bypass
- MOS scan via
OSBYTE &81or&7Acosts several hundred cycles per call — 243 measured on a Model B,JSRtoRTSinclusive. - Exact figures from the port that took them (Paradroid, 2026-08-26, jsbeeb — beeb-port-kit-hardware-facts §3): 69 cycles direct against 243 through
OSBYTE &81,JSRtoRTSinclusive; the OSBYTE figure taken twice by different routes (a poll-loop rate and a timer bracket, 238 and 237), the direct figure a breakpoint pair differencingelapsed_cycles. The 26 masked cycles, twelve times a pass, cost a T1-driven rupture nothing: 25.0 Hz before and after. - Phantom keys, on real hardware. The matrix has no diodes, so five keys held at once can phantom a sixth, and testing one key at a time does not save you: Z, X, K, M and L together triggered a debug key on C. Reported by KC, not instrumented; addressed by gating every debug key on CTRL. A phantom landing on a play control is still an open problem (Paradroid human-notes-status.md).
- Edge-triggered keys must not block waiting for release where two can be held together, or the loop deadlocks swallowing the press (Paradroid Layer 3, jsbeeb).
- While
!BOOTis being EXECed,INKEYand OSRDCH read the exec stream, not the keyboard. An intro’sINKEY(0)consumed the*of the next*RUNline. Use theOSBYTE &7Ascan for “any key” (Paradroid, 2026-08-26 and 2026-08-29). See filing-systems. - Testing one named key direct costs ~55-70 cycles — see test-one-known-key—the-useful-one. A control loop polling a dozen keys a frame saves ~2,000 cycles by going direct, which is a couple of percent of a 50 Hz frame.
- A full direct scan of every key is a different (and much rarer) job; budget per key tested, ~3 µs each.
- What the MOS’s key scan costs a game that keeps the MOS (MOS 3.20): while any key is held, the 100 Hz tick does a full keyboard-matrix scan every tick — 6.3% of all CPU cycles in Stryker’s Run’s original, inside a tick that takes ~16% in play. Zeroing the keyboard polling byte
&0242(OSBYTE&B2) stops the tick’s key scan; a game reading keys direct loses nothing by it (Stryker’s Run, 2026-10-16, jsbeeb 2.5.0, Master,tools/irqprofile.mjs; decision 14 — beeb-port-kit-hardware-facts-2026-11 §3, §6). - A routine’s first bytes are an entry point too: Stryker’s Run’s optimiser dropped
keydown_int’s leadingTXA, and observer mode’sJSR keydown_int + 1then landed in an operand — no key worked there. Search forlabel+ncallers before changing an entry (BUGS #11; kit gotchas class 4). - Emulator key names changed under the harnesses: jsbeeb 2.0 changed
keyDownfrom a numeric browser key code to aKeyboardEvent.codestring (silently pressing nothing until 2.3.1 made it throw), and jsbeeb-mcp 4.0.0 renamed its key names — the internal and INKEY numbers did not move. See emulators. - Disabling auto-repeat (
*FX 11,0) is mandatory for any game that distinguishes “key down” from “auto-repeated”. - The keyboard buffer (ID 0) carries the type-ahead — purge with
OSBYTE &15, X=0if you want a clean slate (buffers).
KBDENC scan modes (hardware)
The Master’s keyboard encoder (KBDENC IC, a custom Acorn ULA — same role on Model B / B+ via different parts) operates in three modes per master-arm Ch 5:
- Free-run (idle, no key pressed): a 4-bit counter clocked at 1 MHz drives a 4-to-15 decoder, rippling a logic-low signal through columns C0-C14. If any key is pressed, the NAND output goes high and triggers CA2 → System VIA → IRQ. The CPU does nothing during free-run.
- Column detection (after IRQ, MOS scan): MOS asserts
nKBEN(latch line 3 low) to stop free-run, then writes a column number to PA0-PA3 and reads PA7. The selected column output drives low; if a key on that column is pressed, PA7 reads low. MOS scans columns 0-14 to find the active one. - Row detection: with the column held, MOS writes a row number to PA4-PA6 and reads PA7. The row decoder gates one row’s input onto PA7 in inverted form, so PA7 high means “key at this (col,row) is pressed”.
After identifying all pressed keys, MOS returns to free-run by clearing nKBEN. MOS rescans every 10 ms (via the 100 Hz interval timer) while any key remains held, until the matrix is clear.
For a direct-matrix scan from user code (SEI + drive PA + read PA7), you take over the slow bus — which competes with sound-chip writes and CMOS access. Pattern: own the bus, drive nKBEN low (latch line 3 = 0), iterate through column/row pairs of interest, then restore. ~3 µs per key tested; ~200-300 cycles to scan all interesting keys.
The Model B / B+ keyboard hardware is functionally identical at the slow-bus interface, so this section applies to all BBC machines — just the silicon implementing the matrix scanner differs.
See also
- system-via — CA2 IRQ source, slow-bus matrix scan via addressable latch line 3.
- buffers — keyboard buffer (ID 0).
- events — events 2 (char entering input) and 6 (ESCAPE).
- os-workspace — soft-key string storage at
&B00-&BFF.
This wiki is curated by Claude following the LLM-Wiki methodology — a human curates source documents, the LLM compiles structured cross-linked markdown. Content may contain errors, omissions, or stale claims. For authoritative information refer to the original source documents in the bbc-documents GitHub archive.