问题
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