I would like to have a boolean event toggle when a key is pressed. Specifically, the \'s\' key. I have been pointed to the function GetKeyState(), which supposedly works und
From what I understand you need to do:
if( GetKeyState(115) & 0x8000 )
{
<EVENT>
}
The highest bit tells if key is pressed. The lowest tells if key is toggled (like, if caps lock is turned on).
Bit late for this but the high order bit is 0x80000000 not 0x8000, change this and it will work fine.
The other bit - useful for nothing - it acts as if you pressed CAPS LOCK when you pressed LSHIFT.
Since SHORT is signed, high-order bit equals sign bit.
Therefore to test if a given key is pressed, simply test if the value returned by GetKeyState()
is negative:
if (GetKeyState('S') < 0) {
// The S key is down.
} else {
// The S key is up.
}
Besides, 115 is ASCII code for 's'. I believe, you should use capital case 83 to test the 'S' key.
I use a global variable bool altgr
Example:
void Editor::HandleKey(char car) {
bool shift = false;
//bool altgr = false;
bool printable = false;
if (car == 27) SendMessage(hwnd, WM_DESTROY, 0, 0);
if ((GetKeyState(VK_CAPITAL) & 0x0001) == 1) shift = true;
if ((GetKeyState(VK_SHIFT) & 0x8000) == 0x8000) shift = true;
// if(GetKeyState(VK_RMENU) & 0x80000000 == 0x80000000) altgr = true;
if (car == 18) altgr = true;
Sometimes you want to use a combination of keys.
To avoid the situations when a combination of keys (eg: VK_SHIFT && VK_LEFT) satisfies two conditions:
std::cout << "Shift RIGHT pressed" << std::endl;
std::cout << "LEFT key pressed" << std::endl;
just use Sleep(...);
and GetAsyncKeyState(VK_...)
GetKeyState() vs. GetAsyncKeyState() vs. getch()?
#include <windows.h>
...
while (1)
{
if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_LEFT) & 0x8000))
{
Sleep(200);
std::cout << "Shift LEFT pressed" << std::endl;
}
if ((GetKeyState(VK_SHIFT) & 0x8000) && (GetAsyncKeyState(VK_RIGHT) & 0x8000))
{
Sleep(200);
std::cout << "Shift RIGHT pressed" << std::endl;
}
if (GetAsyncKeyState(VK_RIGHT))
{
std::cout << "RIGHT key pressed" << std::endl;
}
if (GetAsyncKeyState(VK_LEFT))
{
std::cout << "LEFT key pressed" << std::endl;
}
}