C 太好了。kbhit()是一个DOS/Windows控制台IO函数。它仍然是一个有效的答案,在许多情况下都可以使用。OP没有指定可移植性或任何特定的平台。Alnitak:我不知道这意味着一个平台-什么是等效的Windows术语?@Alnitak为什么“非阻塞
C 太好了。kbhit()是一个DOS/Windows控制台IO函数。它仍然是一个有效的答案,在许多情况下都可以使用。OP没有指定可移植性或任何特定的平台。Alnitak:我不知道这意味着一个平台-什么是等效的Windows术语?@Alnitak为什么“非阻塞,c,linux,asynchronous,input,nonblocking,C,Linux,Asynchronous,Input,Nonblocking,太好了。kbhit()是一个DOS/Windows控制台IO函数。它仍然是一个有效的答案,在许多情况下都可以使用。OP没有指定可移植性或任何特定的平台。Alnitak:我不知道这意味着一个平台-什么是等效的Windows术语?@Alnitak为什么“非阻塞”作为一个编程概念在Unix环境中更为常见?@Alnitak:我的意思是,在接触任何*nix之前很久,我就熟悉“非阻塞调用”这个术语了。。所以就像Zxaos一样,我永远不会意识到它意味着一个平台。这个答案是不完整的。如果不使用tcsetattr
太好了。kbhit()是一个DOS/Windows控制台IO函数。它仍然是一个有效的答案,在许多情况下都可以使用。OP没有指定可移植性或任何特定的平台。Alnitak:我不知道这意味着一个平台-什么是等效的Windows术语?@Alnitak为什么“非阻塞”作为一个编程概念在Unix环境中更为常见?@Alnitak:我的意思是,在接触任何*nix之前很久,我就熟悉“非阻塞调用”这个术语了。。所以就像Zxaos一样,我永远不会意识到它意味着一个平台。这个答案是不完整的。如果不使用tcsetattr()进行终端操作[请参阅我的答案],在按enter键之前,您将无法在fd 0上获得任何信息。我也考虑过使用诅咒,但其潜在问题是initscr()调用会清除屏幕,并且可能会妨碍使用普通stdio进行屏幕输出。是的,curses就是全部,或者什么都不是。curses既是库的名称,也是你使用它时要咕哝的内容;)但是,既然我要向您提到+1,那么getch()应该返回按下的键,还是仅仅应该使用该字符?@Salepate它应该返回该字符。是
(void)
位获取该字符,然后将其丢弃。哦,我明白了,也许我做得不对,但当我在printf(“%c”)上使用getch()时;它在屏幕上显示奇怪的字符。稍微编辑一下,你需要#include for read()才能工作。有没有一种简单的方法来读取整行(例如使用“getline”)而不是单个字符?这个答案不完整。您需要将终端设置为非规范模式。另外,nfds
应该设置为1。switch语句缺少一个括号:)很好的例子,但我遇到了一个问题,当事件设备在按住六个以上键时停止发出时,Xorg将继续接收键事件:\n对吗?打开一个线程怎么样?
//Example will loop until a key is pressed
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
while(1)
{
if(kbhit())
{
break;
}
}
}
#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 */
}
WINDOW *w = initscr();
cbreak();
nodelay(w, TRUE);
#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;
}
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;
}
}
}
#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);
}