栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Ubuntu键盘非阻塞响应事件汇总

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Ubuntu键盘非阻塞响应事件汇总

一、

参考博客

#include 
#include 
#include 
#define TTY_PATH            "/dev/tty"
#define STTY_US             "stty raw -echo -F "
#define STTY_DEF            "stty -raw echo -F "

int get_char()
{
    fd_set rfds;
    struct timeval tv;
    int ch = 0;
    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    tv.tv_sec = 0;
    tv.tv_usec = 10; //设置等待超时时间
    if (select(1, &rfds, NULL, NULL, &tv) > 0) //检测键盘是否有输入
    {
        ch = getchar(); 
    }
    return ch;
}
 
int main()
{
    int ch = 0;
    system(STTY_US TTY_PATH);
    
    while(1){
        ch = get_char();
        if(ch != 0)
        {
            printf("%dnr",ch);
        }
        if(ch == 3){
            system(STTY_DEF TTY_PATH);
            return 0;
        }         
    }
}

二、

参考博客

#include 
#include 
//键盘检测函数
int scanKeyboard()
{
    int in;
    struct termios new_settings;
    struct termios stored_settings;
    tcgetattr(0,&stored_settings);
    new_settings = stored_settings;
    new_settings.c_lflag &= (~ICANON);
    new_settings.c_cc[VTIME] = 0;
    tcgetattr(0,&stored_settings);
    new_settings.c_cc[VMIN] = 1;
    tcsetattr(0,TCSANOW,&new_settings);

    in = getchar();

    tcsetattr(0,TCSANOW,&stored_settings);
    return in;
}
int main(int argc, char **argv)
{
    while(1)
        {
            int ascii = scanKeyboard();
            printf(":%dn",ascii);
            if(ascii==27) break;
        }
    return 0;
}

三、
#include 
#include 
#include  
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);
//    std::cout< 0;
}
int main(int argc, char **argv)
{
    int i;
    int c;
    // Check for keyboard input
    while (true)
    {
        if (kbhit())
        {
            c = fgetc(stdin);
            std::cout <
            case 'h':
                std::cout << "you input h"<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号