On 04/08/13 18:46, Greg Ercolano wrote:
>       My X11's a bit rusty, but there might be a way to acquire the realtime
>       capslock state by a different X library call

        FWIW, 'xset q' will tell you the state of the keyboard LEDs, eg:

$ xset q | grep LED
  auto repeat:  on    key click percent:  0    LED mask:  00000002

        On my system, toggling capslock alternates between 00000002 / 00000003,
        so I think bit 0 is the capslock key

        So you could look at the source for xset to see what it's doing for 

        Also, xkbvleds(1) accurately shows the state of the capslock LED,
        and like all things in X, there's quite a bit of complexity getting 
this info.
        It's maybe something to do with XkbGetIndicatorState(3). eg.

                unsigned n;
                XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
                caps_state = (n & 0x01) == 1;

        ..where d is the X11 Display*, ie. fl_display

        I tried the following FLTK code with the above X11 only code
        and it seemed to work more reliably.

#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/x.H>
#include <X11/XKBlib.h>
// demonstrate how to read the realtime capslock state in FLTK under X windows 
-erco 4/8/13
//     Reads capslock state on KEYUP and also with an FLTK timer.
class MyWindow : public Fl_Window {
     MyWindow(int W,int H) : Fl_Window(W,H,"Watch CAPSLOCK") { }
     int handle(int e) {
         switch (e) {
             case FL_KEYUP:
                unsigned n;
                XkbGetIndicatorState(fl_display, XkbUseCoreKbd, &n);
                printf("KEYHIT: Capslock is %s\n", (n & 1) ? "on" : "off");
void Timer_CB(void*) {
    unsigned n;
    XkbGetIndicatorState(fl_display, XkbUseCoreKbd, &n);
    printf(" TIMER: Capslock is %s\n", (n & 1) ? "on" : "off");
    Fl::repeat_timeout(1.0, Timer_CB);

int main() {
    MyWindow *win = new MyWindow(256,256);
    Fl::add_timeout(1.0, Timer_CB);

fltk mailing list

Reply via email to