Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 893
dragonroot Avatar asked Nov 29 '25 22:11

dragonroot


1 Answers

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.

like image 105
ninjalj Avatar answered Dec 01 '25 12:12

ninjalj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!