Keyboard code

From C64-Wiki
Jump to navigationJump to search

Code-to-key conversion table

CodeKey
Dec.Hex.
0$00Inst/Del
1$01Return
2$02Crsr ←→
3$03F7/F8
4$04F1/F2
5$05F3/F4
6$06F5/F6
7$07Crsr ↑↓
8$083
9$09W
10$0AA
11$0B4
12$0CZ
13$0DS
14$0EE
15$0F(Unused)
CodeKey
Dec.Hex.
16$105
17$11R
18$12D
19$136
20$14C
21$15F
22$16T
23$17X
24$187
25$19Y
26$1AG
27$1B8
28$1CB
29$1DH
30$1EU
31$1FV
CodeKey
Dec.Hex.
32$209
33$21I
34$22J
35$230
36$24M
37$25K
38$26O
39$27N
40$28+
41$29P
42$2AL
43$2B
44$2C>
45$2D[
46$2E@
47$2F<
CodeKey
Dec.Hex.
48$30£
49$31*
50$32]
51$33Clr/Home
52$34(Unused)
53$35=
54$36
55$37?
56$381
57$39
58$3A(Unused)
59$3B2
60$3CSpace
61$3D(Unused)
62$3EQ
63$3FRun/Stop

Keyboard codes are numbers in the range 0–63/$00–$3F, assigned to most of the keys on the 64's keyboard (the exceptions are Shift and Shift Lock keys, the Commodore logo key, the Ctrl key, and the Restore key): When scanning the keyboard, the 64 will place the keyboard code of whatever key was pressed down, in addresses 203 and later 197. If no key was pressed, the scan routine will report a "key code" of 64/$40, and if more than one of the keys which have a key code is pressed, the one with the highest key code will take precedence.

Note the four unused codes; 15/$0F, 52/$34, 58/$3A, and 61/$3D: Internally in the ROM routines handling the keyboard, these codes represent the Shift, Commodore and Ctrl keys. Since they are only used internally, it's not possible to detect the depression of these keys — to do this, use addresses 653 and 654.

  • 15/$0F is the leftmost of the two Shift keys, or the Shift Lock key
  • 52/$34 is the rightmost of the two Shift keys
  • 58/$3A is the Ctrl key
  • 61/$3D is the Commodore logo key

The RESTORE key does not leave software-readable "signs" in the system; rather, it raises a Non-Maskable interrupt. "Reading" the RESTORE key thus implies writing a custom interrupt handling routine for the purpose.

Conversion tables in ROM[edit | edit source]

The KERNAL ROM contains four tables used by the system to convert keyboard codes into PETSCII character codes: Each table contains 65 bytes; one PETSCII code for each of the 64 keys assigned a keyboard code, plus a 255/$FF which will be returned for the keyboard code of 64/$40 (indicating no key pressed).

  • 60289–60353/$EB81–$EBC1: PETSCII codes for keys pressed without simultaneously using Shift, Commodore or Ctrl keys. In this table, the entries for those three keys are 1, 2 and 4; values which get "sorted out" by the keyboard scan routines in ROM and thus never "show up" in the addresses 203 and 197.
  • 60354–60418/$EBC2–$EC02: PETSCII codes for keys pressed simultaneously with a Shift or the Shift Lock keys.
  • 60419–60483/$EC03–$EC43: PETSCII codes for keys pressed simultaneously with the Commodore logo key.
  • 60536–60600/$EC78–$ECB8: PETSCII codes for keys pressed simultaneously with the Ctrl key. This table has several bytes with 255/$FF, indicating no character; if you press Ctrl along with e.g. Inst/Del, the 64 behaves as if nothing was typed at all.