0

I'm wriitng an app in c that will be using Xlib, GTK. I want to create a hotkey that should interrupt the application from it's activity and putting it into a dormant state.

I have done some reading and I believe I cannot use: XGrabKey because a window manager is running and is already doing that

is there any other way from gtk or Xlib I can press a combination of keys to send an order to my application when the application does not have focus?

I'm trying out the following code but it's not working:

Display* dpy = XOpenDisplay(0);
    Window root = DefaultRootWindow(dpy);
    XEvent ev;
    unsigned int modifiers = ControlMask | ShiftMask;
    int keycode = 45;
    int pointer_mode = GrabModeAsync;
    int keyboard_mode = GrabModeAsync;
    XGrabKey(dpy, AnyKey, modifiers, root, 0, pointer_mode, keyboard_mode);
    XSelectInput(dpy, root, KeyPressMask);
    while(1)
    {
      XNextEvent(dpy, &ev);
      if (ev.type == KeyPress)
      {
        printf("key has been pressed\n");
        break;
      }
      else printf("event type is %i\n",ev.type);

    }

but I get: Gdk-ERROR **: The program 'test' received an X Window System error. This probably reflects a bug in the program. The error was 'BadAccess (attempt to access private resource denied)'. (Details: serial 7 error_code 10 request_code 33 minor_code 0) (Note to programmers: normally, X errors are reported asynchronously; that is, you will receive the error a while after causing it. To debug your program, run it with the --sync command line option to change this behavior. You can then get a meaningful backtrace from your debugger if you break on the gdk_x_error() function.) aborting...

thanks

Adel Ahmed
  • 638
  • 7
  • 24
  • Possible duplicate of [Global Hotkey with X11/Xlib](http://stackoverflow.com/questions/4037230/global-hotkey-with-x11-xlib) – Nathan Tuggy Nov 20 '15 at 01:55
  • here's what I'm trying out: Display* dpy = XOpenDisplay(0); Window root = DefaultRootWindow(dpy); XEvent ev; unsigned int modifiers = ControlMask | ShiftMask; int keycode = 45; int pointer_mode = GrabModeAsync; int keyboard_mode = GrabModeAsync; XGrabKey(dpy, AnyKey, modifiers, root, 0, pointer_mode, keyboard_mode); XSelectInput(dpy, root, KeyPressMask); while(1) { XNextEvent(dpy, &ev); if (ev.type == KeyPress) { printf("key has been pressed\n"); break; } else printf("event type is %i\n",ev.type); – Adel Ahmed Nov 20 '15 at 20:26
  • I get: Gdk-ERROR **: The program 'test' received an X Window System error. This probably reflects a bug in the program. The error was 'BadAccess (attempt to access private resource denied)'. (Details: serial 7 error_code 10 request_code 33 minor_code 0) (Note to programmers: normally, X errors are reported asynchronously; that is, you will receive the error a while after causing it. To debug your program, run it with the --sync command line option to change this behavior. You can then get a meaningful backtrace from your debugger if you break on the gdk_x_error() function.) – Adel Ahmed Nov 20 '15 at 20:31

1 Answers1

0

This means another program has Control+Shift+"-" already bound. Try grabbing another key combination.

exebook
  • 32,014
  • 33
  • 141
  • 226