Reading the Keyboard, Console Keys and HELP — and Taming BREAK

INPUT is fine for typing a name, but games and menus want something snappier: one keypress, no RETURN, no cursor, and ideally no way for the user to wreck things with BREAK. The Atari gives you several routes to the keyboard, plus a separate register for the three yellow console keys. This guide covers them all.

You will learn: reading a single key through the K: device, reading the raw key code from location 764, detecting START/SELECT/OPTION, the XL/XE HELP key, adjusting key repeat and click, the CAPS and inverse flags, and disabling BREAK so it stays disabled.

You need: Atari BASIC on any 8-bit Atari. The HELP-key, repeat and click locations exist only on XL/XE machines.

1. One key, the easy way: GET from K:

The keyboard handler is a device like any other. Open it for input and GET returns the ATASCII code of the next key pressed, waiting if necessary:

10 OPEN #1,4,0,"K:"
20 PRINT "PRESS A KEY"
30 GET #1,K
40 PRINT "YOU PRESSED ";CHR$(K);" (CODE ";K;")"
50 GOTO 30

Because the handler translates keys for you, SHIFT, CTRL and CAPS are all taken care of, and RETURN arrives as 155. This is the right tool for menus:

100 PRINT "1) PLAY  2) SCORES  3) QUIT"
110 GET #1,K
120 IF K<49 OR K>51 THEN 110
130 ON K-48 GOSUB 1000,2000,3000
140 GOTO 100

(Digits 1–9 are ATASCII 49–57.) Remember to CLOSE #1 before your program ends or re-opens the channel, or you will get error 129. Turbo-BASIC XL shortens this to a plain GET K, and also offers INKEY$.

2. Without waiting: location 764 (CH)

GET stops the program until a key arrives — no good in an action game. Location 764 ($2FC), CH, holds the hardware code of the last key pressed, or 255 if none. Poll it, and POKE 255 back to "consume" the key:

10 IF PEEK(764)=255 THEN 10
20 PRINT PEEK(764)
30 POKE 764,255:GOTO 10

