Skip to main content
gamedev.net
🔒 Locked

KeyCodes in VB

Started by capn_midnight 4 replies 450+ views
Original Post
capn_midnight
capn_midnight
how could I know if more than one key is being pressed? my current attempt is to make an array that stores the keycodes, but the problem is that the KeyDown event is called multiple times while holding down the same key. Is there any VB specific way to tell what keys are down, or do I have to continue with my list idea?
dmc
dmc
Instead of storing the keycodes in an array, you could store the status of each key in a boolean array.
The KeyDown event is still called repeatedly, but I don't know of any ways around that...

Let me know if you find a better method!



Dim Keys(3) As Boolean

Private Sub Form_Load()
Form1.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Is = vbKeyW
Keys(0) = True
Case Is = vbKeyA
Keys(1) = True
Case Is = vbKeyS
Keys(2) = True
Case Is = vbKeyD
Keys(3) = True
End Select
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Is = vbKeyW
Keys(0) = False
Case Is = vbKeyA
Keys(1) = False
Case Is = vbKeyS
Keys(2) = False
Case Is = vbKeyD
Keys(3) = False
End Select
End Sub



Edited by - dmc on January 18, 2002 3:06:52 PM

Edited by - dmc on January 18, 2002 3:08:18 PM
ragonastick
ragonastick
Use GetKeyState. Actually, from memory, there was a bit more to it than for me to just say look it up...

You need these two constants:

''Keys
Const KEY_TOGGLED As Long = &H1
Const KEY_PRESSED As Long = &H1000

And this declare:

Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Then you use it like this:

If GetKeyState(vbMyKey) And KEY_PRESSED Then
''MyKey is pressed
End If
If GetKeyState(vbKeyNumLock) And KEY_TOGGLED Then
''NumLock is on
End If

Trying is the first step towards failure.
Trying is the first step towards failure.
Thrump
Thrump
It''s been a while since I programmed in VB. I''ve always thought KeyDown was only done on the way down, and vice versa for KeyUp. KeyPress I thought was the wacky one. Oh well, it''s been a long time.

I would do something similar to dmc (although ragonastick''s idea is good too), but if vbKeyA, vbKeyB are small, define an array booleans for all the keys. It will save you the case statement.

Dim Keys[256] as boolean

Private Sub KeyDown(...)
Keys[keyCode] = TRUE
End

Private Sub KeyUp(...)
Keys[keyCode] = FALSE
End

If the keycodes are big, you can probably subtract a big number off them to transpose them to 0-255
ragonastick
ragonastick
Beware if you use those Key_Up/Key_Down events because they don''t happen if your form doesn''t have focus (from memory at least, I''m not sure if KeyPreview fixes that). Using GetKeyState is a lot simpler and faster.

Trying is the first step towards failure.
Trying is the first step towards failure.
ktuluorion
ktuluorion
Or you can just use DirectInput. It can be used under VB without TOO much hassle, and will do anything you want it to, with nice little examples and tutorials.
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.