我一直在寻找相当于kbhit()
我读过几个关于这个主题的论坛,大多数似乎都建议使用 ncurses。
我应该如何使用 ncurses 检查 C++ 中是否按下了某个键?
功能getch()
ncurses 提供从窗口读取一个字符。
我想写一个函数,只检查是否有按键,然后我想做getch()
.
您可以使用nodelay()
转动功能getch()
进入非阻塞调用,返回ERR
如果没有可用的按键。如果按键可用,则会从输入队列中提取该按键,但如果您愿意,也可以将其推回到队列中ungetch()
.
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1) {
if (kbhit()) {
printw("Key pressed! It was: %d\n", getch());
refresh();
} else {
printw("No key pressed yet...\n");
refresh();
sleep(1);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)