CPCWiki forum

General Category => Programming => Topic started by: Fran123 on 10:42, 06 April 21

Title: reading key
Post by: Fran123 on 10:42, 06 April 21

Hello,


I'm trying to read the pressed keys. I have already visit some pages how to do it, but I haven't found what I want.


I read first I need is getting the keyboard status and then check if a key is pressed or not.
The next code scan each line to find pressed keys. The read code is stored the array


I press "2" or "3" the code is 00000011, the same!


How I can distinguish each key?


Thank you.



org #3000


PrintChar equ &bb5a
Keyboard_buffer_len equ 10




inicio:
   di ; disable interrupts


   ld hl, keyboard_status
   ld bc, #f782 ;; select ppi port direction: A out, C out
   out (c), c
   ld bc, #f40e
   ld e, b
   out (c), c
   ld bc, #f6c0
   ld d, b
   out (c), c
   xor a      ;
   out (c), a ; out (c), 0
   ld bc, #f792
   out (c), c
   ;
   ld a, #40 ; line to read , linea a leer, va de #40 a #49
   ld c, #4a ; max linea


keyb_scan_loop:
   ld b, d ; b=#f6
   out (c), a ; read from line, seleccionar linea
   ld b, e ; b=#f4
   ini ;; read datum is stored in (hl), hl++ ;lo leido de la linea se guarda en (HL), hl++
   inc a ; next line, siguiente linea
   cp c ; a==c?
   jp nz, keyb_scan_loop ; if not equals goto, si distinto goto


   ; restores
   ld bc, #f782
   out (c), c
   ei ; enable interrupts


   call PrintBuffer
   call NewLine
   
jp inicio






;A
PrintByte:
ld b, a
ld c, #80 ;  mascara 1000.0000
_ini:
ld a, b
and c
cp c
jr nz, _no_iguales
_iguales:
ld a, '1'
jr _endif
_no_iguales:
ld a, '0'
_endif
call PrintChar
or a ; carry=0
rr c
ld a, c
cp a, #00
jr nz, _ini


ld a, ' '
call PrintChar


ret



; touches hl, c, a

PrintBuffer:
ld hl, keyboard_status
ld c, Keyboard_buffer_len
_otro_byte
ld a, (hl) ; byte a imprimir
cp #ff ; es #ff?
jr z, _no_print ; si es distinto ir a _no_print
push bc
neg
call PrintByte
pop bc
_no_print
dec c ; c--
ret z ; si c==0 ret
inc hl ; hl apunta al sig byte
jr _otro_byte ; procesar sig byte
ret



NewLine:
   ld a, 13
   call PrintChar
   ld a, 10
   call PrintChar
   ret



keyboard_status:
   db #ff,#ff, #ff,#ff, #ff,#ff, #ff,#ff, #ff,#ff,   #ff






Title: Re: reading key
Post by: Nesu on 00:54, 11 April 21
To detect specific keys you also need to use the keyboard line number:

org #3000
run #3000


PrintChar equ &bb5a
Keyboard_buffer_len equ 10


inicio:
   di ; disable interrupts


   ld hl, keyboard_status
   ld bc, #f782 ;; select ppi port direction: A out, C out
   out (c), c
   ld bc, #f40e
   ld e, b
   out (c), c
   ld bc, #f6c0
   ld d, b
   out (c), c
   xor a      ;
   out (c), a ; out (c), 0
   ld bc, #f792
   out (c), c
   ;
   ld a, #40 ; line to read , linea a leer, va de #40 a #49
   ld c, #4a ; max linea


keyb_scan_loop:
   ld b, d ; b=#f6
   out (c), a ; read from line, seleccionar linea
   ld b, e ; b=#f4
   ini ;; read datum is stored in (hl), hl++ ;lo leido de la linea se guarda en (HL), hl++
   inc a ; next line, siguiente linea
   cp c ; a==c?
   jp nz, keyb_scan_loop ; if not equals goto, si distinto goto


   ; restores
   ld bc, #f782
   out (c), c
   ei ; enable interrupts


   call PrintBuffer
   call NewLine
   
jp inicio



;A
PrintByte:
    dec a
    ld b, a
    ld c, #80 ;  mascara 1000.0000
_ini:
    ld a, b
    and c
    cp c
    jr nz, _no_iguales
_iguales:
    ld a, '1'
    jr _endif
_no_iguales:
    ld a, '0'
_endif
    call PrintChar
    or a ; carry=0
    rr c
    ld a, c
    cp a, #00
    jr nz, _ini

    ld a, ' '
    call PrintChar

    ret

; touches hl, c, a

PrintBuffer:
    ld hl, keyboard_status
    ld c, Keyboard_buffer_len
_otro_byte
    ld a, (hl) ; byte a imprimir
    cp #ff ; es #ff?
    jr z, _no_print ; si es distinto ir a _no_print
    push bc
    neg
    call PrintByte
    pop bc
    push bc  ;;  ---------- PRINT KEYBOARD LINE
    ld a, 58
    sub c
    call PrintChar 
    ld a, ' '
    call PrintChar
    pop bc ;;  ------------------------------
   
_no_print
    dec c ; c--
    ret z ; si c==0 ret
    inc hl ; hl apunta al sig byte
    jr _otro_byte ; procesar sig byte
    ret



NewLine:
   ld a, 13
   call PrintChar
   ld a, 10
   call PrintChar
   ret



keyboard_status:
   db #ff,#ff, #ff,#ff, #ff,#ff, #ff,#ff, #ff,#ff,   #ff


Look at a keyboard values table for more info (i.e:  Table 2 (http://lronaldo.github.io/cpctelera/files/keyboard/keyboard-txt.html))
Title: Re: reading key
Post by: Nemo59 on 14:17, 16 April 21
why dont use the firmware ($bb1e)? It works fine and not sure it's very slower... and so easy ;D


Firmware guide :
Quote
KM TEST KEY #BB1E
Test if a key is pressed.
Action:
Test if a particular key or joystick button is pressed. This is done using the key
state map rather than by accessing the keyboard hardware.
Entry conditions:
A contains a key number.
Exit conditions:
If the key is pressed:
Zero false.
If the key is not pressed:
Zero true.
Always:
Carry false.
C contains the current shift and control state.
A, HL and other flags corrupt.
All other registers preserved.
Powered by SMFPacks Menu Editor Mod