How to detect combined key input in C++?

  arrow-keys, c++, input, windows

Let’s say you play a game on the computer, you move your character with arrow keys or WASD, if, let’s say, you press and hold the up arrow key/W and left arrow key/A together, if the game allows it, your character will move somewhere in between, even if you press and hold one of the 2 first and the other later, while continue holding the first, none of the 2 will get interrupted and then ignored.

I know how to detect if the user pressed an arrow key/letter,

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
    while (1) {
        int ch = getch();
        if (ch == 224) {
            ch = getch();
            switch (ch) {
            case 72: cout << "upn";    break;
            case 80: cout << "downn";  break;
            case 77: cout << "rightn"; break;
            case 75: cout << "leftn";  break;
            default: cout << "unknownn";
            }
        }
        else 
            cout << char(ch) << 'n';
    }
}

but I can’t find a way to do what I mentioned at the beginning of this question


Any attempt to help is appreciated

Source: Windows Questions

LEAVE A COMMENT