GetAsyncKeyState() and GetKeyState() [Solved]
-
I think the hint is in the name: you should call
GetKeyState
after a Keyboard message has been sent to your thread (MSDN [^]: "An application calls GetKeyState in response to a keyboard-input message."). On the other hand, you may callGetASyncKeyState
whenever you need it. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Ok, get it but:
CPallini wrote:
On the other hand, you may call GetASyncKeyState whenever you need it.
What does it give back then: The current state or the state it had after the last keyboard message or ...?
According to MSDN:
The key status returned from this function (GetKeyState) changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
"The status does not reflect the interrupt-level state associated with the hardware." What's this? (i do know the concept of interrupts) Is it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?
-
Ok, get it but:
CPallini wrote:
On the other hand, you may call GetASyncKeyState whenever you need it.
What does it give back then: The current state or the state it had after the last keyboard message or ...?
According to MSDN:
The key status returned from this function (GetKeyState) changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
"The status does not reflect the interrupt-level state associated with the hardware." What's this? (i do know the concept of interrupts) Is it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?
GetAsyncKeyState gives the current state.
Rozis wrote:
s it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?
Yes, it maybe outdated. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
GetAsyncKeyState gives the current state.
Rozis wrote:
s it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?
Yes, it maybe outdated. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
GetAsyncKeyState gives the current state.
OK. What I learned so far (correct me please if i'm wrong): GetKeyState() returns if a key is pressed based on the current status of the messagequeue. The status of the messagequeue is formed by the keyboard messages sent and processed to this queue. GetAsyncKeyState(), on the other hand, returns if a key is pressed based on the current status of the keyboard hardware. In most cases both functions have the same outcome. There are some differences: 1) Synthesizing a keypress would work when GetKeyState() was used but not when GetAsyncKeyState() was used. - Am i right? And what about keyboard hooks? 2) As Adam Roderick J 09 points out GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values. GetKeyState buffers these values. GetAsyncKeyState gives you only if these keys are currently pressed or not, not if they are on or off. Avi Berger gave this link: http://blogs.msdn.com/oldnewthing/archive/2004/11/30/272262.aspx[^]
modified on Sunday, March 21, 2010 5:01 PM
-
Can anyone give me the precise difference between GetAsyncKeyState(vk_shift) and GetKeyState(vk_shift) ? I can and did read the explainment of MS, but i guess i still don't get it. In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed. Should i use GetKeyState() instead and if yes, why? And could you give me an example of how to check the shift key with that one. If no, where is GetKeyState() then good for? Can you give an example of its use? Thanks
Just go throught my new article, that will help you clear with all the doubts regarding the ose APIs Mouse and KeyBoard Hooking utility with VC++.
Величие не Бога может быть недооценена.
-
Just go throught my new article, that will help you clear with all the doubts regarding the ose APIs Mouse and KeyBoard Hooking utility with VC++.
Величие не Бога может быть недооценена.
-
Can anyone give me the precise difference between GetAsyncKeyState(vk_shift) and GetKeyState(vk_shift) ? I can and did read the explainment of MS, but i guess i still don't get it. In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed. Should i use GetKeyState() instead and if yes, why? And could you give me an example of how to check the shift key with that one. If no, where is GetKeyState() then good for? Can you give an example of its use? Thanks
Rozis wrote:
In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed.
In most cases you want to use GetKeyState() and not GetAsyncKeyState(). If the shift-key was pressed when? Are you processing a particular keystroke? (Such as in an OnKeyDown handler?) GetKeyState() tells you the state at the time of the event that you are processing. GetAsyncKeyState() tells you the state at the instant that you call it. This is not coordinated with where your program is in processing its input stream. For a longer explanation, see here[^].
Please do not read this signature.
-
Then it must be a peace of cake for you to point me out the precise difference between GetAsyncKeyState() and GetKeyState() ?
GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed). Above explanation as per msdn. Now the case of SHIFT button applied to these cases to a normal application as example. Now if you click the SHIFT key before calling this API in your application then SHORT nVirtAsyc = GetAsyncKeyState(VK_LSHIFT); SHORT nVirtSync = GetKeyState(VK_LSHIFT); Async = 0 Sync = 1; Now again you click SHIFT key Aync = 0 Sync = 0 Now i think you got the point, GetKeyState gives the toggle values. While GetAyncKeyState will be 1 when you have Pressed the SHIFT button. Not the released button. GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values
Величие не Бога может быть недооценена.
modified on Sunday, March 21, 2010 12:35 PM
-
GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed). Above explanation as per msdn. Now the case of SHIFT button applied to these cases to a normal application as example. Now if you click the SHIFT key before calling this API in your application then SHORT nVirtAsyc = GetAsyncKeyState(VK_LSHIFT); SHORT nVirtSync = GetKeyState(VK_LSHIFT); Async = 0 Sync = 1; Now again you click SHIFT key Aync = 0 Sync = 0 Now i think you got the point, GetKeyState gives the toggle values. While GetAyncKeyState will be 1 when you have Pressed the SHIFT button. Not the released button. GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values
Величие не Бога может быть недооценена.
modified on Sunday, March 21, 2010 12:35 PM
Adam Roderick J 09 wrote:
GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values
Of course you're right: there's no (practical) way to get vk_numlock with GetAsyncKeyState. I didn't thought of that - Good answer! Does this also mean that a synthesized keystroke (with SendInput() or sending keyboard messages) will not be handled with GetAsyncKeyState() ?
-
Rozis wrote:
In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed.
In most cases you want to use GetKeyState() and not GetAsyncKeyState(). If the shift-key was pressed when? Are you processing a particular keystroke? (Such as in an OnKeyDown handler?) GetKeyState() tells you the state at the time of the event that you are processing. GetAsyncKeyState() tells you the state at the instant that you call it. This is not coordinated with where your program is in processing its input stream. For a longer explanation, see here[^].
Please do not read this signature.
-
Adam Roderick J 09 wrote:
GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values
Of course you're right: there's no (practical) way to get vk_numlock with GetAsyncKeyState. I didn't thought of that - Good answer! Does this also mean that a synthesized keystroke (with SendInput() or sending keyboard messages) will not be handled with GetAsyncKeyState() ?
why not? Just check with a sample code man :)
Величие не Бога может быть недооценена.