user20695956
user20695956

Reputation: 73

how to detect multiple keypresses in ncurses

I'm trying to make a crossplatform way to detect when multiple keys are held down in ncurses. I used https://gamedev.stackexchange.com/questions/144558/key-released-multiple-keys-how-to-go-around-ncurses-limitations to write some code which is shown here

#include <curses.h>
#include <stdio.h>
int pressedKeys[200];



int main(void)
{
    initscr();
    addstr("test");
    nodelay(stdscr, TRUE);
    
    int r=0;
    while (true)
    {
        int c;
        while ((c = getch()) != ERR)
        {
            r=0;
            while (pressedKeys[r] != '\0')
            {
                pressedKeys[r]=c;
                r++;
            }
                
        }

        char m[200];
        m[0]=pressedKeys[0];
        m[1]=pressedKeys[1];
        int n=0;
        while (pressedKeys[n] != '\0')
        {
            n++;
        }
        clear();
        char s[1];
        sprintf(s,"%d",n);
        addstr("numkeys");
        addstr(s);

        addstr("keypresses");
        addstr(m);
        addstr("\n");

    }
    endwin();
    return 0;
}

but it seems unable to detect any keypresses. it needs to just leave the pressedkeys string empty if there are no keys pressed down as well but currently well... it seems to always be empty, I was wondering if there was a way to fix it? it seems related to nodelay() but I need it as well...there needs to be no delay

Upvotes: 0

Views: 78

Answers (1)

user20695956
user20695956

Reputation: 73

As mentioned in this comment:

Curses immediately reports all key presses, but it does not report key releases, as it cannot be done reliably across even modern terminal emulators. It is just not the way the terminal works. Therefore, detecting multiple keypresses comes down to watching the clock and hoping you get it right. BTW, the terminal is a terrible choice for writing a game. You’d be much better off playing with PyGame or something yeah it kinda just doesnt work I guess then, at least using pure C

Upvotes: 1

Related Questions