非阻塞键盘读取 - C / C ++ [英] Non-blocking keyboard read - C/C++

查看:1560
本文介绍了非阻塞键盘读取 - C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我现在的工作得到了这个下面的函数。但我需要改进的是,它会从键盘读取(在终端上)输入,即使它没有被preSSED。我需要知道什么时候是不是pressed(空闲),使开关的情况下块将陷入默认部分。在这一点上,<​​code>阅读()函数等到有来自用户的输入。谁能给刚刚基础上修改此以下code有何建议?
注:我是一个Java程序员,并且还在学习C / C ++,因此可能难以在我的脑海得到一点点。谢谢你们..

编辑:我发现这个链接,似乎有与什么我在的fcntl行寻找的东西(STDIN_FILENO,F_SETFL,旗帜| O_NONBLOCK); 。但因为我几乎不知道用C什么,我完全不知道它的说法,但。

HTTP://www.$c$cguru.com/forum/showthread的.php?T = 367082

  INT KFD = 0;
结构termios的熟,生;
焦炭℃;
布尔脏= FALSE;//获得原始模式控制台
tcgetattr(KFD,&安培;熟);
的memcpy(安培;生,和放大器;熟的sizeof(结构的termios));
raw.c_lflag&安培; =〜(ICANON | ECHO);
//设定新线路,然后结束文件
raw.c_cc [VEOL] = 1;
raw.c_cc [VEOF] = 2;
tcsetattr(KFD,TCSANOW,&安培;生);看跌期权(从键盘读);
看跌期权(=====================);
看跌期权(使用箭头键导航);而(真){
//从键盘的下一个事件
如果(读(KFD,和C,1) - = 0)
{
  PERROR(读());
  出口(-1);
}linear_ = angular_ = 0;
ROS_DEBUG(值:为0x%02X \\ n,C);开关(三)
{
  案例KEY code_L:
    ROS_DEBUG(左);
    angular_ = -1.0;
    脏= TRUE;
    打破;
  案例KEY code_R:
    ROS_DEBUG(右);
    angular_ = 1.0;
    脏= TRUE;
    打破;
  案例KEY code_U:
    ROS_DEBUG(UP);
    linear_ = 1.0;
    脏= TRUE;
    打破;
  案例KEY code_D:
    ROS_DEBUG(DOWN);
    linear_ = -1.0;
    脏= TRUE;
    打破;
  默认:
    ROS_DEBUG(发行);
    linear_ = 0;
    angular_ = 0;
    脏= TRUE;
    打破;
}


解决方案

(由OP在一个问题回答的编辑,改造成社区维基回答,见<一href=\"http://meta.stackoverflow.com/questions/251597/question-with-no-answers-but-issue-solved-in-the-comments\">Question没有答案,但问题的评论解决)

在OP写道:


  

我想我解决我的问题。请,任何人,核实并让我知道这是做正确的方式,或者是完整的方式做到这一点(我错过任何其他附加步骤如再回来 - 如果,即使是有道理的重置它)。


  
  

所以我发现的是之前进入while循环添加这个3行:


 标志=的fcntl(0,F_GETFL,0); / *获取当前文件状态标志* /
标志| = O_NONBLOCK; / *关闭阻塞标志* /
的fcntl(0,F_SETFL,旗); / *设置了非阻塞读* /

I got this following function with me working now. But what I need to improve is that it would read input from the keyboard (on the terminal) EVEN THOUGH IT IS NOT BEING PRESSED. I need to know when it is NOT pressed (idle) so that the switch case block will fall into the default section. At this point, the read() function waits until there's an input from the user. Can anyone give a suggestion just based on modifying this following code? NOTE: I'm a Java programmer, and still learning C/C++ so it might be hard to get in my head a little bit. Thanks guys..

EDIT: I found this link, and seems to have something related to what i'm looking for at the line of fcntl(STDIN_FILENO,F_SETFL,flags | O_NONBLOCK); . But since I barely know anything in C, I completely have no Idea what it's saying, yet.
http://www.codeguru.com/forum/showthread.php?t=367082

int kfd = 0;
struct termios cooked, raw;
char c;
bool dirty = false;

//get the console in raw mode
tcgetattr(kfd, &cooked);
memcpy(&raw, &cooked, sizeof(struct termios));
raw.c_lflag &=~ (ICANON | ECHO);
// Setting a new line, then end of file
raw.c_cc[VEOL] = 1;
raw.c_cc[VEOF] = 2;
tcsetattr(kfd, TCSANOW, &raw);

puts("Reading from keyboard");
puts("=====================");
puts("Use arrow keys to navigate");

while(true){
//get the next event from the keyboard
if(read(kfd, &c, 1) < 0)
{
  perror("read():");
  exit(-1);
}

linear_ = angular_ = 0;
ROS_DEBUG("value: 0x%02X\n", c);

switch(c)
{
  case KEYCODE_L:
    ROS_DEBUG("LEFT");
    angular_ = -1.0;
    dirty = true;
    break;
  case KEYCODE_R:
    ROS_DEBUG("RIGHT");
    angular_ = 1.0;
    dirty = true;
    break;
  case KEYCODE_U:
    ROS_DEBUG("UP");
    linear_ = 1.0;
    dirty = true;
    break;
  case KEYCODE_D:
    ROS_DEBUG("DOWN");
    linear_ = -1.0;
    dirty = true;
    break;
  default:
    ROS_DEBUG("RELEASE");
    linear_ = 0;
    angular_ = 0;
    dirty = true;
    break;
}

解决方案

(Answered by the OP in a question edit. Transformed into a Community Wiki Answer. See Question with no answers, but issue solved in the comments )

The OP wrote:

I think I solved my problem. Please, anyone, verify and let me know if this is the right way to do it, or is it the complete way to do it (am I missing any other addition step e.g. resetting it back again -if that even makes sense) .

So what i found is to add this 3 lines before entering the while loop:

flags = fcntl(0, F_GETFL, 0); /* get current file status flags */
flags |= O_NONBLOCK;          /* turn off blocking flag */
fcntl(0, F_SETFL, flags);         /* set up non-blocking read */

这篇关于非阻塞键盘读取 - C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