A problem when using GetKeyState()
Rer
80
Reputation points
I wrote a function to catch long press but it only works outside the message loop. In my WinProc(), the message queue seems to be congested as the console keeps printing the character (in while loop #2) after I release the key. (It has detected the long press action indeed but GetKeyState() keeps telling me the key is down all the time in loop #2)
#define KeyPressed(nVirKey) (bool)(GetKeyState(nVirKey) & 0x8000)
int getLongPressed(char ch, clock_t DAS, clock_t ARR)
{
cout << "Test for long press of " << ch << "..." << endl;
cout << KeyPressed(ch) << endl;
bool longPressed = false;
clock_t clk = clock();
while(KeyPressed(ch))
{
if((clock() - clk) > DAS)
{
longPressed = true;
break;
}
}
if(longPressed)
{
clk = clock();
while(KeyPressed(ch))
{
if((clock() - clk) > ARR)
{
cout << ch << " ";
clk = clock();
}
}
}
cout << endl << "End GetLongPressed()..." << endl;
return longPressed;
}
Sign in to answer