getch returns -1?

邮差的信 提交于 2019-12-06 07:46:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!