This is the wait-for-any-key idiom found in countless listings (Antic's error-trapping article uses exactly this). Run it and press keys to discover codes for yourself.

These codes are not ATASCII; they are the raw keyboard-matrix values. A few common ones, from Carl Evans's tables in Atari BASIC Faster and Better:

Key Code Key Code Key Code
A 63 1 31 RETURN 12
N 35 2 30 SPACE 33
Y 43 3 26 ESC 28
Q 47 9 48 0 50

Modifier keys set the top bits: SHIFT adds 64 (SHIFT+A is 127) and CTRL adds 128. SHIFT, CTRL and BREAK never appear on their own, but CAPS, TAB, ESC and the inverse (Atari logo) key do. Because CH ignores whether CAPS is on, it is ideal for games that want "the Q key" regardless of case.

To act repeatedly while a key is held, keep testing CH without clearing it:

200 IF PEEK(764)=TEST THEN GOSUB 500:GOTO 200
210 POKE 764,255:RETURN

3. START, SELECT and OPTION: location 53279 (CONSOL)

The yellow console keys are not part of the keyboard matrix at all; they are read from the GTIA register CONSOL at 53279 ($D01F). The low three bits read 0 when a key is held: bit 0 is START, bit 1 SELECT and bit 2 OPTION.

Held PEEK(53279)
none 7
START 6
SELECT 5
START + SELECT 4
OPTION 3
START + OPTION 2
SELECT + OPTION 1
all three 0
10 PRINT "PRESS START TO PLAY, SELECT FOR LEVEL"
20 C=PEEK(53279)
30 IF C=6 THEN 100
40 IF C=5 THEN LV=LV+1:PRINT "LEVEL ";LV:FOR D=1 TO 30:NEXT D
50 GOTO 20
100 PRINT "GO!"

The short delay in line 40 stops one press counting several times. Because CONSOL is separate from CH, a program can use the main keyboard for one job and the console keys for another — Evans notes that the Atari Word Processor works this way.

CONSOL doubles as the keyboard "click" speaker: POKEing values 0–8 here produces a click, and alternating 8 and 0 in a loop makes a buzz. The OS writes 8 to it every vertical blank. RESET is not readable here — it is wired differently and always interrupts the program.

4. The HELP key (XL/XE)

XL and XE keyboards have a HELP key, reported in location 732 ($2DC), HELPFG: 17 means HELP alone, 81 SHIFT+HELP and 145 CTRL+HELP. The OS never clears it, so POKE 0 after you have read it:

10 IF PEEK(732)=17 THEN POKE 732,0:GOSUB 900

5. Comfort settings and flags

Location Name Use
729 ($2D9) KRPDEL (XL/XE) Delay before auto-repeat starts, in jiffies (default 48; 0 = no repeat)
730 ($2DA) KEYREP (XL/XE) Jiffies between repeats (default 6 ≈ ten per second)
731 ($2DB) NOCLIK (XL/XE) Non-zero silences the key click
702 ($2BE) SHFLOK 0 = lower case, 64 = CAPS lock, 128 = CTRL lock
694 ($2B6) INVFLG 0 = normal, 128 = typed characters come out inverse
752 ($2F0) CRSINH Non-zero hides the cursor (takes effect when it next moves)

POKE 702,64 before an INPUT guarantees upper-case answers, which saves comparing against both cases. Note that INVFLG only affects characters as they are typed, not text you PRINT. CRSINH is reset whenever the screen is OPENed or a GRAPHICS command runs, so set it again afterwards.

6. Disabling BREAK — and keeping it disabled

Pressing BREAK in the middle of a disk write or a display-list trick can leave things in a mess. BREAK is a POKEY interrupt, enabled by bit 7 of the IRQ enable mask. The OS keeps a shadow copy of that mask in POKMSK (16) and writes it to IRQEN (53774). Clear bit 7 in both:

20530 REM DISABLE BREAK KEY
20531 B=PEEK(16):IF B>127 THEN B=B-128
20532 POKE 16,B:POKE 53774,B
20533 RETURN

The catch: the OS re-enables BREAK on RESET, on any OPEN of the screen (S: or E:), on the first PRINT to the screen after such an OPEN, and on every GRAPHICS command. So call the subroutine again after each of those — many programs simply call it after every GRAPHICS statement and before every bout of input. To re-enable BREAK, POKE both locations with 192, the power-up value.

Only disable BREAK in finished programs; while debugging it is your emergency exit. In Turbo-BASIC XL, the *B command makes BREAK trappable with TRAP instead.

7. In assembly language

The same location works from machine code — check for a key without waiting, then clear it:

CH      = $02FC

        LDA CH
        CMP #$FF        ; no key?
        BEQ NOKEY
        AND #$3F        ; strip SHIFT/CTRL bits
        STA LASTKEY
        LDA #$FF
        STA CH          ; consume the key
NOKEY   RTS

Troubleshooting

  • GET returns the wrong key or error 129: the channel is already open — CLOSE it first.
  • A held key triggers a menu option twice: clear CH (POKE 764,255) after acting, and add a short delay for CONSOL.
  • BREAK works again after the title screen: a GRAPHICS or screen OPEN re-enabled it; repeat the disable routine.
  • The cursor reappears: CRSINH is reset by GRAPHICS and screen OPENs.
  • HELP never seems to clear: you must POKE 732,0 yourself.

Further reading

  • Atari BASIC Faster and Better — Carl M. Evans (IJG, Inc., 1983), Chapter 11 "Keyboard Trickery" and Appendix B key-code tables.
  • Mapping the Atari — Ian Chadwick (Small System Services, 1983).
  • Atari BASIC Tutorial — Robert A. Peck (Howard W. Sams, 1983), Chapter 6 on keeping control during user input.

Credits