Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

GetAsyncKeyState?

I figured out GetKeyState with relative ease. But I think I need GetAsyncKeyState, too and I can't figure out what the return values mean. I think that if the value is less than 0, then the key is being pressed when the call is being made, but other than that I'm lost. Can anyone help?


Comments

  • If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.


    Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.


    Remarks

    The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling


    GetSystemMetrics(SM_SWAPBUTTON)



    which returns TRUE if the mouse buttons have been swapped.


    You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right.


    Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.


    Code Meaning

    VK_LSHIFT VK_RSHIFT

    VK_LCONTROL VK_RCONTROL

    VK_LMENU VK_RMENU




    These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.





  • : If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.


    Thanks, but...


    The least significant bit in the return value for GetAsyncKeyState changes while the key is held down.


    This is the code I used to try and figure this out(it is in a timer event):


    Static KeyState As Integer

    Temp = GetAsyncKeyState(65)

    If Temp <> KeyState Then

    KeyState = Temp

    Text1.SelText = KeyState & Chr$(9) & GetKeyState(65) & vbNewLine

    End If




    This is a list of return values:


    -32767 -128

    0 0

    -32767 -127

    -32768 -127

    0 1

    -32767 -128

    0 0

    -32767 -127

    -32768 -127

    0 1

    -32767 -128

    -32768 -128

    0 0

    -32767 -127

    -32768 -127

    0 1

    -32767 -128

    -32768 -128

    -32767 -128

    -32768 -128

    -32767 -128

    1 0

    0 0




    The toggle of the low-order bit is obvious in both calls. In GetKeyState, it changes when the key is released. In GetAsyncKeyState, it changes while the key is down. The "0" is obvious here, it indicates the key has been released. The neg. values indicate the key is being held. Where does the "1" come from? Why does the low-order toggle while the key is held? Does it indicate the repeat? If it does, then why does it toggle so fast when the repeat is so slow? I can use GetKeyState to monitor keys while my app has the focus, but apparently I need GetAsyncKeyState to monitor at other times.


    Thanks again to anyone who can help!


  • I figured it out.


  • Andre YoungAndre Young USAMember Posts: 0

    _____ | http://forcoder.org | free video tutorials and ebooks about | Go Objective-C R MATLAB Python Swift Assembly Ruby Delphi JavaScript C# C++ PL/SQL Visual Basic Java Visual Basic .NET C Perl Scratch PHP F# Julia ML VBScript Apex LabVIEW Rust ABAP Awk Lua Logo Erlang Crystal Hack COBOL Dart Kotlin Transact-SQL Lisp D SAS Clojure Scala Alice Scheme Fortran Ada Prolog FoxPro Bash | _______

Sign In or Register to comment.