i have the function for test 1 key\combination keys(using a vector) if they are pressed:
Code:
//test if a key\combination keys is pressed
bool AreAllKeysPressed(const std::vector<int> &keys)
{
int state = 0x8000;
for (int key:keys)//please anyone can explain to me these type of 'for' ?
{
state &= GetAsyncKeyState(key);
}
return (state & 0x8000) != 0;
}
maybe i have 1 problem in these function and i don't know.
these function go thro the vector and test if the keys are pressed in same time(1 or more keys that can be in vector).
and heres the function that do more combination:
Code:
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;
}
these function test the combination of keys. the vector is a 2D vector. and test the 1st dimension with the AreAllKeysPressed() function.
and now see these sample(used with keydown message):
Code:
if(CombineKeys({{'A', 'S', 'D'}, {'P'}, {'A'}, {'B'}}))
{
MessageBox(NULL,"hi","hi",MB_OK);
}
these sample works fine... but i do:
Code:
if(CombineKeys({{'A', 'S', 'D'}, {'P','O'}, {'A'}, {'B'}}))
{
MessageBox(NULL,"hi","hi",MB_OK);
}
don't works... only the 1st combination
is tested correctly.. but not the 2nd
why these happens?
because i use a irregular vector? (the 1st index have 3, the 2nd index have 2 and the 3rd and 4th have just 1)
what you can say?