栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

按键后如何立即读取终端的输入缓冲区

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

按键后如何立即读取终端的输入缓冲区

看来您正在寻找这样的东西。

该程序实际上等待用户输入。如果按下向上箭头键,程序将打印“按下箭头键”,然后退出。如果按下了其他任何按钮,它将等待用户完成键入的内容并打印出来,然后退出。

#include <termios.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>int main(){  struct termios oldt, newt;  char ch, command[20];  int oldf;  tcgetattr(STDIN_FILENO, &oldt);  newt = oldt;  newt.c_lflag &= ~(ICANON | ECHO);  tcsetattr(STDIN_FILENO, TCSANOW, &newt);  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);  while(1)  {    ch = getchar();    if (ch == '33')    { printf("Arrow keyn"); ch=-1; break;}    else if(ch == -1) // by default the function returns -1, as it is non blocking    {      continue;    }    else    {      break;    }  }  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);  fcntl(STDIN_FILENO, F_SETFL, oldf);   if(ch != EOF)   {      ungetc(ch,stdin);ith      putchar(ch);      scanf("%s",command);      printf("n%sn",command);      return 1;    }    return 0;  }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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