views:

693

answers:

4

I would like to write a simple C program that will perform different actions based on both "key down" and "key up" events. This program will be run from inside rxvt.

What library or mechanism should I use to access both key presses and releases? Is it true that reading /dev/tty will only provide key releases? Is this also true for termcap, terminfo, ncurses, and slang? Is there a way to achieve this from within a terminal application?

+1  A: 

Consider using ncurses. This is an easy but not crossplatfom way. You might also find useful its features besides keypressing.

Basilevs
+1  A: 

I don't think ncurses will work. The link Basilevs provided talks about the "up arrow" key and the "down arrow" key, not a key press and release.

A: 

This won't work in the general case. ANSI terminals (from which all emulators descend) represent key "press" events only. They don't record down/up events independently.

If you need low level keyboard event access, the proper environment is really a GUI program. Even a web application will have cleaner access to key events than a terminal.

Andy Ross
A: 

The following links may be of some assistance in using the keyboard raw mode which will give you access to the keyboard events rather than just key releases.

  1. This Linux Journal article
  2. This article on SourceForge
  3. And this HOWTO for when it all goes bad

Raw mode does have the disadvantage that you have to do your own conversion from scan codes to characters though.

Andrew Edgecombe