Read All About it!
AceFORTH allows you to enter information from the
keyboard using INKEY, QUERY and RETYPE. The INKEY
word is the easiest to use as it reads the keyboard and leaves
its ASCII code on the stack, but if you are pressing several keys it leaves 0 on the stack. There are many ways of using the
INKEY word to test for keys pressed, but its only good for reading one character at a time.
QUERY and
RETYPE use the input buffer.
QUERY clears the input buffer
and then lets you enter characters.
RETYTE keeps the characters in the input buffer which gives you a chance to edit them.
More information on these words can be found in the
user manual, with the
examples.
These AceFORTH
words are useful but for but for some applications - for
example, when combinations of keys must be
read simultaneously - the
IN function can be
used to read the keyboard directly.
The keys on the Jupiter Ace are connected to the
Z80 microprocessor through input/output ports.
There are 65536 input/output ports, and each
one can be addressed individually. In the same
way that @ and ! are used to read or write
to memory, IN and OUT are used to read and write
to input/output ports.
The word IN ( add - c) will take a port address from the top of the stack (ToS) and
return the value of a port to the ToS, 65278 IN would result with a value from port 65278 on the ToS. while the forth word
OUT ( c add -) will write the value c to port add from the stack, 20 65278 OUT
will remove 20 and 65278 from the stack and output the value 20 to port 65278.
The keyboard consists of four rows of 10 keys,
each row being divided into two half-rows of five
keys. Each half-row maps onto a port as shown in the image below, port addresses in blue.
Notice that the keys in the left-hand half-rows
map to their ports from right to left, but that the
right-hand half-rows map left to right.
The rightmost five bits of a port each map onto
a key, and will take the value 0 if the
corresponding key is pressed, or 1 if it is not.
The bits marked X are unspecified (in yellow in the image) - they can hold
either a one or a zero. This means that the value
of left on the stack will be undefined. This problem can be
overcome by setting the value of the top three bits
to zero in software by using 31 AND.
Please note: That on the Ace keyboard the Symbol shift key is next to the SPACE key the above image
is an address port map of where the key ports are mapped.
For example: if the letter c is pressed port 65278 will contain :
XXX11110 = 15
If both SHIFT and the letter c is pressed port 65278 will contain:
XXX01110 = 14 .
If no keys are pressed the value is 31 = XXX11111
Try this demonstration program,
keydemo , and press different keys also multiple key presses to see how the port bits change.
The system variable
KEYCOD 3C26 (
15398) is read to
EMIT the ASCII code of the last key pressed, note that
KEYCOD only stores 1 byte, so multiple key press ASCII codes are not stored.
( Key demo listing JAAT 2009 )
( to run type GO )
: msg1 0 0 AT ." Multi Key press demo JAAT 2009";
: msg2 2 0 AT ." Port Value 31 AND Bits";
: BIN 2 BASE C! . DECIMAL ;
(( c c - ) print ToS value as binary)
( Test for keys 1 to 5)
: row1left 6 0 AT ." 63486" 2 SPACES
63486 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys 6 to 0)
: row1right 7 0 AT ." 61438" 2 SPACES
61438 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys q to t)
: row2left 8 0 AT ." 64510" 2 SPACES
64510 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys y to p)
: row2right 9 0 AT ." 57342" 2 SPACES
57342 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys a to g)
: row3left 10 0 AT ." 65022" 2 SPACES
65022 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys h to enter)
: row3right 11 0 AT ." 49150" 2 SPACES
49150 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys Sym shift & shift to c)
: row4left 12 0 AT ." 65278" 2 SPACES
65278 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( Test for keys b to space)
: row4right 13 0 AT ." 32766" 2 SPACES
32766 IN DUP . SPACE ." =" 4 SPACES
31 AND DUP . 4 SPACES BIN
;
( prints ASCII code of last key pressed)
: KEYCOD 18 0 AT ." Last key pressed ="
SPACE
15398 @ EMIT
;
: GO INVIS CLS
msg1 msg2
BEGIN
row1left
row1right
row2left
row2right
row3left
row3right
row4left
row4right
0
until
;
In the above screen shot a single z key has been pressed, in the second screen shot keys 3,e,d and z are being pressed simultaneously.
There is also a routine that
Garry Knight uses in his ALIENS game ( which is
MIA).
Garry's routine uses machine code and details can be found in
Ace User Issue 3, page 12