检测击键 [英] Detecting keystrokes
本文介绍了检测击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检测一个按键,无需用户pressing进入。什么是最优雅的方式?
I need to detect a keystroke, without the user pressing enter. What's the most elegant way?
即。如果用户点击字母Q,没有pressing进入,程序做了。
I.e. If the user hits the letter Q, without pressing enter, the program does something.
推荐答案
在UNIX / POSIX,这样做的标准方法是把输入到非规范模式tcsetattr:
In unix/posix, the standard way of doing this is to put the input into non-canonical mode with tcsetattr:
#include <termios.h>
#include <unistd.h>
:
struct termios attr;
tcgetattr(0, &attr);
attr.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &attr);
见的termios(3)手册页面了解详细信息(可能更多信息比你想知道的)。
See the termios(3) man page for more details (and probably more information than you wanted to know).
这篇关于检测击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文