Whats a good way to detect key presses? Using things like if KeyCode = vbKeyDown Then leaves this annoying little delay between pressing the key and doing stuff.
Printable View
Whats a good way to detect key presses? Using things like if KeyCode = vbKeyDown Then leaves this annoying little delay between pressing the key and doing stuff.
I find that the easiest way is to keep an array of booleans, like so:
Just call DoKeys at each iteration of your main loop.Code:Public keys(256) as Boolean
Private Sub Form_KeyDown(keyAscii As Integer)
keys(keyAscii) = True
End Sub
Private Sub Form_KeyUp(keyAscii As Integer)
keys(keyAscii) = False
End Sub
Private Sub DoKeys()
If keys(...) Then
'... do stuff
End If
End Sub
Z.
Where can I find a list of the key's ascii numbers?
there is constants called
vbKeyA for example...
I would rather use getkeystate though...
how do I use getkeystate?
and I get an error when I use only keyAscii as Integer in the parentheses after Private Sub Form_KeyUp and KeyDown.
VB Code:
If GetKeyState(vbKeyA) <> 1 Then Form1.Caption = "Key a pressed"
but that code is not supposed to go into a key down event or something, but needs to be put in a loop!
I have a feeling I did something wrong somewhere... The program starts and it instantly does the code in the GetKeyState thing.
Where is GetKeyState declared, and as what?
There are probably other parameters that I dont remember. The code I posted was off the top of my head.Quote:
Originally posted by Drakon
and I get an error when I use only keyAscii as Integer in the parentheses after Private Sub Form_KeyUp and KeyDown.
Z.
getkeystate will return a non zero value if the key was pressed any time before the previous call to getkeystate. What I usually do is run through a loop of all the keys 1 to 255 with getkeystate when my prog starts, this will then void any key presses prior to program execution and later on during the application.