简体   繁体   English

在控制台应用程序中检测按键?

[英]Detect keypress in console application?

I need to detect a keypress in a console application, without prompting the user. 我需要在不提示用户的情况下检测控制台应用程序中的按键。 Basically, my app is normally a daemon that listens to a special input device, but i need to simulate it on a dev box using the keyboard in interactive mode. 基本上,我的应用程序通常是监听特定输入设备的守护进程,但是我需要在交互模式下使用键盘在开发盒上对其进行仿真。 How can I do this? 我怎样才能做到这一点? - Im on a Linux system. -我在Linux系统上。

如果在等待输入时不能阻塞,则可以使用例如select来检查STDIN_FILENO文件描述符是否已准备好读取,如果可以,则可以使用常规输入函数( scanffgets std::getline等) )。

You check this answer which explains how to read from the input events ( usually /dev/input/event0 ) 您检查此答案 ,该答案说明了如何从输入事件(通常是/dev/input/event0 )中/dev/input/event0

Or directly check the answer's source : 或直接检查答案的来源:

Credits do not go to me, this code is taken from the Ventriloctrl hack to get keystrokes. 不用谢我,此代码摘自Ventriloctrl黑客以获取击键。 http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz

This text explaines hw to do such a thing. 本文解释了硬件如何做这样的事情。 http://thc.org/papers/writing-linux-kernel-keylogger.txt http://thc.org/papers/writing-linux-kernel-keylogger.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM