i'm build a function for do the combination of keys(like the kombat games: we do some combination keys for do an actions):

bool CombineKeys(std::vector<int> const keys)
{
    static bool PreviousKeyPressed;
    static DWORD StartTimer = GetTickCount();
    static int i=0;

        if((GetAsyncKeyState(keys[0]) & 0x8000) and PreviousKeyPressed==false)
        {
            i=0;
            SetWindowText(a,"hello");
            PreviousKeyPressed=true;
            i++;
        }
        else if((i==(int)keys.size()-1) && (GetAsyncKeyState(i) & 0x8000) and PreviousKeyPressed==true)
        {
            SetWindowText(a,"hi");
            return true;
        }
        else if((GetAsyncKeyState(keys[i]) & 0x8000) and PreviousKeyPressed==true)
        {
            i++;
            PreviousKeyPressed=true;
        }

        else if(GetTickCount() < (StartTimer + 20000))//2 seconds
        {
            PreviousKeyPressed=false;
            i=0;
        }
        else
        {
            PreviousKeyPressed=false;
            i=0;
        }

    return false;
}

//heres how i use in keydown message
if(CombineKeys({'A','B'})==true)
            MessageBox(NULL,"hi","hi",MB_OK);

i see some problems with my function :(
seems the i not be plus 1, because only get the 1st if :(
if i press the next letter, don't do nothing :(
what i'm doing wrong?

bool CombineKeys(std::vector<int> const keys)
{
    static bool PreviousKeyPressed=false;
    static DWORD StartTimer = 0;
    static int i=0;

        if((GetAsyncKeyState(keys[0]) & 0x8000) and PreviousKeyPressed==false)
        {
            i=0;
            SetWindowText(a,"hello");
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            i++;
        }
        else if((i==(int)keys.size()-1) && (GetAsyncKeyState(keys[(int)keys.size()-1]) & 0x8000) and PreviousKeyPressed==true)
        {
            SetWindowText(a,"hihi");
            PreviousKeyPressed=false;
            i=0;
            return true;

        }
        else if((GetAsyncKeyState(keys[i]) & 0x8000) and PreviousKeyPressed==true)
        {

            SetWindowText(a,"hi");
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            i++;
        }

        else if(GetTickCount() < (StartTimer + 2000))//2 seconds
        {
            StartTimer = GetTickCount();
            PreviousKeyPressed=false;
            i=0;
        }
        else
        {
            PreviousKeyPressed=false;
            StartTimer = GetTickCount();
            i=0;
        }

    return false;
}

almost corrected ;)
is these if corrected?

else if(GetTickCount() < (StartTimer + 2000))//2 seconds

seems not ok. because, after 2 seconds, the StartTimer isn't igual to GetTickCount():(

the problem was the 'if' order ;)

bool CombineKeys(std::vector<int> const keys)
{
    static bool PreviousKeyPressed=false;
    static DWORD StartTimer = GetTickCount();
    static int i=0;

        //test if the 1st key was pressed
        if((GetAsyncKeyState(keys[0]) & 0x8000) and PreviousKeyPressed==false)
        {
            i=0;
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            i++;
        }
        //test if the StartTimer have 2 seconds
        else if (GetTickCount() - StartTimer >= 2000)//now i put the timer here ;)
        {
            SetWindowText(a,"hi");
            StartTimer =GetTickCount();
            PreviousKeyPressed=false;
            i=0;
        }
        //test if the last key was pressed
        else if((i==(int)keys.size()-1) && (GetAsyncKeyState(keys[(int)keys.size()-1]) & 0x8000) and PreviousKeyPressed==true)
        {
            PreviousKeyPressed=false;
            i=0;
            StartTimer=0;
            return true;

        }
        //test if the next key was pressed
        else if((GetAsyncKeyState(keys[i]) & 0x8000) and PreviousKeyPressed==true)
        {
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            i++;
        }

        else
        {
            PreviousKeyPressed=false;
            StartTimer = GetTickCount();
            i=0;
        }

    return false;
}

