ubuntuのコマンドラインでシフトキーの状態を得る

 前からシフトキーのオンオフでスクリプトの動作を分岐したいと思っていたけど、それらしきプログラムは見つからなかった。
 で、英語でいろいろ検索してみたら発見。
stackoverflow.com

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdbool.h>
#include <stdio.h>

int main()
{
    Display* dpy = XOpenDisplay(NULL);
    char keys_return[32];
    XQueryKeymap( dpy, keys_return );
    KeyCode kc2 = XKeysymToKeycode( dpy, XK_Shift_L );
    bool bShiftPressed = !!( keys_return[ kc2>>3 ] & ( 1<<(kc2&7) ) );
    printf("Shift is %spressed\n", bShiftPressed ? "" : "not ");
    XCloseDisplay(dpy);
}

 gccで-lX11オプションをつけてコンパイル。確かに動作する。ただし左シフトだけ。c言語はまったく素養がないしビット演算もよくわかっていないけど、想像力を膨らませて、左右どちらでも動作し、終了コードを返すようにしてみた。

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdbool.h>
#include <stdio.h>

int main()
{
    Display* dpy = XOpenDisplay(NULL);
    char keys_return[32];
    XQueryKeymap( dpy, keys_return );
    KeyCode kc2 = XKeysymToKeycode( dpy, XK_Shift_L );
    bool bShiftPressed = !!( keys_return[ kc2>>3 ] & ( 1<<(kc2&7) ) );
//    printf("LShift is %spressed\n", bShiftPressed ? "" : "not ");
    KeyCode kc3 = XKeysymToKeycode( dpy, XK_Shift_R );
    bool cShiftPressed = !!( keys_return[ kc3>>3 ] & ( 1<<(kc3&7) ) );
//   printf("RShift is %spressed\n", cShiftPressed ? "" : "not ");
	bool stat = bShiftPressed|cShiftPressed;
    printf("Shift is %s\n", stat ? "on" : "off ");
    XCloseDisplay(dpy);
    return(stat);
}

 動作状況。

 シフトキーの状態によってkc2の値が変わるのかと思って出力させてみたけど、どっちでも同じ。どうやらkc2はキーの番号を入れている変数で、keys_returnで状態を得ているらしい。

keys_return
どのキーが押されたかを示すバイト配列が返される。 それぞれのビットはキーボードの 1 つのキーを表す。

Manpage of XChangeKeyboardControl