0

I have the following code which works fine.

int x = 0;
int main()
{    
    while(true) {   
        if (GetKeyState('A') & 0x8000 && x == 0) {
            Sleep(500);
            x = 1;
        }
        else if (GetKeyState('B') & 0x8000 && x == 1) {
            Sleep(500);
            x = 2;
        }
        else if (GetKeyState('C') & 0x8000 && x == 2) {
            Sleep(500);
            x = 0;
            //Do Something
        }
    }
}

In order to execute the //Do Something part of the code, the user must first press A, then B and then C in that order. However the user can press any key in between and it will still work. So in addition to "A+B+C" the following will also work.

  1. A+C+B+C
  2. A+Q+B+C
  3. A+F11+B+8+LSHIFT+Spacebar+Tab+C

I want only the A+B+C combination to work. Not any of the above.

The code I am trying to implement is somewhat like this

int x = 0;
int main()
{
    while(true) {

        if (GetKeyState('A') & 0x8000 && x == 0) {
            Sleep(500);
            x = 1;
        }
        else if (GetKeyState('B') & 0x8000 && x == 1) {
            Sleep(500);
            x = 2;
        }
        else if (GetKeyState(/*Any keyboard input other than 'B' or 'A'*/) & 0x8000 && x == 1) {
            Sleep(500);
            x = 0;
        }
        else if (GetKeyState('C') & 0x8000 && x == 2) {
            Sleep(500);
            x = 0;
            //Do Something
        }
        else if (GetKeyState(/*Any keyboard input other than 'C' or 'B'*/) & 0x8000 && x == 2) {
            Sleep(500);
            x = 0;
        }
    }
}

So you see I understand the logic required. I just don't know the correct code required to replace the commented part of my code. Please note that the reason I have entered two keys in the comments is because the user might accidentally press a key twice or thrice for which he/she needs to be excused and code needs to still work.

I think I have tried my best to make this question as understandable as possible. If not feel free to suggest edits.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Bari
  • 1
  • 1
  • 1
    What is `GetKeyState`? – melpomene Mar 31 '18 at 07:36
  • Can't you use a state machine or two boolean flags that get enabled sequentially as `'A'`, `'B'` and `'C'` are pressed respectively? – Carl Mar 31 '18 at 07:48
  • What do you think x == 1 (or 0 or 2) in if statements do? What are they used for? I think when you answer my question, you will get one step closer to find answer to your own question. :] – Alp Mar 31 '18 at 08:01
  • You meant to use just a plain else {} clause at the end. GetKeyState() is not suitable for use in a console mode app, it is synchronous with the message queue and a console mode app doesn't have one. Do it correctly, without the Sleep calls, with ReadConsoleInput(). – Hans Passant Mar 31 '18 at 10:15

1 Answers1

1

On Windows, for simplicity, you could use _kbhit() unless there is a specific reason you want to use GetKeyState

#include <iostream>
#include <conio.h>  

int main()
{
    int x = 0;
    while (true)
    {
        if (!_kbhit())
            continue;

        switch (_getch())
        {
        case 'A':
            x == 0 ? ++x : x = 0;
            break;
        case 'B':
            x == 1 ? ++x : x = 0;
            break;
        case 'C':
            x == 2 ? ++x : x = 0;
            break;
        default:
            x = 0;
        }

        if (x == 0)
            std::cout << "Oops!" << std::endl;
        else if (x == 3)
        {
            std::cout << "Bingo!" << std::endl;
            break;
        }
    }
}
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37