我想读取键盘操作,有些键会生成ESC[...,当我读取ESC时,我想判断ESC键是按下键还是其他键。(也就是说,当我阅读时,如何检查stdin,缓冲区中是否有字符?)
我关闭了行输入模式,在linux中,我可以使用以下代码来解决这个问题:
int check_keydown()
{
#if defined _WIN32
#else
int ch = 0;
int res = read(STDIN_FILENO, &ch, 1);
if (res > 0)return ch;
return -1;
#endif
}但我不知道如何在windows中实现相同的功能。
(我检查了ncurses的源代码,它使用ReadConsoleInput读取键盘操作,这不是我想要的。)我想知道如何检查stdin缓冲区是否为空,通过非阻塞方式()检查
有人能告诉我该怎么做吗?谢谢。
发布于 2021-10-08 06:19:08
对于非阻塞IO,可以在民意测验上使用选择,也可以在stdin上使用
#include <stdio.h> // stdin
#include <poll.h> // int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd fd;
fd.fd = stdin;
fd.events = POLLIN; // "There is data to read."
fd.revents = 0;
int ret = poll(&fd, 1, 0);
if (ret > 0 && (fd.revents & POLLIN != 0)) {
// got some data
}
else {
perror("poll failed on stdin");
}https://stackoverflow.com/questions/69393895
复制相似问题