Skip to content

Commit 8669aff

Browse files
cursoragentomiq
andcommitted
Docs: PEEK keyboard uses uppercase ASCII; UCASE$ INKEY$ in gfx_inkey_demo
- gfx_inkey_demo: UCASE$ before ASC so lowercase w matches W=87 - gfx_key_demo: clarify REMs (56320+n is key_state index, not PETSCII) - README: keyboard polling vs INKEY$ case Co-authored-by: Chris Garrett <chris@chrisg.com>
1 parent e3cb644 commit 8669aff

3 files changed

Lines changed: 56 additions & 51 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ Releases include **basic-gfx** — a full graphical version of the interpreter b
351351

352352
**Keyboard polling (basic-gfx)**:
353353

354-
- BASIC can poll a simple key-down map via `PEEK(56320 + code)` where 56320 is 0xDC00.
354+
- BASIC can poll a simple key-down map via `PEEK(56320 + code)` where 56320 is 0xDC00 (`GFX_KEY_BASE`).
355+
- For letters, **code is ASCII of the uppercase key** (e.g. W = 87, same as `ASC("W")`), not the PETSCII screen code. Lowercase **w** is not a separate slot.
355356
- Supported codes include ASCII `A``Z`, `0``9`, Space (32), Enter (13), Esc (27), and C64 cursor codes Up (145), Down (17), Left (157), Right (29).
356357

357358
**INPUT (basic-gfx)**:
@@ -361,6 +362,7 @@ Releases include **basic-gfx** — a full graphical version of the interpreter b
361362
**INKEY$() (basic-gfx)**:
362363

363364
- `INKEY$()` is **non-blocking**: it returns a 1-character string for the next queued keypress, or `""` if none.
365+
- The character may be **upper or lower case**; use **`UCASE$(INKEY$())`** (or compare to both) if you test with numeric **`ASC`**.
364366
- This is driven by the raylib host; it is currently available in `basic-gfx` (gfx build) and returns `""` in the terminal build.
365367
- Example: `./basic-gfx -petscii examples/gfx_inkey_demo.bas`
366368

examples/gfx_inkey_demo.bas

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
2 REM Run with: ./basic-gfx -petscii examples/gfx_inkey_demo.bas
33
3 REM
44
4 REM INKEY$() returns a 1-character string when a keypress is available,
5-
5 REM or "" if no key was pressed since last call.
5+
5 REM or "" if none. Letters may be upper or lower case — UCASE$ matches WASD.
6+
6 REM Compare to gfx_key_demo.bas: PEEK(56320+n) uses n = ASCII of UPPERCASE
7+
7 REM letter (W=87, A=65, S=83, D=68, ESC=27). INKEY$ gives the real char.
68
10 SCR = 1024
79
20 COL = 55296
810
30 X = 20
@@ -17,20 +19,20 @@
1719
120 REM Loop
1820
130 K$ = INKEY$()
1921
140 IF K$ = "" THEN SLEEP 1 : GOTO 130
20-
150 K = ASC(K$)
21-
160 IF K = 27 THEN END
22-
170 NX = X : NY = Y
23-
180 IF K = 87 THEN NY = Y - 1
24-
190 IF K = 83 THEN NY = Y + 1
25-
200 IF K = 65 THEN NX = X - 1
26-
210 IF K = 68 THEN NX = X + 1
27-
220 IF NX < 0 THEN NX = 0
28-
230 IF NX > 39 THEN NX = 39
29-
240 IF NY < 0 THEN NY = 0
30-
250 IF NY > 24 THEN NY = 24
31-
260 IF NX = X AND NY = Y THEN GOTO 130
32-
270 POKE SCR + Y*40 + X, 32
33-
280 X = NX : Y = NY
34-
290 POKE SCR + Y*40 + X, 0
35-
300 GOTO 130
36-
22+
150 K$ = UCASE$(K$)
23+
160 K = ASC(K$)
24+
170 IF K = 27 THEN END
25+
180 NX = X : NY = Y
26+
190 IF K = 87 THEN NY = Y - 1
27+
200 IF K = 83 THEN NY = Y + 1
28+
210 IF K = 65 THEN NX = X - 1
29+
220 IF K = 68 THEN NX = X + 1
30+
230 IF NX < 0 THEN NX = 0
31+
240 IF NX > 39 THEN NX = 39
32+
250 IF NY < 0 THEN NY = 0
33+
260 IF NY > 24 THEN NY = 24
34+
270 IF NX = X AND NY = Y THEN GOTO 130
35+
280 POKE SCR + Y*40 + X, 32
36+
290 X = NX : Y = NY
37+
300 POKE SCR + Y*40 + X, 0
38+
310 GOTO 130

