Results 1 to 22 of 22

Thread: [RESOLVED] [2005] Keyboard input

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Resolved [RESOLVED] [2005] Keyboard input

    I need to be able to capture multiple keys. How do I do it?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Keyboard input

    If you press multiple keys at once, multiple KeyDown events will be raised.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    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
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [2005] Keyboard input

    Although, the GetAsyncKeyState API is the common standard..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    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.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [2005] Keyboard input

    Quote 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

  9. #9
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: [2005] Keyboard input

    Quote 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.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    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.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  11. #11
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: [2005] Keyboard input

    Also, because of it's non-threading nature, getasynckeystate can have the status bits stolen by another program.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  12. #12
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [2005] Keyboard input

    Well.. if its improperly documented..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: [2005] Keyboard input

    Quote 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.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  14. #14

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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?

  15. #15
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    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
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  16. #16
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: [2005] Keyboard input

    you can either pass getkeyboardstate a byte array or a fixed length string. Works the same either way.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  17. #17

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Question Re: [2005] Keyboard input

    And if the integer it returns is more than 0, then the key is pressed?

  18. #18

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  19. #19

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking 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.

  20. #20
    Fanatic Member technorobbo's Avatar
    Join Date
    Dec 2008
    Location
    Chicago
    Posts
    864

    Re: [RESOLVED] [2005] Keyboard input

    it works and thats all that matters.
    Have Fun,

    TR
    _____________________________
    Check out my Alpha DogFighter2D Game Demo and Source code. Direct Download:http://home.comcast.net/~technorobbo/Alpha.zip or Read about it in the forum:http://www.vbforums.com/showthread.php?t=551700. Now in 3D!!! http://home.comcast.net/~technorobbo/AlPha3D.zip or read about it in the forum: http://www.vbforums.com/showthread.php?goto=newpost&t=552560 and IChessChat3D internet chess game

  21. #21
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: [2005] Keyboard input

    Quote 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.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  22. #22

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] [2005] Keyboard input

    Whoops! OK, then.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width