-
19-08-2019 - |
题
我试着写在循环,直到用户按下一个键C(Linux下)的计划,但不应该要求一个按键,继续每个循环。
有没有一种简单的方法来做到这一点?我想我可能有select()
做到这一点,但似乎像很多的工作。
可替换地,是有办法钓 CTRL - <?KBD> C 按键做清理程序关闭,而不是之前非阻塞IO
解决方案
如已经陈述的,可以使用sigaction
捕获CTRL-C,或select
捕获任何标准输入。
请注意但是,与后者的方法还需要设置TTY使得它在字符在-A-时间,而不是线在一次一个模式。后者是默认的 - 如果你在一行文字输入,直到你按下输入它不会发送到正在运行的程序的标准输入
您可能需要使用的功能tcsetattr()
关闭ICANON模式,大概也禁止回声了。从存储器,还必须将终端设置回ICANON模式时,程序退出!
只是为了完整性,这里的一些代码,我刚刚敲了(注:没有错误检查!),它建立在Unix TTY和模仿DOS <conio.h>
功能kbhit()
和getch()
:
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
struct termios orig_termios;
void reset_terminal_mode()
{
tcsetattr(0, TCSANOW, &orig_termios);
}
void set_conio_terminal_mode()
{
struct termios new_termios;
/* take two copies - one for now, one for later */
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
/* register cleanup handler, and set the new terminal mode */
atexit(reset_terminal_mode);
cfmakeraw(&new_termios);
tcsetattr(0, TCSANOW, &new_termios);
}
int kbhit()
{
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
int getch()
{
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) {
return r;
} else {
return c;
}
}
int main(int argc, char *argv[])
{
set_conio_terminal_mode();
while (!kbhit()) {
/* do some work */
}
(void)getch(); /* consume the character */
}
其他提示
select()
有点太低级别为了方便。我建议你使用ncurses
库摆在CBREAK模式和延时模式的终端,然后调用getch()
,如果没有字符准备将返回ERR
:
WINDOW *w = initscr();
cbreak();
nodelay(w, TRUE);
在这一点上可以调用getch
无阻塞。
在UNIX系统中,可以使用sigaction
调用注册SIGINT
信号的信号处理程序,它表示控制+ C键序列。信号处理程序可以设置将在环路被检查的标记使其适当地断裂。
您可能想kbhit();
//Example will loop until a key is pressed
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
while(1)
{
if(kbhit())
{
break;
}
}
}
,这可能不是在所有的环境下工作。一种便携式方式是创建一个监视线程和设置一些标志上getch();
另一种方式来获得无阻塞键盘输入是打开设备文件,并宣读了!
您必须知道你正在寻找为/ dev /输入/事件*的一个设备文件。您可以运行执行cat / proc /总线/输入/设备,以找到您想要的设备。
此代码对我的作品(运行作为管理员)。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
int main(int argc, char** argv)
{
int fd, bytes;
struct input_event data;
const char *pDevice = "/dev/input/event2";
// Open Keyboard
fd = open(pDevice, O_RDONLY | O_NONBLOCK);
if(fd == -1)
{
printf("ERROR Opening %s\n", pDevice);
return -1;
}
while(1)
{
// Read Keyboard Data
bytes = read(fd, &data, sizeof(data));
if(bytes > 0)
{
printf("Keypress value=%x, type=%x, code=%x\n", data.value, data.type, data.code);
}
else
{
// Nothing read
sleep(1);
}
}
return 0;
}
在诅咒库可以用于此目的。当然,select()
和信号处理程序,可以得用来在一定程度上。
如果你很快乐,只是追赶控制-C,它是木已成舟。如果你真的想非阻塞I / O,但你不想要的library,另一种选择是移动锁,股票和桶到的 AT&T sfio
库。它的图案基于C stdio
但更灵活,线程安全的漂亮库,并更好地执行。 (激活sfio代表安全,快速I / O。)
还有就是要做到这一点,但选择()可能是一个很好的方式没有可移植的方法。请参见 http://c-faq.com/osdep/readavail.html 了解可能的解决方案。
下面就来为你做这一个功能。您需要附带POSIX系统termios.h
。
#include <termios.h>
void stdin_set(int cmd)
{
struct termios t;
tcgetattr(1,&t);
switch (cmd) {
case 1:
t.c_lflag &= ~ICANON;
break;
default:
t.c_lflag |= ICANON;
break;
}
tcsetattr(1,0,&t);
}
断裂下来:tcgetattr
得到在t
当前的终端信息,并将其存储。如果cmd
是1,在t
本地输入标志设定为非阻塞输入。否则,复位。然后tcsetattr
改变标准输入到t
。
如果你不重置标准输入你的程序,你将有你的shell问题的结束。
您可以做到这一点使用选择如下:
int nfds = 0;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(0, &readfds); /* set the stdin in the set of file descriptors to be selected */
while(1)
{
/* Do what you want */
int count = select(nfds, &readfds, NULL, NULL, NULL);
if (count > 0) {
if (FD_ISSET(0, &readfds)) {
/* If a character was pressed then we get it and exit */
getchar();
break;
}
}
}
不太多的工作:d