# Key released, multiple Keys, how to go around ncurses limitations?

I have been trying to make a small game in the terminal in C++ using ncurses.

After searching around for some time, I came to realize that ncurses is pretty limiting as far as keyboard inputs are concerned :

1. How to separate keyPressed and keyReleased events?
2. How to deal with multiple keys pressed at the same time?

Do you know of any solution to go around those limitations?

To answer someone's comment, in other languages I would use a keyListener to detect if a key has been pressed or released, and keep each pressed key in an array that I can use later :

array pressedKeys[];

keyPressed(event)
{
}

keyReleased(event)
{
pressedKeys.remove(event.getKey());
}

... // Later in update()
if keyPressed.contains("up")
goUp();


This does not seem possible in ncurses as it just gets the last pressed character with getch(), and pressed means keydown then keyup, which does not allow to keep track of the different keys pressed at the moment.

• What are you trying to do? What have you tried? – Vaillancourt Jul 8 '17 at 20:42
• stackoverflow.com/questions/4347253/ncurses-multi-key-input – Trevor Powell Jul 9 '17 at 0:44
• Thank you @TrevorPowell, but as I thought I explained, I know it is not possible using only ncurses, that is why I asked if you know how to go around ncurses limitations, maybe by using an external library or something of the same kind. Please do not assume I have not been using google intensively before asking this question here. – realUser404 Jul 9 '17 at 1:31
• @realUser404 I posted the link because it goes to a related/identical question to yours, elsewhere on the Stack Exchange network. Whether or not you'd already seen that other question, other people landing on this page in the future may not have. – Trevor Powell Jul 9 '17 at 16:14

For future readers, here is what I did in order to handle multi key inputs with ncurses :

I realized you can call getch() multiple times during a frame, and it will return a different key each time as long as there are other key pressed. For example if I am holding keys 'A', 'B', and 'C', three calls to getch() will give me the 3 different keys, and the 4th call will give me ERR as no 4th key is pressed.

Then I just need to store all the pressed keys in an array and use them later as I would normally do.

A small pseudo-code may not hurt

int pressedKeys[];

while (gameRunning)
{
int c;
while ((c = getch()) != ERR)

• You might be interested also in getstr() and its length restricted counterpart getnstr() which handles more than one key in a sequence. Of course you're using C++ and this is a char * but still might be of interest to some. I will add that the limitation isn't entirely a curses issue but a terminal issue. For example detecting modifier keys or more than one key at the same time. If you use cat -v you can see this in action. – Pryftan Dec 6 '19 at 16:54