1

I'm trying to detect when the Control key has been released with this code:

#include <stdlib.h>
#include <unistd.h>
#include <gtk/gtk.h>

int main (int argc, char* argv[])
{
    const unsigned int pause_microseconds = 100 * 1000;

    gtk_init(&argc, &argv);

    GdkModifierType mask;
    while (1) {
        if(gdk_window_get_pointer(NULL, NULL, NULL, &mask) == NULL){
            puts("gdk_window_get_pointer failed");
            return EXIT_FAILURE;
        }

        unsigned control_has_been_released = (mask & GDK_CONTROL_MASK) != GDK_CONTROL_MASK;
        if (control_has_been_released) {
            puts("OK");
            return EXIT_SUCCESS;
        }

        if(usleep(pause_microseconds) != 0){
            puts("usleep failed");
            return EXIT_FAILURE;
        }
    }
}

But I get this output:

 gdk_window_get_pointer failed

Code compiled with:

 gcc -Wall -g ctrl.c -o ctrl `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`

I have tried running the code as root, but the result is the same.

Eleno
  • 2,864
  • 3
  • 33
  • 39

1 Answers1

1

gdk_window_get_pointer() cannot be called with a NULL first argument; i.e. you need to specify a window.

On top of that, in order to be able to see the changes, you probably need to run the event loop, see Getting keyboard modifiers state using Gnome libs (GDK) fetches initial state only.

Acorn
  • 24,970
  • 5
  • 40
  • 69