How to check key pressed (positive transition)

Por albs_br

Champion (499)

imagem de albs_br

21-05-2023, 17:46

Hi guys,

I'm trying to get a key press, but it is triggering multiple times.
The way I think it would solve is by checking if on OLDKEYS it was released before.

This way I should get only one trigger, on positive transition (key pressed).


_READ_KEYBOARD:

    ld      a, (BIOS_NEWKEY + 8)
    bit     7, a
    jp      z, .keyPressed

    ret

.keyPressed:
    ; check if key was previously released
    ld      a, (BIOS_OLDKEY + 8)
    bit     7, a
    ret     z

    ; execute key pressed code here
    call    BIOS_BEEP

    ret

But is not working. Any ideas?

Entrar ou registrar-se para comentar

Por MsxKun

Paragon (1134)

imagem de MsxKun

21-05-2023, 18:27

Don't trust OLDKEY. Save the old key values into RAM by yourself, to be sure.

Try something like this... Returns 0 if not pressed or if at past check the key was also already pressed.

CHECK_KEY: 
   call    .CHK
   or      a
   ret     z
              
   jp      BEEP

.CHK: 
   ld	a,(PRESSED)		   ; saved value before current check, so = old status
   ld	b,a
		
   ld	a,(BIOS_NEWKEY + 8)	; Value stored by BIOS
   cpl
   and	10000000b	   ; Keep only bit 7
   ld	(PRESSED),a		   ; Saves press status

   ld	c,a			           ; readed value again
   and	b			   ; old value
   xor	c			                        
   ret

Por aoineko

Paragon (1138)

imagem de aoineko

22-05-2023, 07:51

OLDKEYS is reset by the BIOS in the interrupt handler so, unfortunately, you can't use it.

Por albs_br

Champion (499)

imagem de albs_br

22-05-2023, 13:44

Thanks for all help guys.

@MsxKun sugestion solved the problem.