3

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?

John
  • 1,110
  • 3
  • 14
  • 28

1 Answers1

7

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).

caf
  • 233,326
  • 40
  • 323
  • 462
  • I'm no curses expert but it seems you may also want to call `keypad(stdscr, TRUE);` so that ncurses can translate special keys escape sequences and return from `getch()` the convenient values defined by the *KEY_* macros. e.g. `if (getch() == KEY_F0+11) { /* f11 pressed */ }` – Alex Jasmin Nov 22 '10 at 01:23
  • Thank you. My FN keys do not work on Linux for some reason so I cannot test those, but it does work on odd ones like insert and whatnot.. I hope I don't need to map raw keyboard input from the kernel to get those fn+ins keys and stuff to get pause :\ – John Nov 22 '10 at 01:30