-
Jan 25th, 2009, 07:21 PM
#1
Thread Starter
Stack Overflow moderator
[RESOLVED] [2005] Keyboard input
I need to be able to capture multiple keys. How do I do it?
-
Jan 25th, 2009, 07:27 PM
#2
Re: [2005] Keyboard input
If you press multiple keys at once, multiple KeyDown events will be raised.
-
Jan 25th, 2009, 08:38 PM
#3
Fanatic Member
Re: [2005] Keyboard input
The API call GetKeyboardState lets you read the whole keyboard at once and you can use the vbKeyboard contants to check for a key that does not equal zero,
Code:
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Declare Sub GetKeyboardState Lib "user32" (kbArray As KeyboardBytes)
Private Function KeySrch()
Dim kbbs as KeyboardBytes
DoEvents
GetKeyboardState kbbs
If kbbs.kbByte(vbKeyEscape ) And &H80 Then
End
End If
End Sub
-
Jan 26th, 2009, 07:48 AM
#4
Re: [2005] Keyboard input
Although, the GetAsyncKeyState API is the common standard..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jan 26th, 2009, 10:13 PM
#5
Fanatic Member
Re: [2005] Keyboard input
I like GetKeyboardState because it mirrors the DirectX GetDeviceStateKeyboard. In a tight code loop where you have to read 12 keys for gameplay GetKeyboardState will limit your API calls to 1 while GetAsyncKeyState will require you to surrender your program to windows 12 times.
-
Jan 27th, 2009, 08:32 PM
#6
Re: [2005] Keyboard input
I dont understand what you mean by "surrender", as it sounds like it contradicts the meaning of Asynchronous.
Also, GetKeyboardState requires you to check the hi and low order bits of a byte, which GetAsyncKeyState will do asynchronously anyway..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jan 27th, 2009, 08:35 PM
#7
Re: [2005] Keyboard input
Excuse me, I'm not that into game development, but whats wrong with handling the controls (most likely the form's) KeyDown event?
-
Jan 27th, 2009, 08:41 PM
#8
Re: [2005] Keyboard input
Originally Posted by Atheist
Excuse me, I'm not that into game development, but whats wrong with handling the controls (most likely the form's) KeyDown event?
The KeyDown event pops out the top message on the message queue that involves a key being pressed. Therefore, only one message can be read at a time, and in turn, only one key can be determined at a time.
For example, if a player wants to move diagonally downwards.. theres no way to tell if both the down arrow and a left or right arrow are pressed within the KeyDown event.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jan 27th, 2009, 08:45 PM
#9
Fanatic Member
Re: [2005] Keyboard input
Originally Posted by chemicalNova
I dont understand what you mean by "surrender", as it sounds like it contradicts the meaning of Asynchronous.
Also, GetKeyboardState requires you to check the hi and low order bits of a byte, which GetAsyncKeyState will do asynchronously anyway..
chem
Asynchronous means it's not in a Queue. But the multiple calls to the API are sequential - more overhead from windows before you get all your data. You don't have to read any bits just the abscence of them - thats why I checked for a "true" state. It's the same difference between GetDIBits and GetPixel - chunks are faster.
-
Jan 27th, 2009, 08:48 PM
#10
Re: [2005] Keyboard input
actually it's funny you've asked that Athiest, because directinput is being phased out in favor of built-in windows apis.
Note: You never want to use events in games because they slow the game down. If you were to rapidly hit key presses while in a game loop hooked into the form's keydown event, the speed of the game would actually visibly show down.
getasynckeystate is great and also improperly documented. Supposedly it only reads the key buffer for the current program active, but it in fact doesn't care which program has focus. this is useful for hotkey programs. Getkeyboardstate will NOT do this. Your program must have focus. I don't know what chemicalnova meant by "requires you to check hi and low bits" One of the bits is simply a toggle bit that alternates 1 and 0 depending on whether the key has been pressed since the last check. This is extremely useful if you are not constantly checking. I always just checked to see if it was less than zero as an integer.
-
Jan 27th, 2009, 09:44 PM
#11
Fanatic Member
Re: [2005] Keyboard input
Also, because of it's non-threading nature, getasynckeystate can have the status bits stolen by another program.
-
Jan 27th, 2009, 09:46 PM
#12
Re: [2005] Keyboard input
Well.. if its improperly documented..
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jan 28th, 2009, 01:00 AM
#13
Re: [2005] Keyboard input
Originally Posted by chemicalNova
Well.. if its improperly documented..
chem
i don't know if it's been corrected in the latest sdk, but in the one i have it says it only works when your window has focus but this is not the case.
-
Jan 30th, 2009, 02:10 PM
#14
Thread Starter
Stack Overflow moderator
Re: [2005] Keyboard input
Wow, there's been a lot of posts, yet somehow I haven't noticed...
I'll try the API method, I'll post again later. Who knew you had to do all that, just to get a couple of keys? I'm not sure I understand that API call, is the argument passed ByRef? I use a different version of VB. Do I have to specify ByRef, if so?
-
Jan 30th, 2009, 02:26 PM
#15
Fanatic Member
Re: [2005] Keyboard input
If it's GetKeyboardState your inquiring about, it's byref because the API has to fill the data structure. If it's GetAsyncKeyState the byval because it a value your passint to the API.
This is how you declare.
Code:
Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer
The VB references for keyconstants will work with both.
Here's some references so you can study bit usage:
GetAsyncKeyState:
http://msdn.microsoft.com/en-us/libr...93(VS.85).aspx
GetKeyboardState :
http://msdn.microsoft.com/en-us/libr...99(VS.85).aspx
-
Jan 30th, 2009, 07:24 PM
#16
Re: [2005] Keyboard input
you can either pass getkeyboardstate a byte array or a fixed length string. Works the same either way.
-
Jan 30th, 2009, 07:29 PM
#17
Thread Starter
Stack Overflow moderator
Re: [2005] Keyboard input
And if the integer it returns is more than 0, then the key is pressed?
-
Jan 30th, 2009, 07:41 PM
#18
Thread Starter
Stack Overflow moderator
Re: [2005] Keyboard input
Umm... I tried using GetKeyboardState, and it would have probably crashed my computer if the debugger hadn't caught it first. So I'll try GetAsyncKeyState.
-
Jan 30th, 2009, 07:54 PM
#19
Thread Starter
Stack Overflow moderator
Re: [2005] Keyboard input
OK, thanks! My game works perfectly, now. GetAsyncKeyState is perfect.
Thanks!
RESOLVED
Last edited by minitech; Feb 1st, 2009 at 05:35 PM.
-
Jan 30th, 2009, 08:23 PM
#20
Fanatic Member
Re: [RESOLVED] [2005] Keyboard input
it works and thats all that matters.
-
Jan 31st, 2009, 12:40 PM
#21
Re: [2005] Keyboard input
Originally Posted by minitech
OK, thanks! My game works perfectly, now. GetAsyncKeyboardState is perfect.
Thanks!
RESOLVED
please edit this to getasynckeystate so people searching resolved threads for solutions don't get erronous information.
-
Feb 1st, 2009, 05:35 PM
#22
Thread Starter
Stack Overflow moderator
Re: [RESOLVED] [2005] Keyboard input
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|