2

Possible Duplicate:
Access Keystrokes in C
Monitoring Keyboard keys in Ubuntu

I want to detect and time stamp every keydown and keyup event in a program (Yes, I mean keydown and keyup not just keypress) along with what key was pressed. I could do this by using an APi such as GTK, but I want to get as simple and low level as possible in order to avoid overhead from the libraries affecting the times as well as writing less code.

I have been googling this for a while and so far have found a ton of stuff about how to do it on Windows, which doesn't help me since I am using a Linux system, and how to detect a key press on Linux, but not keyup keydown.

Can anyone tell me what syscall, library, etc. I would need to use in order to capture the keydown and keyup events on a Linux system with a commandline program in C++? And if you have a link to a tutorial or a code example it would be most appreciated.

Community
  • 1
  • 1
njvb
  • 1,377
  • 3
  • 18
  • 36
  • http://stackoverflow.com/a/8277258/841108 – Basile Starynkevitch Oct 22 '12 at 19:18
  • http://stackoverflow.com/a/12747819/841108 – Basile Starynkevitch Oct 22 '12 at 19:22
  • duplicate of http://stackoverflow.com/q/12746869/841108 – Basile Starynkevitch Oct 22 '12 at 19:22
  • I have seen an ncurses example, but it does not specify how to differentiate between keyup and keydown, it just gets a keypress. I want to know if there is a way to do the former. – njvb Oct 22 '12 at 19:24
  • 1
    It appears that the only way to distinguish between keyup and keydown events without the overhead of a GUI (what I was trying to avoid) is to "hack the kernel" directly. The only function in ncurses that deals with this is getch() which through experimentation I have been led to believe only responds to keydown. There is no function in ncurses that allows you to detect when a key was released. – njvb Oct 22 '12 at 20:26

3 Answers3

3

You could read the raw device, I havn't tried this but this blog post looks promising: https://web.archive.org/web/20180108194231/http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/ (was http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/ but that site went offline)

so essentially you're reading directly from /dev/input/*

You can verify this works by running sudo cat /dev/input/eventX where X is one of the event devices listed in that directory (one of them will be your keyboard.. I'm sure there is a good way of finding which one programatically, but you can quickly find out by looking in /dev/input/by-id/ or just reading from one of those symlinks directly.)


NOTE: This will get you keyboard input all the time, not just when your window is in focus.. (your program wouldn't even need to be running in an xterm, or even a pty for that matter).

hexist
  • 5,151
  • 26
  • 33
2

Realistically, you aren't going to be able to do this without a library. If you want something that has very little overhead, I would suggest ncurses. If you absolutely must do it without a library, look at how ncurses implements it. It will, of course, be very complicated.

NKN
  • 6,482
  • 6
  • 36
  • 55
Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37
0

I would consider taking a look at OIS (Object Oriented Input System) library. It is quite easy to use, has a good OO design and it is cross-platform. For a tutorial and some code take a look at http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Using+OIS.

Mihai Bişog
  • 998
  • 9
  • 24