Keyboard buffering

Basic and Machine Language

Moderator: Moderators

Post Reply
edinky
Vic 20 Newbie
Posts: 18
Joined: Sat Apr 07, 2018 10:34 am

Keyboard buffering

Post by edinky »

hi,

I've been putting more and more stuff in to an update loop, and its got to the point where its hard for the program to pick up a key input.

My update looks like this, in which I'm updating game states in between updating drawing guages and calling kc=peek(197) to look for key input, hoping enough calls to peek(197) will catch an input. i've even put more in some of the BG updates to try and catch the input.

there's probably a better way of doing this but i cant seem to find a call for looking for the "latest" key input instead? any ideas?

Code: Select all

1050 rem update loop 
1055 sys(4609):rem draw guages
1058 nb$=n$: rem notification buffer
1060 kb=peek(197): rem buffer current input state
1065 gosub 1125:sys(4609): kc = peek(197): rem buffer vitals = power/water/food/habitat
1070 gosub 2500:sys(4609) : kc = peek(197): rem process changes
1080 kc = peek(197): gosub 2600: kc = peek(197): rem check for input, BG tasks, input check
1081 gosub 1110:kc = peek(197): rem update vital graph if changed
1083 gosub 1090
1084 if n$<>nb$ then gosub 1130
1085 gosub 1090
1086 goto 1050
1090 if kb<>kc then POKE36878,15: POKE36876,230: POKE36876,0: gosub 2260: rem key was pressed, goto input
1100 return
User avatar
Mike
Herr VC
Posts: 4976
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Keyboard buffering

Post by Mike »

PEEK(197) returns the keyboard matrix number of a key and is, as you've noticed, unbuffered.

For buffered keyboard input, GET <variable> is the preferred way to go. GET returns PETSCII characters, though - SHIFT, CTRL, C= on their own will not register. Neither will the RESTORE key, but that one doesn't work with PEEK(197) either, as it is processed as NMI and is not part of the keyboard matrix.

Note: GET should only be used with a string variable. If you use a numeric variable instead, GET stops the running program with a ?SYNTAX error as soon as something else than 0..9/./+/-/E is input.

For an example, take a look at the main loop of my TRON Light Cycles game:

Code: Select all

[...]
9 GETA$
10 IFA$="C"THENC=0
11 IFA$="{F1}"THENC=1
12 IFA$="Z"THENC=2
13 IFA$="{F7}"THENC=3
14 POKEV+11,140:POKEB,58:POKEB+CO,6:B=B+D(C):IFPEEK(B)<>219THENLI=LI-1:GOTO20
15 POKEB,59+C:SC=SC+1:NL=NL-1:PRINT"{HOME,BLK,7 RIGHT,RVS ON}"SC:POKEV+11,0
16 IFNL<0THENNL=NL+1000:LI=LI+1:GOSUB40
17 IFPEEK(G+D(H))<>219ORRND(1)<.1THENI=C:GOSUB41
18 POKEG,58:POKEG+CO,5:G=G+D(H):IFPEEK(G)<>219THENSC=SC+100:NL=NL-100:B=G:GOTO20
19 POKEG,59+H:GOTO9
[...]
Post Reply