C++ mutiple key input or key press/release events

Viewed 852

I am writing a game in C++. For it, I need to detect multiple keys being pressed or held at the same time, or detect key press and key release events.

For example, if I hold both the up and right keys, I want to be able to detect this and move the game diagonally. Additionally, currently if I am holding the right key, and I press up, it no longer recognizes that right is being held. I need a way to find when the right key is actually released so it isn't interrupted like this.

I know that sdl2 can recognize multiple keys being pressed or held, but that requires creating a window, and I want this to be entirely within the terminal.

getch() from curses/ncurses additionally doesn't work for this.

What can I use for this? I'd like to avoid Allegro if possible.

1 Answers

You don’t mention how you are reading key input now and how you are handling the input so this is just a general advice on how to handle key input.

When you detect or handle a key pressed (key down) event for a specific key, set a flag (e.g. keyflags = keyflags | KEY_RIGHT) to indicate the key is down, and unset (delete) the flag on keyup event. This might solve the problem you experience today if you act directly on key presses.

Related