Getting keyboard options using Gnome libs (GDK) selects only the initial state

I am trying to get the current state of keyboard modifiers through the gnome GDK or GTK library in order to implement the gnome accessibility shell extension that shows this state.

I know how to get its state using xlib, but there is no full binding for gns gjs.

The code below gets only the initial state. It does not update state.

/*
 * compiling: gcc `pkg-config --cflags gdk-3.0` -o gdk_mod gdk_mod.c `pkg-config --libs gdk-3.0`
 */

#include <gdk/gdk.h>

int main (int argc, char **argv) {

    gdk_init(&argc, &argv);

    GdkDisplay * disp;
    disp = gdk_display_open(NULL);
    if (disp!=NULL) g_printf("display connected!\n");

    GdkKeymap * kmap;
    kmap = gdk_keymap_get_for_display(disp);

    guint state;
    state = gdk_keymap_get_modifier_state(kmap);
    g_printf("mod state: %x\n", state);

    while (1) {
        g_usleep(1000000);
        //kmap = gdk_keymap_get_for_display(disp);
        state = gdk_keymap_get_modifier_state(kmap);
        g_printf("mod state: %x\n", state);
    }

}

Here's an example of CAPS-locked output active, then inactive, but not changing:

$ ./gdk_mod 
display found!
mod state: 2
mod state: 2
mod state: 2
mod state: 2
mod state: 2
^C

Currently used by Kubuntu 15.04.

What is wrong with my code?

+2
source share
2 answers

GTK + . GLib. gtk_main(), . , , , , .

GDK - GTK + gtk_init() gtk_main(). GDK , , . , , , .

g_usleep(), , . g_timeout_add(). , g_timeout_add(), , , , , GLib .

+2
  • , , andlabs . GTK gtk_init() gtk_main() .

    /*
     * compiling: gcc `pkg-config --cflags gtk+-3.0` -o gtk_xkbmod3 gtk_xkbmod3.c `pkg-config --libs gtk+-3.0`
     */
    
    #include <gtk/gtk.h>
    
    static void update(GdkKeymap * kmap) {
        guint state;
        state = gdk_keymap_get_modifier_state(kmap);
        g_printf("%i\n", state);
    }
    
    int main (int argc, char **argv) {
    
        gtk_init(&argc, &argv);
    
        GdkKeymap * kmap;
        kmap = gdk_keymap_get_default();
    
        g_timeout_add_seconds(1, (GSourceFunc) update, kmap);
    
        gtk_main();
    
    }
    
  • GDK GLib GMainLoop.

    /*
     * compiling: gcc `pkg-config --cflags gdk-3.0` -o gdk_xkbmod4 gdk_xkbmod4.c `pkg-config --libs gdk-3.0`
     */
    
    #include <gdk/gdk.h>
    
    GMainLoop *mainloop;
    
    static void update(GdkKeymap * kmap) {
        guint state;
        state = gdk_keymap_get_modifier_state(kmap);
        g_printf("%i\n", state);
    }
    
    int main (int argc, char **argv) {    
    
        gdk_init(&argc, &argv);
    
        GdkKeymap * kmap;
        kmap = gdk_keymap_get_default();
    
        g_timeout_add_seconds(1, (GSourceFunc) update, kmap);
    
        mainloop = g_main_loop_new(g_main_context_default(), FALSE);
        g_main_loop_run(mainloop);    
    }
    

:

+1

Source: https://habr.com/ru/post/1532486/


All Articles