//heres how we can use it:
if(CombineKeys({'A','B', VK_LEFT,'C'})
          MessageBox(NULL,"these combination keys activated","Combination Keys",MB_OK);

now i'm working with a diferent combination, for accept '&' and '|' ;)

i'm working with a code for get all pressed keys inside of a variable:

int GetAllKeyPressed()
{
    int key=0;

    for(int i=0; i<256; i++)
    {
        if(GetAsyncKeyState(i) & 0x8000)
        {
            if(i==0)
                key=i;
            else
                key=key&i;
        }
    }
    return key;
}

if the key0 is pressed then put it in key variable. if key1 is pressed combine it with '&' with key.
so what i'm doing wrong?

i found my problem:

int GetAllKeyPressed()
{
    int key=0;
    bool FirstKey=false;

    for(int i=0; i<256; i++)
    {
        if(GetAsyncKeyState(i) & 0x8000)
        {
            if(FirstKey==false)
            {
                key=i;
                FirstKey=true;
            }
            else
            {
                key=key&i;
            }
        }
    }
    return key;
}

thanks for all

now i must do another complex test ;)
maybe i can use several combinations of '&' and '|' ;)

heres the code more cool and working:

//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) != 0;
}

bool CombineKeys(std::vector<std::vector<int>> const &keys)
{
    static bool PreviousKeyPressed=false;
    static DWORD StartTimer = GetTickCount();
    static DWORD AllKeysTimer = 0;
    static int i=0;

        //test if the 1st key was pressed
        if((AreAllKeysPressed(keys[0])==true) && PreviousKeyPressed==false)
        {
            i=0;
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            AllKeysTimer=0;
            i++;
        }
        //if the last combination have only 1 key
        else if((i==(int)keys.size()-1) && (AreAllKeysPressed(keys[(int)keys.size()-1])==true) && PreviousKeyPressed==true && keys[(int)keys.size()-1].size()==1)
        {
            PreviousKeyPressed=false;
                StartTimer = 0;
                AllKeysTimer=0;
                i=0;
                return true;
        }
        //testing if the keys are pressed in same time
        //but you only have 1 second for that
        else if(GetTickCount() - AllKeysTimer <= 500 && PreviousKeyPressed==true)
        {
            if(AreAllKeysPressed(keys[i])==true && i!=(int)keys.size()-1)
            {
                PreviousKeyPressed=true;
                StartTimer = GetTickCount();
                AllKeysTimer=0;
                i++;
            }
            else if(AreAllKeysPressed(keys[(int)keys.size()-1])==true && i==(int)keys.size()-1)
            {
                PreviousKeyPressed=false;
                StartTimer = 0;
                AllKeysTimer=0;
                i=0;
                return true;
            }
        }
        //test if the StartTimer have 2 seconds
        else if ((GetTickCount() - StartTimer >= 2000))//now i put the timer here ;)
        {
            StartTimer =0;
            AllKeysTimer=0;
            PreviousKeyPressed=false;
            i=0;
        }
        //test if the last key was pressed
        else if((i==(int)keys.size()-1) && (AreAllKeysPressed(keys[(int)keys.size()-1])==true) && PreviousKeyPressed==true)
        {
            StartTimer=0;
            AllKeysTimer=GetTickCount();
        }
        //test if the next key was pressed
        else if((AreAllKeysPressed(keys[i])==true) && PreviousKeyPressed==true)
        {
            PreviousKeyPressed=true;
            StartTimer = GetTickCount();
            AllKeysTimer=0;
            i++;
        }
        else if((AreAllKeysPressed(keys[i])==false) && PreviousKeyPressed==true)
        {
            AllKeysTimer=GetTickCount();
        }

        else
        {
            PreviousKeyPressed=false;
            StartTimer = GetTickCount();
            AllKeysTimer=0;
            i=0;
        }

    return false;
}

#define CombinationKeys(...) CombineKeys({__VA_ARGS__})

see the test samples:

if(CombinationKeys({'A','S'},{'W'},{'R','T'}, {'O'})==true)
         //do something



if(CombinationKeys({'A'},{'W'},{'R'}, {'O'})==treu)
        //do something



if(CombinationKeys({'A'},{'W'},{'R'}, {'O','P'})==true)
        //do somthing

very cool.. thanks for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.