3
\$\begingroup\$

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)
{
    pressedKeys.add(event.getKey());
}

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ What are you trying to do? What have you tried? \$\endgroup\$ – Alexandre Vaillancourt Jul 8 '17 at 20:42
  • 1
    \$\begingroup\$ stackoverflow.com/questions/4347253/ncurses-multi-key-input \$\endgroup\$ – Trevor Powell Jul 9 '17 at 0:44
  • \$\begingroup\$ 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. \$\endgroup\$ – realUser404 Jul 9 '17 at 1:31
  • \$\begingroup\$ @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. \$\endgroup\$ – Trevor Powell Jul 9 '17 at 16:14
5
\$\begingroup\$

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)
        pressedKeys.add(c);

    ... // Later in update()
    if (pressedKeys.contains(KEY_UP))
        goUp();
}
\$\endgroup\$
  • \$\begingroup\$ may I ask if there are any special settings required for ncurses to accomplish this? It seems like we must set ncurses to nodelay mode? I have tried implementing your pseudo code several ways using C and ncurses. I cannot get my program to recognize more than one key press at a time. thanks \$\endgroup\$ – Matt Farrer Apr 4 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.