Date: | November 30, 2004 / year-entry #406 |
Tags: | code |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20041130-00/?p=37173 |
Comments: | 14 |
Summary: | I've seen some confusion over the difference between the GetKeyState function and the GetAsyncKeyState function. GetKeyState returns the virtual key state. In other words, GetKeyState reports the state of the keyboard based on the messages you have retrieved from your input queue. This is not the same as the physical keyboard state: If the user... |
I've seen some confusion over the difference between the
When should you use For user interface work, you nearly always want If you are responding to an input message and want to know what keys were pressed at the time that input was generated, then you want to use Note that if you are implementing a context menu handler, then you shouldn't be using either Given this primer on the difference between [Updated: 1 Dec 2004, minor typo.] |
Comments (14)
Comments are closed. |
And similarly, I see a lot of people using GetCursorPos when they really meant GetMessagePos.
…and why on Windows 95, pressing Shift-Delete to bypass the recycle bin doesn’t work if you release Shift too quickly.
Thanks asdf, I’d never heard of GetMessagePos before.
It sure would have been nice if the GetCursorPos documentation included a link to GetMessagePos, like GetMessagePos has links to GetCursorPos.
I once had a problem with tinysfv, a console CRC32 calculator. In short, it takes names of files on the command line, calculates CRC32 for each and puts them into a crc.sfv file.
Of course, this is done way longer than said; you typically switch to another program while tinysfv is calculating. Then you switch back to check on the status, only to find that it had aborted for no particular reason.
When I looked at the source (thankfully, it comes with sources), I found this:
while (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&message); // Translate keyboard messages DispatchMessage(&message); // Message handling
}
if (GetAsyncKeyState(VK_ESCAPE) & 0x8001)
{
Abort = TRUE;
return 0;
}
Now that was a major WTF to me. A console program, having a message pump, and on top of that, polling for Escape? Whatever happened to good old Ctrl+C? Of course, removing these lines fixed everything.
With regard to context menu handlers, you discuss what to do if you are using IContextMenu, I assume based on the MSDN documentation for it that it is for shell context menus only. I am wondering if there is a correct way to check for keys being held down when implementing context menus in other applications. Right now I am just using GetKeyState().
is it because no message is stuck in the input queue when setting numlock programmatically?
The numlock message is in the queue. That’s the problem.
Typo: "When should use use" should be "When should you use"
What’s a viable use case for GetAsyncKeyState?
Checking for keyboard input in a game loop.
Except then you’d want DirectInput :)
I see a good point here!
bob@abirnet.co.il
DirectInput is overkill.
Not for games, I don’t think. It will (optionally) disable the windows key for you — nobody likes a working windows key when playing a game! Definitely worth the effort of using it, I think, though ‘effort’ is hardly the right word since it’s only about 60 lines to set up the keyboard (less if you support 1 version of DirectX and don’t use the emulated versions) and extremely easy to use once it’s running.