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

  • Home
  • SEARCH
  • 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 7846385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:33:56+00:00 2026-06-02T17:33:56+00:00

I have some code that uses XGrabButton to capture mouse clicks. I want it

  • 0

I have some code that uses XGrabButton to capture mouse clicks. I want it to always capture all clicks on the specified buttons, regardless of any other concerns. It currently uses the following invocation:

XSelectInput(display, window, ButtonPressMask);
XGrabButton(display, Button2, AnyModifier, window, True,
    ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
XGrabButton(display, Button3, AnyModifier, window, True,
    ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
XGrabButton(display, Button4, AnyModifier, window, True,
    ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);

However, pressing and holding button 1 (the left mouse button, which is not captured by my code) causes clicks on the other buttons to not be captured. How do I prevent this from happening?

Edit for clarification:

  • I want to capture buttons 2-4 always, and button 1 never.
  • The above invocation captures buttons 2-4 just fine normally.
  • It does NOT capture button 1 (left click).
  • It will NOT capture buttons 2-4 while button 1 is held down.

How do I make it capture buttons 2-4 while button 1 is held down?

  • 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-06-02T17:33:58+00:00Added an answer on June 2, 2026 at 5:33 pm

    Hope there is more to the code if it acts as you say:

    “However, pressing and holding button 1 (the left mouse button, which is not captured by my code) causes clicks on the other buttons to not be captured.“

    The line:

    XSelectInput(display, window, ButtonPressMask);
    

    will cause all buttons to be captured. The additional XGrabButton calls are (redundant?) as you do not specify any parameters that differ from the default ones.

    However; if it in fact is how you say, that even with that code Button1 is not captured, or you have some other code where i.e. XSelectInput is not called – I’ll take that as base to start with.


    Event distribution

    Every window has a event queue. An event is sent to a window when it is generated in that window. But, the window only receives the event if it has selected it or it is always selected. Or as a side effect to an Xlib routine. Only when it receives the event it is placed in the queue.

    If we use XSelectInput() and select most masks, /usr/include/X11/X.h EVENT DEFINITIONS, and use XNextEvent() to have a look we’ll typically see something like this:

     ...
     19: Event EnterNotify     # Window Entry/Exit Events
     20: Event KeymapNotify    # X send after every EnterNotify and FocusIn event
     21: Event MotionNotify    # Keyboard and Pointer Events
     22: Event ButtonPress   2 # Button 2 down
     23: Event ButtonRelease 2 # Button 2 up
     24: Event ButtonPress   1 # Button 1 down
     25: Event ButtonPress   2 # Button 1 and 2 down
     ...
    

    If we remove ButtonPressMask and ButtonReleaseMask from XSelectInput and use XGrabButton() to grab events for every button except 1 and run the program again the events is mapping a tendency:

     22: Event LeaveNotify       # Button 1 pressed causes "blurring" of window.
     23: Event EnterNotify       # Button 1 released
     24: Event KeymapNotify
     25: Event ButtonPress   3   # Button 3 down
     26: Event ButtonPress   1   # Button 3 + 1 down; 1 registers as Press 
                                 #                    even if not tracked.
     27: Event ButtonRelease 3   # Button 3 up
     28: Event MotionNotify
     29: Event ButtonRelease 1   # Button 1 up
     30: Event LeaveNotify       # Button 1 pressed, 1+2 pressed, 1+2+3 …
     31: Event EnterNotify       # Button 1 released, 2 released, …
     32: Event KeymapNotify
     33: Event ButtonPress   2
     34: Event ButtonPress   3
     35: Event ButtonPress   1
     36: Event ButtonRelease 2
     37: Event ButtonRelease 3
    

    We observe that the button events, when pressing multiple, inherits status of first. You can also see this with ltrace, strace, xev etc. I.e. xev registers buttons that are not captured internally. As mentioned earlier; events are sent to the window, but only registered are received. That is with exception of some side effects as one for example can see when:

    • first button pressed is set to capture; and then one that is not – the one that is not will also be queued in the event list.
    • first button pressed is not set to capture; and then one that is – then neither is queued.

    A button press on one that is not set to capture results in LeaveNotify, (window is left) – and parallel events are blocked.


    How to solve

    There are so many things to take into account here. All depending on the logic of the rest of the code project etc. There are however some basics. The simplest being to capture all buttons and flag and track; XNextEvent.

    static const char *event_names[] = {
        "", "", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease",
        "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut",
        "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify",
        "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",
        "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
        "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify",
        "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify",
        "ClientMessage", "MappingNotify"
    };
    
    /* ... */
    
    Display *dpy;
    XEvent  ev;
    int k = 0;
    
    /* ... */
    
    for (;;) {
        XNextEvent(dpy, &ev);
        if ((ev.type == ButtonPress || ev.type == ButtonRelease) &&
                                ev.xbutton.button == Button1) {
            fprintf(stderr,
                    "%04d: BLOCK!     %-18s %d\n",
                    k++, event_names[ev.type],
                    ev.xbutton.button);
            continue;
        }
        fprintf(stderr, "%04d: PASS! Work %-18s", k++, event_names[ev.type]);
        switch (ev.type) {
            case ButtonPress:
            case ButtonRelease:
                fprintf(stderr, " %d", ev.xbutton.button);
                break;
        }
        putchar('\n');
    }
    

    Resulting in something like:

    0005: PASS! Work FocusIn           
    0006: BLOCK!     ButtonPress        1
    0007: BLOCK!     ButtonRelease      1
    0008: BLOCK!     ButtonPress        1
    0009: BLOCK!     ButtonRelease      1
    0010: BLOCK!     ButtonPress        1
    0011: PASS! Work ButtonPress        3 # Button 1 is also down
    0012: PASS! Work ButtonRelease      3
    0013: BLOCK!     ButtonRelease      1
    0014: PASS! Work ButtonPress        3
    0015: PASS! Work ButtonRelease      3
    0016: BLOCK!     ButtonPress        1
    0017: PASS! Work ButtonPress        3
    0018: PASS! Work ButtonRelease      3
    0019: BLOCK!     ButtonRelease      1
    0020: PASS! Work ButtonPress        2
    0021: PASS! Work ButtonPress        3
    0022: BLOCK!     ButtonPress        1
    0023: BLOCK!     ButtonRelease      1
    0024: PASS! Work ButtonRelease      2
    0025: PASS! Work ButtonRelease      3
    0026: BLOCK!     ButtonPress        1
    0027: PASS! Work ButtonPress        3
    0028: PASS! Work ButtonRelease      3
    0029: PASS! Work ButtonPress        2
    0030: PASS! Work ButtonRelease      2
    0031: BLOCK!     ButtonRelease      1
    0032: PASS! Work FocusOut          
    

    Another way could be to use XIfEvent, XCheckIfEvent()etc. It all depends on structure. One thing to remember / check is that some of the functions; i.e. XIfEvent does not remove the event from queue if it is not a match! So if you implement a predicate function that return False if event is i.e. Button1Press – that event will remain in queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses an Informix 11.5 database that I want to
I have some code that uses dispatchEvent to simulate clicks and the same exact
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that uses the Oracle function add_months to increment a Date
I have some code that uses Open XML to open up a .docx file,
I have some code that uses ODP.Net using (OracleConnection connection = new OracleConnection(connectionString)) {
I have some Java code that uses curly braces in two ways // Curly
I have some old code that uses qsort to sort an MFC CArray of
We have some code kicking around that uses this old internal Sun package for
I have some c(++) code that uses sprintf to convert a uint_64 to a

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.