dragonroot
dragonroot

Reputation: 5821

Check if the shift key is currently being held in the Linux text/fb console

How could one check whether the SHIFT key is currently being held while in the Linux console? By the Linux console I mean the real text/framebuffer one, not an xterm.

Preferably with only the built-in/standard shell commands, if possible.

Upvotes: 3

Views: 3395

Answers (1)

ninjalj
ninjalj

Reputation: 43688

There is no command that I know of that gets the shift state of the keyboard. That said, looking at console_ioctl(4), there is a ioctl request for that: TIOCLINUX, subcode=6.

So you could write a simple C program:

#include <stdio.h>
#include <stdlib.h>

#include <sys/ioctl.h>

int main()
{
    char shift_state;

    shift_state = 6;
    if (ioctl(0, TIOCLINUX, &shift_state) < 0) {
            perror("ioctl TIOCLINUX 6 (get shift state)");
            exit(1);
    }
    printf("%x\n", shift_state);
    return 0;
}

The result can be interpreted in accordance to /usr/src/linux/include/linux/keyboard.h:

#define KG_SHIFT        0
#define KG_CTRL         2
#define KG_ALT          3
#define KG_ALTGR        1
#define KG_SHIFTL       4
#define KG_KANASHIFT    4
#define KG_SHIFTR       5
#define KG_CTRLL        6
#define KG_CTRLR        7
#define KG_CAPSSHIFT    8

The above is a shift amount, so Shift is 1, AltGr is 2, Ctrl is 4, and so on.

Upvotes: 7

Related Questions