This is for any PET/C=64/VIC-20. ?BUTTON returns the key value, or 0 (false) if neither button is pressed. It is limited to just the SPACE and RETURN characters, it should return the key value exactly once, when the key is first pressed, and key repeat is disabled. The key has to be released and pressed again to generate another 13 (or 32).
It should be possible to fit `?BUTTON` in fewer than 38 bytes but I'm too confused at the moment to golf it down. I'm pretty sure this is called "latched input." Please, have at it.
Code:
: foo
begin
?button .
?terminal until ; ok
foo 13 0 0 0 0 0 0 0 0 0 0 32 0 0 0 13 0
0 0 0 0 0 0 32 0 0 0 0 0 13 0 0 0 0 0 0
0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 0 0
0 0 13 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 0
0 0 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0...
my rubbish, below. tested, working, bloated.
Code:
;--------------------------------------------------------------
#if 0
name=?BUTTON
stack=( -- keyval|false )
tags=mmm
Detects a leading edge transition of either spacebar or return
key and leaves either key value on the stack, otherwise [[FALSE]]
LSTX contains the ASCII value of the current keypress
or 255 if no key is pressed
#endif
qbutton
lda LSTX ; peek(151) hex $97 -- Current Key Pressed 255 = No Key
ldy latch
beq testkeys
ldy #0
cmp latch
bne unlatch
testkeys
cmp #13
beq latchit
cmp #32
bne unlatch
latchit
eor latch
beq fail
.byt $24 ; BIT zp
unlatch
tya
sta latch
.byt $24 ; BIT zp
fail
tya
jmp pushya
latch
.byt $0