在Linux上使用kbhit()和getch()

时间:2015-03-29 22:36:16

标签: c++ linux getch conio kbhit

在Windows上,我有以下代码来查找输入而不中断循环:

#include <conio.h>
#include <Windows.h>
#include <iostream>

int main()
{
    while (true)
    {
        if (_kbhit())
        {
            if (_getch() == 'g')
            {
                std::cout << "You pressed G" << std::endl;
            }
        }
        Sleep(500);
        std::cout << "Running" << std::endl;
    }
}

然而,看到没有conio.h,最简单的方法是在Linux上实现同样的事情吗?

5 个答案:

答案 0 :(得分:12)

如果你的linux没有支持conio.h的{​​{1}},你可以看here for Morgan Mattews's code以与任何POSIX兼容系统兼容的方式提供kbhit()功能。

当技巧在termios级别停用缓冲时,它还应该解决kbhit()问题,如所示here

答案 1 :(得分:6)

如上所述的ncurses howto可能会有所帮助。下面是一个示例,说明如何使用ncurses,如conio示例:

#include <ncurses.h>

int
main()
{
    initscr();
    cbreak();
    noecho();
    scrollok(stdscr, TRUE);
    nodelay(stdscr, TRUE);
    while (true) {
        if (getch() == 'g') {
            printw("You pressed G\n");
        }
        napms(500);
        printw("Running\n");
    }
}

请注意,对于ncurses,不使用iostream标头。这是因为将stdio与ncurses混合会产生意想不到的结果。

顺便说一下,

ncurses定义TRUEFALSE。正确配置的ncurses将使用与ncurses'bool相同的数据类型作为用于配置ncurses的C ++编译器。

答案 2 :(得分:6)

基于Christophe答案的紧凑型解决方案

#include <sys/ioctl.h>
#include <termios.h>

bool kbhit()
{
    termios term;
    tcgetattr(0, &term);

    termios term2 = term;
    term2.c_lflag &= ~ICANON;
    tcsetattr(0, TCSANOW, &term2);

    int byteswaiting;
    ioctl(0, FIONREAD, &byteswaiting);

    tcsetattr(0, TCSANOW, &term);

    return byteswaiting > 0;
}

与该答案不同,在程序退出后,这不会使终端处于奇怪的状态。但是,它仍然会将字符留在输入缓冲区中,因此按下的键将不受欢迎地出现在下一个提示行中。

解决此问题的另一种解决方案是

void enable_raw_mode()
{
    termios term;
    tcgetattr(0, &term);
    term.c_lflag &= ~(ICANON | ECHO); // Disable echo as well
    tcsetattr(0, TCSANOW, &term);
}

void disable_raw_mode()
{
    termios term;
    tcgetattr(0, &term);
    term.c_lflag |= ICANON | ECHO;
    tcsetattr(0, TCSANOW, &term);
}

bool kbhit()
{
    int byteswaiting;
    ioctl(0, FIONREAD, &byteswaiting);
    return byteswaiting > 0;
}

用法如下

enable_raw_mode();
// ...
if (kbhit()) ...
// ...
disable_raw_mode();
tcflush(0, TCIFLUSH); // Clear stdin to prevent characters appearing on prompt

现在,在执行第一行和最后一行之间键入的任何字符都不会显示在终端中。但是,如果您使用Ctrl + C退出,则终端 将处于奇怪的状态。 (叹气)

答案 3 :(得分:3)

虽然使用ncurses在功能上等同于Turbo C“conio.h”API,但更完整的解决方案是使用conio实现,可以是found here

您可以在程序中下载并使用它,以便在Linux上完全实现conio接口。 (或OSX。)由Ron Burkey撰写。

答案 4 :(得分:0)

如果您使用的是Linux,我发现了此解决方案,您可以在其中创建自己的本地库:

http://linux-sxs.org/programming/kbhit.html

kbhit.cpp


#include "kbhit.h"
#include <unistd.h> // read()
    
keyboard::keyboard(){
    tcgetattr(0,&initial_settings);
    new_settings = initial_settings;
    new_settings.c_lflag &= ~ICANON;
    new_settings.c_lflag &= ~ECHO;
    new_settings.c_lflag &= ~ISIG;
    new_settings.c_cc[VMIN] = 1;
    new_settings.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &new_settings);
    peek_character=-1;
}
    
keyboard::~keyboard(){
    tcsetattr(0, TCSANOW, &initial_settings);
}
    
int keyboard::kbhit(){
    unsigned char ch;
    int nread;
    if (peek_character != -1) return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0,&ch,1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if (nread == 1){
        peek_character = ch;
        return 1;
    }
    return 0;
}
    
int keyboard::getch(){
    char ch;

    if (peek_character != -1){
        ch = peek_character;
        peek_character = -1;
    }
    else read(0,&ch,1);
    return ch;
}

kbhit.h

#ifndef KBHIT_H
#define KBHIT_H
    
#include <termios.h>
    
class keyboard{
    public:
        keyboard();
        ~keyboard();
        int kbhit();
        int getch();

    private:
        struct termios initial_settings, new_settings;
        int peek_character;
};
    
#endif

在main.cpp内部,我创建了一个实例:

#include "kbhit.h"

int main(){
    int key_nr;
    char key;
    keyboard keyb;
    while(true){
        if( keyb.kbhit() ){
            key_nr = keyb.getch(); //return int
            key = key_nr; // get ascii char
            // do some stuff
        }
    }
    return 0;
}
相关问题