Detect keypress in console application?

旧巷老猫 提交于 2021-02-19 05:31:59

问题


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.


回答1:


If you can't block while waiting for input, then you can use e.g. select to check if the STDIN_FILENO file descriptor is ready for reading, and if it is then you can use normal input functions (scanf, fgets std::getline, etc.).




回答2:


You check this answer which explains how to read from the input events ( usually /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. http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz




回答3:


This text explaines hw to do such a thing. http://thc.org/papers/writing-linux-kernel-keylogger.txt



来源:https://stackoverflow.com/questions/12245081/detect-keypress-in-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!