Reading Keypresses from Port
z80 » Advanced
call _getkeyis that it only gives the value of the key last pressed and only that key. If you had  and [ENTER] pressed at the same time, if would return 0 in
a. That's not good sometimes.
Checking KeysAnother way of getting the keypresses is to read them directly from the keyport. You can read later about All the Ports and how the z80 uses them. They are numbered with the keyport being number 1.) We kinda need to send a byte out through the port and then wait a second before reading what was returned.
We use the chart below
to determine the values returned. The byte sent is a bit mask and is
to the left of the chart in the respective row to the key you want to check
for. If you wanted to check for the [ALPHA] being pressed, you would send
%10111111 through the port, get it back, and check if bit 7 were set
(not being pressed now) or reset (being pressed now).
This is only to detect what keys are being held down currently whereas GetKey checked what the last key pressed was.
Let's make a loop that won't exit unless [ALPHA] is pressed. The program will then branch (jump) if so.
key_port =1 ;number of key's port check_for_alpha: ld a,%11011111 ;load the bit mask so port 1 knows ; which row to check from ; table out (key_port),a ;send out the bit mask nop ;no operation so there's some time nop ; to get a result back* in a,(key_port) ;get back the results bit 7,a ;[alpha]'s position jr z,alpha_held_down_now jr check_for_alpha
* Most people say you need these nop's...but in my experience they don't do jack.
You can download the MultiKey.asm key-test program to see how to use this with many other keys.
Bitmask ChartWhenever you start using the key port, have this chart handy. It tells you the request value (bit mask) for the row of keys you want to check. Upon return, check the certain key's bit to see if it is set (not pressed now) or reset (pressed now).
One thing to think about in games is to clear the key buffer right before exiting your program. This way, if your program was run from a shell, it won't exit the shell too.
More from z80 » Advanced
All The Ports // APD // Assembler Directives // Entry Stack // User Fonts // IM 1 // IM 2 // Index, Shadow, and Other Registers // User Interrupt // Morphic Code // On-Off // Reading Keypresses from Port // Shift and Rotate // Simulating Key Presses // Sound // Square-Root Programs // System Flags of TI-OS