examples/gfx_key_demo.bas

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
1 REM GFX KEY DEMO: poll host keyboard via PEEK and move a character
22
2 REM Run with: ./basic-gfx examples/gfx_key_demo.bas
33
3 REM
4-
4 REM Keyboard map (via gfx video peek):
5-
5 REM PEEK(56320 + ASCII) is 1 while that key is held down.
6-
6 REM Supported here: WASD, ESC.
4+
4 REM PEEK(GFX_KEY_BASE + n) reads key_state[n]: 1 while key is held.
5+
5 REM GFX_KEY_BASE is 56320 (0xDC00). For letters, n is ASCII of the
6+
6 REM UPPERCASE key (same as ASC("W") not PETSCII screen code): W=87 A=65 S=83 D=68.
7+
7 REM ESC = 27. This matches Raylib/basic-gfx and canvas wasmGfxKeyIndex().
78
10 SCR = 1024
89
20 COL = 55296
910
30 X = 20
1011
40 Y = 12
1112
50 C = 1
12-
60 REM Clear screen to spaces and set colour to light blue (14)
13-
70 FOR I = 0 TO 999
14-
80 POKE SCR + I, 32
15-
90 POKE COL + I, 14
16-
100 NEXT I
17-
110 REM Draw initial @
18-
120 POKE SCR + Y*40 + X, 0
19-
130 POKE COL + Y*40 + X, C
20-
140 REM Main loop
21-
150 REM ESC (27) to exit, WASD to move
22-
160 IF PEEK(56320+27) <> 0 THEN END
23-
170 NX = X
24-
180 NY = Y
25-
190 IF PEEK(56320+87) <> 0 THEN NY = Y - 1
26-
200 IF PEEK(56320+83) <> 0 THEN NY = Y + 1
27-
210 IF PEEK(56320+65) <> 0 THEN NX = X - 1
28-
220 IF PEEK(56320+68) <> 0 THEN NX = X + 1
29-
230 IF NX < 0 THEN NX = 0
30-
240 IF NX > 39 THEN NX = 39
31-
250 IF NY < 0 THEN NY = 0
32-
260 IF NY > 24 THEN NY = 24
33-
270 IF NX = X AND NY = Y THEN GOTO 320
34-
280 REM Erase old, draw new
35-
290 POKE SCR + Y*40 + X, 32
36-
300 X = NX : Y = NY
37-
310 POKE SCR + Y*40 + X, 0
38-
320 SLEEP 1
39-
330 GOTO 160
40-
13+
60 REM Optional named constants (same numbers as gfx_inkey_demo after UCASE$)
14+
70 REM KW=87 : KA=65 : KS=83 : KD=68 : KESC=27
15+
80 REM Clear screen to spaces and set colour to light blue (14)
16+
90 FOR I = 0 TO 999
17+
100 POKE SCR + I, 32
18+
110 POKE COL + I, 14
19+
120 NEXT I
20+
130 REM Draw initial @
21+
140 POKE SCR + Y*40 + X, 0
22+
150 POKE COL + Y*40 + X, C
23+
160 REM Main loop: ESC to exit, WASD to move (hold key to repeat each SLEEP)
24+
190 IF PEEK(56320+27) <> 0 THEN END
25+
200 NX = X
26+
210 NY = Y
27+
220 IF PEEK(56320+87) <> 0 THEN NY = Y - 1
28+
230 IF PEEK(56320+83) <> 0 THEN NY = Y + 1
29+
240 IF PEEK(56320+65) <> 0 THEN NX = X - 1
30+
250 IF PEEK(56320+68) <> 0 THEN NX = X + 1
31+
260 IF NX < 0 THEN NX = 0
32+
270 IF NX > 39 THEN NX = 39
33+
280 IF NY < 0 THEN NY = 0
34+
290 IF NY > 24 THEN NY = 24
35+
300 IF NX = X AND NY = Y THEN GOTO 340
36+
310 REM Erase old, draw new
37+
320 POKE SCR + Y*40 + X, 32
38+
330 X = NX : Y = NY
39+
340 POKE SCR + Y*40 + X, 0
40+
350 SLEEP 1
41+
360 GOTO 190

0 commit comments

Comments
 (0)