Thread: [c++11 - win32] - combination keys for do some actions

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    [c++11 - win32] - combination keys for do some actions

    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
    Code:
    '{'A', 'S', 'D'}'
    is tested correctly.. but not the 2nd
    Code:
    '{'P','O'}'
    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?

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    now i understand why the 1st combination works fine and the don't.
    the 1st is more easy to test.. so no problem.. but the others are more dificulty, but how the user can press 2 keys in same and not lose the last combination(it's keydown message and if we don't press that 2 keys in same time, we lose what we did before).
    i need some advices for fix these problem.. if anyone, can advice me, please tell me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 actions at once
    By Chobo_C++ in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2004, 06:28 AM
  2. accelerator keys in win32 API
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2002, 09:42 PM
  3. Actions of CreateDirectory
    By golfinguy4 in forum Windows Programming
    Replies: 3
    Last Post: 06-30-2002, 10:41 AM
  4. arrow keys, ctrl and alt keys
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 03:53 PM