25

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 S-Lang? Is there a way to achieve this from within a terminal application?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

13

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
  • 39,594
  • 3
  • 35
  • 61
  • 5
    +1 Thanks for this answer, I needed raw keyboard access in a terminal program and this was the solution! But one should remember that using raw keyboard access, your program doesn't "behave" like a classical terminal program. For example when running it on a remote machine, you can't register the key events. Just keep this in mind. – leemes Mar 20 '11 at 22:31
  • 3
    Note that these only work if you're connected to the actual console -- if you're using a pseudo-terminal (eg. a terminal window) they won't work. – Chris Dodd Apr 04 '19 at 00:05
  • All three links still work (even the 1995 [Linux Journal](https://en.wikipedia.org/wiki/Linux_Journal) one), but perhaps make it more future-proof? – Peter Mortensen Feb 10 '23 at 20:31
  • The Linux Journal article is somewhat unclear. Is `s+0x80` addition? Or two separate bytes? It isn't what is send from a PS/2 keyboard. For instance, by default (presumably scan set 2), pressing key 'F3' results in a scan code of 0x04. Release of F3 results in two (separate) scan codes, 0xF0 and 0x04, being send over the wire (0xF0 is send first). – Peter Mortensen Feb 10 '23 at 22:31
  • This has been observed (using a [logic analyser](https://en.wikipedia.org/wiki/Logic_analyzer) and [PulseView](https://sigrok.org/wiki/PulseView)) both for ancient PS/2 keyboards (e.g., [BTC 5349](https://www.reddit.com/r/keyboards/comments/ohpmow/btc_5349_restored_by_me/) (1983)) and a 2021 USB keyboard (one that is able to operate in PS/2 mode). – Peter Mortensen Feb 10 '23 at 23:29
9

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
  • 11,699
  • 1
  • 34
  • 31
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user172103
  • 31
  • 4