2

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.

k-a-v
  • 326
  • 5
  • 22
  • Out of curiosity, why do you want to avoid Allegro? – Mode77 Oct 25 '19 at 01:28
  • Echo all the keyboard and mouse events with timestamp to a queue and process them in a separate thread. Can be engageing task however. – seccpur Oct 25 '19 at 02:03
  • If you are only interested in knowing if the key is down or up, do not use a queue. Just track the keypresses in the KEY_DOWN and KEY_UP events (or respective events depending on what API you use). If you want your player to customise the keys and therefore want to use most keys on the keyboard, use an array with the key scancode or similar as the index into the array and use 0 for key is up and 1 for key is down. If you only want to track for example the LEFT/RIGHT/UP/DOWN keys, just use a simple bool var. See my answer below – Eirik Moseng Oct 25 '19 at 17:13

1 Answers1

1

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.

Eirik Moseng
  • 146
  • 5
  • The problem is reading key presses or releases then. As I've said, I've tried `getch` and `sdl2`, neither of which worked for the reasons stated above. – k-a-v Oct 25 '19 at 10:25
  • As stated above, you should use to flag to track pressed and released keys, not act directly on reading the state of the keyboard. You act on key pressed and key released by setting or unsetting the flag for the key in question (the flag can be an array or a simple bool) and in your game loop you check which keys are down by checking the flag. – Eirik Moseng Oct 25 '19 at 17:04
  • And if you use SDL2, you can use the SDL_GetKeyboardState method which essentially do the same thing as I describe above (keep track of key presses and released using an indexed array). – Eirik Moseng Oct 25 '19 at 17:10
  • 1
    I understand the flag part, but my question is how do I actually get the input and detect the key down events? I believe SDL2 requires a GUI window to be open - I'd like this to be entirely in the terminal. – k-a-v Oct 25 '19 at 21:06