This simple example prints the hex value of keys pressed on the terminal, terminates with the return key. Not very well written, but it is one of my first z80 programs for 35 years!
The phex routene does as you ask, but in cpm.
A>type keyval.z80
;
bdos equ 5
org 0100h
;
ld de,str
ld c,9
call bdos
wait ld c,6
ld e,0ffh
call bdos
or a
jr z,wait
cp 13
ret z
call phex
ld de,crlf
ld c,9
call bdos
jr wait
phex ld c,a
rra
rra
rra
rra
call phex2
ld a,c
phex2 and 0fh
add a,090h
daa
adc a,040h
daa
push bc
ld c,2
ld e,a
call bdos
pop bc
ret
;
str db 'Press a key to see it',027h,'s hex value CR to exit'
crlf db 0ah,0dh,'$'