John
John

Reputation: 1118

getch returns -1?

They asked how to capture keys such as F11 or insand getchr does not return anything for those keys, and there is nothing I can find working that accepts raw input from input events..

I am now trying ncurses/curses in a C++ program to capture these keys.

My program to test is simple, it is basically:

#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
int main() {
    int car;
    while(c != '\b') {
        c = getch();
        printf("%i", c);
    }
    return 0;
}

I use it of course the same as another getch() function, but it returns -1 infinite times.. I am using a recent kernel in Arch linux, in a standard terminal (tested in xterm as well)

Is there a certain switch I need to switch on in order to use this getch() in the libraries?

Upvotes: 3

Views: 1342

Answers (1)

caf
caf

Reputation: 239041

You need to call initscr(); to initialise curses before calling getch().

In addition, you probably want non-line-buffered mode, so you should also call cbreak(); noecho(); (echo mode should not be used with cbreak mode).

Upvotes: 7

Related Questions