1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
//test if a key\combination keys is pressed
bool AreAllKeysPressed(const std::vector<int> &keys)
{
int state = 0x8000;
for (int key:keys)
{
state &= GetAsyncKeyState(key);
}
return (state & 0x8000) != 0;
}
bool CombineKeys(std::vector<std::vector<int>> const &keys)
{
static bool PreviousKeyPressed=false;
static DWORD StartTimer = GetTickCount();
static int i;
//test if the 1st key\combination key is pressed
if((AreAllKeysPressed(keys[0])==true) && PreviousKeyPressed==false)
{
i=0;
PreviousKeyPressed=true;
StartTimer = GetTickCount();
i++;
}
//the user only have 2 seconds for press the next key\combination key
else if (GetTickCount() - StartTimer >= 2000)//now i put the timer here ;)
{
StartTimer =GetTickCount();
PreviousKeyPressed=false;
i=0;
}
//test if the previous key\combination keys was pressed
//and the last key\combination keys
else if((i==(int)keys.size()-1) && (AreAllKeysPressed(keys[(int)keys.size()-1])==true) && PreviousKeyPressed==true)
{
PreviousKeyPressed=false;
i=0;
StartTimer=0;
return true;
}
//test if the previous key\combination keys was pressed
//and the next key\combination keys
else if((AreAllKeysPressed(keys[i])==true) && PreviousKeyPressed==true)
{
PreviousKeyPressed=true;
StartTimer = GetTickCount();
i++;
}
else
{
PreviousKeyPressed=false;
StartTimer = GetTickCount();
i=0;
}
return false;
}
#define CombinationKeys(...) CombineKeys({__VA_ARGS__})
//heres a sample working:
if(CombinationKeys({{'A', 'S', 'D'}, {'P'}, {'A'}, {'B'}}))
{
MessageBox(NULL,"hi","hi",MB_OK);
}
//another sample don't works :(
if(CombinationKeys({{'A', 'S', 'D'}, {'P','O'}, {'A'}, {'B'}}))
{
MessageBox(NULL,"hi","hi",MB_OK);
}
|