问题
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?
回答1:
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).
来源:https://stackoverflow.com/questions/4241366/getch-returns-1