2

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.

James Bennet
  • 603
  • 2
  • 10
  • 22

3 Answers3

2

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.).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Does this actually work? In my testing (using the example from the select man page), select doesn't report that stdin is ready for reading until it sees a CR. – Joe Strout Jul 21 '17 at 14:15
  • @JoeStrout You probably have to update the terminal flags as well, using [`tcsetattr`](http://pubs.opengroup.org/onlinepubs/009695399/functions/tcsetattr.html). – Some programmer dude Jul 21 '17 at 19:08
  • Yes, that appears to be the case on my test machines at least (one OS X, one Ubuntu). – Joe Strout Jul 22 '17 at 20:40
0

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

Community
  • 1
  • 1
Jaffa
  • 12,442
  • 4
  • 49
  • 101
-3

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

Software_Designer
  • 8,490
  • 3
  • 24
  • 28