I try to register if a key has been toggled on or off like this:
if (pKeyBuffer[VK_TAB] & 0xFF81)
{
functionA();
}
if (pKeyBuffer[VK_TAB] & 1)
{
functionB();
}
But there is a big problem in the above code – state of the VK_TAB is not known at startup and because of that, sometimes, i have to press Tab one more time at the runtime to set it to the default state.
I cant use regular ‘0xF0’ or ‘0’ because i cant afford having functionA() or functionB() called multiple times. I need switch-like behavior(i.e. if Tab is pressed, even though it is being held down,
functionA() still executes only once).
So is there any way to get key state info to perform that first keystroke? Or maybe you can suggest some alternative approach? Thank you!
In a situation like this you want to use the
GetKeyStatefunction, this will get the key press and key toggle info at the time its called for the given key.so, if you want to check if tab is held down, use
GetKeyState(VK_TAB) & 0x8000, if you want to check is its toggled useGetKeyState(VK_TAB) & 1.