
The Turbo course read a single key with Read(Kbd, Ch), which waited for one, and tested whether one was waiting with KeyPressed. HiSoft Pascal has one function that does both jobs - and a catch, which is the reason S5 spent so long on (*$C-*).
INCH looks at the keyboard and returns at once: the key being pressed, if there is one, or CHR(0) if not. It does not wait and it does not echo the key on the screen. So:
C:=INCH;
IF C<>CHR(0) THEN ...
is Turbo's if KeyPressed then Read(Kbd, Ch), and
REPEAT C:=INCH UNTIL C<>CHR(0);
is Turbo's Read(Kbd, Ch): wait for a key.
The special keys send what they sent in Turbo:
| Key | INCH gives |
|---|---|
| the ↑ key (Up in MAME) | ^ (94) |
| the vertical cursor key (Down in MAME) | CHR(10) |
| the ← key (Left in MAME) | [ (91) |
| the → key (Right in MAME) | ] (93) |
| ENTER | CHR(13) |
| ESC | CHR(27) |
Letters come as the keyboard types them - capitals, while CAPS LOCK is on.
Under the compiler's normal setting, the CTRL-C check in every loop (S5) looks at the keyboard too - and a key it sees there, INCH does not. Keys go missing, and some of them turn up printed on the screen. Every program that uses INCH needs (*$C-*) as its first line.
And then CTRL-C no longer stops it. So give the program its own way out - a key it watches for, like Q below - and save it before you run it the first time.
INCH itself is not fast: about 400 calls a second. A game loop that calls it once a pass will not notice; a loop that calls it many times will.
A * in the middle of the screen, moved by the three keys marked with arrows and the vertical cursor key:
10 (*$C-*)
20 PROGRAM MOVER;
30 VAR C:CHAR; X,Y:INTEGER;
40 PROCEDURE GOTOXY(X,Y:INTEGER);
50 BEGIN
60 WRITE(CHR(29),CHR(X-1),CHR(Y-1))
70 END;
80 BEGIN
90 PAGE;
100 WRITE('ARROWS MOVE, Q STOPS');
110 X:=20; Y:=12;
120 REPEAT
130 GOTOXY(X,Y); WRITE('*');
140 REPEAT C:=INCH UNTIL C<>CHR(0);
150 GOTOXY(X,Y); WRITE(' ');
160 IF (C='[') AND (X>1) THEN X:=X-1;
170 IF (C=']') AND (X<40) THEN X:=X+1;
180 IF (C='^') AND (Y>2) THEN Y:=Y-1;
190 IF (C=CHR(10)) AND (Y<23) THEN Y:=Y+1
200 UNTIL C='Q';
210 GOTOXY(1,23)
220 END.
In XBAS the [, ] and ^ in lines 160 to 180 are typed with the ← → and ↑ keys, the very keys the program will be watching for. The fourth key, CHR(10), is the Einstein's vertical cursor key, the grey one at the end of the QWERTY row. In MAME it is your Down arrow, and the other three are your Left, Right and Up arrows.
Starting from
MOVER.ASC saved from XBAS; HPEIN MOVER.
What you should see
A clear screen, the instructions at the top, and a * in the middle that moves one place for each of the four keys and stops at the edges. CTRL-C does nothing. Q ends it.
Q.. instead of a space. What does the * leave behind?15.1 Count key presses: show the number of keys pressed so far in the top right corner, and stop on Q.
15.2 Make the * keep moving by itself in the last direction chosen, one place every few hundred passes of a loop, turning when one of the four keys is pressed.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
Keys are ignored, or appear on the screen as ] and ^ | No (*$C-*): the CTRL-C check is taking them. |
| CTRL-C will not stop the program | (*$C-*) turned it off. Give the program its own key to stop on. |
| A letter key is not recognised | It came as a capital (or a small letter). Test for both, or keep CAPS LOCK on. |
INCH returns the key being pressed, or CHR(0), without waiting or echoing - KeyPressed and Read(Kbd, Ch) in one. The ← → ↑ keys give [, ] and ^, and the vertical cursor key CHR(10). A program using INCH needs (*$C-*), and so needs its own way to stop.
S16, Chance - RANDOM, which needs no Randomize.
This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.