Receiving Key Press and Key Release Events in Linux Terminal Applications

Detecting keyboard key press and release on Linux

Your kbhit (and, apparently, even Windows' original kbhit) doesn't detect whether a key is pressed, but only whether there is something new to read on stdin. This will only be the case 25 times a second or so, depending on your autorepeat setting. Making stdout unbuffered in your example code will make that more obvious (000000W00000000W000000000W)

How can I fix this so that when a keyboard key is kept pressed, kbhit() always returns 0?

This is impossible to do portably. In Linux, it can be done using the device files under /dev/input
See the example program below. It will register all keys, even a lone SHIFT Note that this is effectively a keylogger, so you will have to run as root (or make the program setuid). It will then register all keystrokes, even when it doesn't have keyboard focus. Yikes!

Note: the example below is based on keystate.c by Kevin Cox

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <glob.h>
#include <linux/input.h>
#include <sys/stat.h>
#include <fcntl.h>

/* Return -1 if no key is being pressed, or else the lowest keycode
(c.f. linux/input-event-codes.h) of all the keys that are being pressed */
int keycode_of_key_being_pressed() {
FILE *kbd;
glob_t kbddev; // Glob structure for keyboard devices
glob("/dev/input/by-path/*-kbd", 0, 0, &kbddev); // Glob select all keyboards
int keycode = -1; // keycode of key being pressed
for (int i = 0; i < kbddev.gl_pathc ; i++ ) { // Loop through all the keyboard devices ...
if (!(kbd = fopen(kbddev.gl_pathv[i], "r"))) { // ... and open them in turn (slow!)
perror("Run as root to read keyboard devices");
exit(1);
}

char key_map[KEY_MAX/8 + 1]; // Create a bit array the size of the number of keys
memset(key_map, 0, sizeof(key_map)); // Fill keymap[] with zero's
ioctl(fileno(kbd), EVIOCGKEY(sizeof(key_map)), key_map); // Read keyboard state into keymap[]
for (int k = 0; k < KEY_MAX/8 + 1 && keycode < 0; k++) { // scan bytes in key_map[] from left to right
for (int j = 0; j <8 ; j++) { // scan each byte from lsb to msb
if (key_map[k] & (1 << j)) { // if this bit is set: key was being pressed
keycode = 8*k + j ; // calculate corresponding keycode
break; // don't scan for any other keys
}
}
}

fclose(kbd);
if (keycode)
break; // don't scan for any other keyboards
}
return keycode;
}

void main()
{
setvbuf(stdout, NULL, _IONBF, 0); // Set stdout unbuffered
while (1) {
int key = keycode_of_key_being_pressed();
printf((key < 0 ? "no key\n" : "keycode: %d\n"), key);
if (key == KEY_X)
exit(0);
}
}

Key pressed and key released interrupts in C

You cannot do this in a portable manner. Terminals (and emulators of those such as xterm) give you only the key that was pressed, not the release events. Graphical user interfaces often provide the ability to receive separate press- and release-events.

Terminal emulators running in a graphical environment compose those events into individual characters. As read in the graphical environment, those are key symbols, which may contain characters. In addition to the press- and release-events for the key events themselves, you can have modifiers such as shift-, control- and meta-modifiers which are separate events. If you run xev, you can see these separate events.

After composing these events into a character, the terminal emulator may send it to your application as a series of data bytes, e.g., in UTF-8 encoding. When you use getch(), the ncurses library reads those bytes, and puts it together again into a character. In between those two (the terminal emulator and application) are the pseudo-terminal and its translation, which both terminal emulator and application must manipulate.

If you are not running in a graphical environment, there are (not always) other ways than graphical applications such as xev which can read directly the key press/release events. The Linux console supports that. See for example the links cited in Receiving key press and key release events in Linux terminal applications?

Detecting keydown and keyup events on Linux C++

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

Accessing Keys from Linux Input Device

Open the input device,

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>

static const char *const evval[3] = {
"RELEASED",
"PRESSED ",
"REPEATED"
};

int main(void)
{
const char *dev = "/dev/input/by-path/platform-i8042-serio-0-event-kbd";
struct input_event ev;
ssize_t n;
int fd;

fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}

and then read keyboard events from the device:

    while (1) {
n = read(fd, &ev, sizeof ev);
if (n == (ssize_t)-1) {
if (errno == EINTR)
continue;
else
break;
} else
if (n != sizeof ev) {
errno = EIO;
break;
}

The above snippet breaks out from the loop if any error occurs, or if the userspace receives only a partial event structure (which should not happen, but might in some future/buggy kernels). You might wish to use a more robust read loop; I personally would be satisfied by replacing the last break with continue, so that partial event structures are ignored.

You can then examine the ev event structure to see what occurred, and finish the program:

        if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2)
printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);

}
fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));
return EXIT_FAILURE;
}

For a keypress,

  • ev.time: time of the event (struct timeval type)

  • ev.type: EV_KEY

  • ev.code: KEY_*, key identifier; see complete list in /usr/include/linux/input.h

  • ev.value: 0 if key release, 1 if key press, 2 if autorepeat keypress

See Documentation/input/input.txt in the Linux kernel sources for further details.

The named constants in /usr/include/linux/input.h are quite stable, because it is a kernel-userspace interface, and the kernel developers try very hard to maintain compatibility. (That is, you can expect there to be new codes every now and then, but existing codes rarely change.)



Related Topics



Leave a reply



Submit