Loading...

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

Answer #1 100 %

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 
#include 

#include 

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.

You’ll also like:


© 2022 CodeForDev.com -