The MSVCRT functions have worked fine in this area but here is a simpler Windows API method that seems to work OK as well using GetAsynchKeyState().
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
comment * -----------------------------------------------------
Build this template with
"CONSOLE ASSEMBLE AND LINK"
----------------------------------------------------- *
iskey MACRO vk_key
invoke GetAsyncKeyState,vk_key
not eax
test eax, 00000000000000001000000000000000b
ENDM
.code
start:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
call main
inkey
exit
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
main proc
push esi
invoke GetTickCount
add eax, 5000 ; add 5 seconds for timeout
mov esi, eax
print "Press 1 to 5 or ESCAPE to quit",13,10
; ---------------------------------
stlbl:
invoke SleepEx,1,0 ; yield while idling
invoke GetTickCount
cmp esi, eax ; check timeout threshold
jle timeout
iskey VK_1
je lbl1
iskey VK_2
je lbl2
iskey VK_3
je lbl3
iskey VK_4
je lbl4
iskey VK_5
je lbl5
iskey VK_ESCAPE ; loop back if ESCAPE not pressed
jne stlbl
; ---------------------------------
print "You pressed ESCAPE",13,10
jmp quit
lbl1:
print "You pressed 1",13,10
jmp quit
lbl2:
print "You pressed 2",13,10
jmp quit
lbl3:
print "You pressed 3",13,10
jmp quit
lbl4:
print "You pressed 4",13,10
jmp quit
lbl5:
print "You pressed 5",13,10
jmp quit
timeout:
print "Loop timed out",13,10
quit:
pop esi
ret
main endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
end start
hutch,
can you explain why you are using the NOT operator & testing the bit you are because when I use GetAsyncKeyState I test the Hi bit of eax to see if a key is down & the Lo bit to see if it is up, I,m a little puzzled?
Neil,
I used the NOT mnemonic to invert the required conditional jump so that the semantics matched the logic.
iskey VK_1 ; if the pressed key = VK_1
je lbl1 ; jump to label
Without the NOT mnemonic you must use the inverse jump "jne" which is counter intuitive to code.
The bit I am testing handles a key held down where you have a repeat of the key.
Thanks hutch,
Got it :thumbu
Hi Hutch,
Thanks for this example :U