Код:2BF KEYBOARD CALL 028E,KEY-SCAN Fetch a key value in the DE
RET NZ register pair but return immedi-
ately if the zero pair flag is reset.
A double system of 'KSTATE system variables' (KSTATE0 - KSTATE 3 and KSTATE4 - KSTATE7) is used from now on.
The two sets allow for the detection of a new key being pressed (using one set) whilst still within the 'repeat period' of the previous key to have been pressed (details in the other set).
A set will only become free to handle a new key if the key is held down for about 1/10 th. of a second. i.e. Five calls to KEYBOARD.
LD HL,KSTATE0 Start with KSTATE0.
02C6 K-ST-LOOP BIT 7,(HL) Jump forward if a 'set is free';
JR NZ,02D1,K-CH-SET i.e. KSTATE0/4 holds +FF.
INC HL However if the set is not free
DEC (HL) decrease its '5 call counter'
DEC HL and when it reaches zero signal
JR NZ,02D1,K-CH-SET the set as free.
LD (HL),+FF
After considering the first set change the pointer and consider the second set.
02D1 K-CH-SET LD A,L Fetch the low byte of the
LD HL,KSTATE4 address and jump back if the
CP L second set has still to be
JR NZ,02C6,K-ST-LOOP considered.
Return now if the key value indicates 'no-key' or a shift key only.
CALL 031E,K-TEST Make the necessary tests and
RET NC return if needed. Also change
the key value to a 'main code'.
A key stroke that is being repeated (held down) is now separated from a new key stroke.
LD HL,KSTATE0 Look first at KSTATE0.
CP (HL) Jump forward if the codes
JR Z,0310,K-REPEAT match - indicating a repeat.
EX DE,HL Save the address of KSTATE0.
LD HL,KSTATE4 Now look at KSTATE4.
CP (HL) Jump forward if the codes
JR Z,0310,K-REPEAT match - indicating a repeat.
But a new key will not be accepted unless one of the sets of KSTATE system variables is 'free'.
BIT 7,(HL) Consider the second set.
JR NZ,02F1,K-NEW Jump forward if 'free'.
EX DE,HL Now consider the first set.
BIT 7,(HL) Continue if the set is 'free' but
RET Z exit from the KEYBOARD
subroutine if not.
The new key is to be accepted. But before the system variable LAST-K can be filled, the KSTATE system variables, of the set being used, have to be initialised to handle any repeats and the key's code has to be decoded.
02F1 K-NEW LD E,A The code is passed to the
LD (HL),A E register and to KSTATE0/4.
INC HL The '5 call counter' for this
LD (HL),+05 set is reset to '5'.
INC HL The third system variable of
LD A,(REPDEL) the set holds the REPDEL value
LD (HL),A (normally 0.7 secs.).
INC HL Point to KSTATE3/7.
The decoding of a 'main code' depends upon the present state of MODE, bit 3 of FLAGS and the 'shift byte'.
LD C,(MODE) Fetch MODE.
LD D,(FLAGS) Fetch FLAGS.
PUSH HL Save the pointer whilst the
CALL 0333,K-DECODE 'main code' is decoded.
POP HL
LD (HL),A The final code value is saved in
KSTATE3/7; from where it is
collected in case of a repeat.
The next three instruction lines are common to the handling of both 'new keys' and 'repeat keys'.
0308 K-END LD (LAST-K),A Enter the final code value into
SET 5,(FLAGS) LAST-K and signal 'a new key'.
RET Finally return.

