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:

SchemeRangeUsed byWhat it represents
ASCII0-255OSRDCH, character inputTranslated value (after SHIFT/CTRL/CAPS apply)
INKEY (negative)-1 to -128BASIC INKEY(-n), OSBYTE &81 scan-key modeRaw key identity
Internal Key Number (IKN)0-127OSBYTE &78/&79/&7ALowest 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

KeyASCIIINKEYIKN
SHIFT—-10
CTRL—-21
Caps Lock—-6564
Shift Lock—-8180
Return13-7473
Escape27-113112
Tab9-9796
Delete127-9089
Copy135-106105
Cursor Up139-5857
Cursor Down138-4241
Cursor Left136-2625
Cursor Right137-122121
Space32-9998
048-4039
1-949-57variesvaries
A-Z65-90variesvaries
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.

KeyIKNKeyIKNKeyIKNKeyIKN
A65K70U53UP57
B100L86V99DOWN41
C82M101W33LEFT25
D50N85X66RIGHT121
E34O54Y68SHIFT0
F67P55Z97CTRL1
G83Q16/104RETURN73
H84R51:72SPACE98
I37S81ESCAPE112
J69T35

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)                → ESCAPE

Test 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 pressed

For 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 none

Flagged 2026-09-07: “or 0 if none” disagrees with NAUG’s own &FF for &79 (see naug-ch14-keyboard) and with a measurement — the MOS in jsbeeb returns X = &FF from OSBYTE &7A when no key is down (Paradroid, 2026-08-26; beeb-port-kit-hardware-facts §7). Treat both 0 and &FF as 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
  RTS

Four details that are easy to get wrong:

  • &FE4F, not &FE41. The no-handshake port A register. &FE41 strobes 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, not SEI/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

OSBYTEFunctionDefault
&0B (11)Initial delay (centiseconds, 0 = disable repeat)50
&0C (12)Repeat period8
&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).

OSBYTEFunction
&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 byte

Model B layout:

BitSet when
3SHIFT pressed
40 = CAPS LOCK engaged (inverted)
50 = SHIFT LOCK engaged (inverted)
6CTRL pressed
7SHIFT 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

OSBYTEFunction
&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

OSBYTEFunctionDefault
&DB (219)TAB character — set to &80+n to make TAB a soft key9
&DC (220)ESCAPE character&1B (27)

Performance / bypass

  • MOS scan via OSBYTE &81 or &7A costs several hundred cycles per call — 243 measured on a Model B, JSR to RTS inclusive.
  • 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, JSR to RTS inclusive; 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 differencing elapsed_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 !BOOT is being EXECed, INKEY and OSRDCH read the exec stream, not the keyboard. An intro’s INKEY(0) consumed the * of the next *RUN line. Use the OSBYTE &7A scan 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 leading TXA, and observer mode’s JSR keydown_int + 1 then landed in an operand — no key worked there. Search for label+n callers before changing an entry (BUGS #11; kit gotchas class 4).
  • Emulator key names changed under the harnesses: jsbeeb 2.0 changed keyDown from a numeric browser key code to a KeyboardEvent.code string (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=0 if 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:

  1. 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.
  2. 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.
  3. 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.