简体   繁体   English

无阻塞键盘读取-C / C ++

[英]Non-blocking keyboard read - C/C++

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. 我需要知道何时未按下它(空闲),以便switch case块将进入default部分。 At this point, the read() function waits until there's an input from the user. 此时, read()函数将等待,直到有用户输入为止。 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. 注意:我是一名Java程序员,并且仍在学习C / C ++,因此可能有点难以理解。 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); 编辑:我找到了此链接,并且似乎与我在fcntl(STDIN_FILENO,F_SETFL,flags | O_NONBLOCK);的行中查找的内容有关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. 但是由于我几乎不了解C的任何知识,所以我完全不知道它在说什么。
http://www.codeguru.com/forum/showthread.php?t=367082 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;
}

The OP seems to have answered their question: OP似乎已经回答了他们的问题:

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 eg resetting it back again -if that even makes sense) . 拜托,任何人,请验证并让我知道这是正确的方法,还是完成此方法的完整方法(我错过了任何其他添加步骤,例如再次将其重新设置-如果这样还行)。

So what i found is to add this 3 lines before entering the while loop: 所以我发现是在进入while循环之前添加这3行:

 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 */ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM