Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:13:31+00:00 2026-05-17T21:13:31+00:00

My goal is to have a program that sleeps in the background but can

  • 0

My goal is to have a program that sleeps in the background but can be activated by the user via some “hotkey”. From digging around the Xlib manual and the Xlib O’reilly manual, I gather that the correct way to to this is with XGrabKey. However my understanding of the process is incorrect as a simple proof of concept does not work.

My understanding is that if I call XGrabKey with the root window as the grab_window, and owner_events false, then whenever my hotkey is pressed the event will be sent only to the root window. If I then select KeyPress events from the root window, and then listen for X events, I should get a key press event when the hotkey is pressed. I’ve pasted a minimal example below.

What I expect is that when the program is run, regardless of what window has focus, if Ctrl+Shift+K is pressed, my program should output “Hot key pressed!” in the console, and then terminate.

Furthermore, it is my understanding that if the XGrabKey fails, the default error handler will display a message, and since it does not I am assuming that the call succeeds.

Obviously, my understanding is flawed somehow. Can anyone point me in the right direction?

#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>


using namespace std;


int main()
{
    Display*    dpy     = XOpenDisplay(0);
    Window      root    = DefaultRootWindow(dpy);
    XEvent      ev;

    unsigned int    modifiers       = ControlMask | ShiftMask;
    int             keycode         = XKeysymToKeycode(dpy,XK_Y);
    Window          grab_window     =  root;
    Bool            owner_events    = False;
    int             pointer_mode    = GrabModeAsync;
    int             keyboard_mode   = GrabModeAsync;

    XGrabKey(dpy, keycode, modifiers, grab_window, owner_events, pointer_mode,
             keyboard_mode);

    XSelectInput(dpy, root, KeyPressMask );
    while(true)
    {
        bool shouldQuit = false;
        XNextEvent(dpy, &ev);
        switch(ev.type)
        {
            case KeyPress:
                cout << "Hot key pressed!" << endl;
                XUngrabKey(dpy,keycode,modifiers,grab_window);
                shouldQuit = true;

            default:
                break;
        }

        if(shouldQuit)
            break;
    }

    XCloseDisplay(dpy);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:13:32+00:00Added an answer on May 17, 2026 at 9:13 pm

    Your program works here. My guess is you have another modifier active, such as NumLock. GrabKey only works on the exact modifier mask.

    For example here is some (GPL) code from metacity window manager

    /* Grab/ungrab, ignoring all annoying modifiers like NumLock etc. */
    static void
    meta_change_keygrab (MetaDisplay *display,
                         Window       xwindow,
                         gboolean     grab,
                         int          keysym,
                         unsigned int keycode,
                         int          modmask)
    {
      unsigned int ignored_mask;
    
      /* Grab keycode/modmask, together with
       * all combinations of ignored modifiers.
       * X provides no better way to do this.
       */
    
      meta_topic (META_DEBUG_KEYBINDINGS,
                  "%s keybinding %s keycode %d mask 0x%x on 0x%lx\n",
                  grab ? "Grabbing" : "Ungrabbing",
                  keysym_name (keysym), keycode,
                  modmask, xwindow);
    
      /* efficiency, avoid so many XSync() */
      meta_error_trap_push (display);
    
      ignored_mask = 0;
      while (ignored_mask <= display->ignored_modifier_mask)
        {
          if (ignored_mask & ~(display->ignored_modifier_mask))
            {
              /* Not a combination of ignored modifiers
               * (it contains some non-ignored modifiers)
               */
              ++ignored_mask;
              continue;
            }
    
          if (meta_is_debugging ())
            meta_error_trap_push_with_return (display);
          if (grab)
            XGrabKey (display->xdisplay, keycode,
                      modmask | ignored_mask,
                      xwindow,
                      True,
                      GrabModeAsync, GrabModeSync);
          else
            XUngrabKey (display->xdisplay, keycode,
                        modmask | ignored_mask,
                        xwindow);
    
          if (meta_is_debugging ())
            {
              int result;
    
              result = meta_error_trap_pop_with_return (display, FALSE);
    
              if (grab && result != Success)
                {      
                  if (result == BadAccess)
                    meta_warning (_("Some other program is already using the key %s with modifiers %x as a binding\n"), keysym_name (keysym), modmask | ignored_mask);
                  else
                    meta_topic (META_DEBUG_KEYBINDINGS,
                                "Failed to grab key %s with modifiers %x\n",
                                keysym_name (keysym), modmask | ignored_mask);
                }
            }
    
          ++ignored_mask;
        }
    
      meta_error_trap_pop (display, FALSE);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that occasionally needs to scan some directories recursively (an improvement
Ok, Just some background... I have a fully functioning game that uses the canvas
I have a program that can generate video in real time. Now I would
My goal is to have a jQuery datepicker pop up when the user clicks
My goal is to have a user select a year and a month. Translate
having some issues with a networking assignment. End goal is to have a C
For about a year I have been thinking about writing a program that writes
My goal is to write a program that handles an arbitrary number of tasks
I have a program (that I did not write) which is not designed to
My goal is to make a flash/as3 program that would pull multiple sets of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.