getch返回-1? [英] getch returns -1?
问题描述
他们问如何捕获诸如F11或insand getchr之类的键,这些键不返回任何内容,我找不到能接受来自输入事件的原始输入的工作.
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..
我现在正在尝试在C ++程序中使用ncurses/curses来捕获这些键.
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;
}
我当然使用它与另一个getch()函数相同,但是它无限次返回-1
.
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)
是否需要打开某些开关才能在库中使用此getch()?
Is there a certain switch I need to switch on in order to use this getch() in the libraries?
推荐答案
您需要先调用initscr();
初始化诅咒,然后再调用getch()
.
You need to call initscr();
to initialise curses before calling getch()
.
此外,您可能希望使用非行缓冲模式,因此您还应该调用cbreak(); noecho();
(回声模式不应与cbreak模式一起使用).
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).
这篇关于getch返回-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!