Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-27-2009, 12:56 AM   #1
LQ Newbie
Registered: Jan 2009
Posts: 14

Rep: Reputation: 0
Xlib mouse events and ButtonPressMask

I have written a simple program which will report key press and release events for a particular window. In my case, it is mostly the terminal since I invoke the program from the terminal. I am able to get the key press and release events taking place in the terminal window (I have used XSelectInput() with KeyPressMask and KeyReleaseMask on the terminal) but the same is not working with ButtonPress and ButtonRelease. Not just these, but any events related to the mouse are not being reported. Any idea why this is happening?


int main() {
Display *display = XOpenDisplay(NULL);
KeySym k;
int revert_to;
Window window;
XEvent event;

XGetInputFocus(display, &window, &revert_to);
XSelectInput(display, window, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);

switch (event.type) {

case KeyPress : printf("Key Pressed\n"); break;
case KeyRelease : printf("Key Released\n"); break;
case ButtonPress : printf("Button Pressed\n"); break;
case ButtonRelease : printf("Button Released\n"); break;
case EnterNotify : printf("Enter\n"); break;
return 0;
Old 11-27-2009, 07:16 AM   #2
Registered: Feb 2003
Location: Germany
Distribution: SLES, SuSe, Ubuntu, HPUX
Posts: 38

Rep: Reputation: 18
reacting on mouse pointer events

The mouse (pointer) generates events when it moves, when it crosses window borders and when buttons are pressed.

When one wants to obtain the final pointer position XQueryPointer() is a good function to call.

ButtonPress and ButtonRelease events are fired when the pointer buttons are presses and released. A mouse (pointing device) can have many buttons, these buttons are mapped on a bit mask that can be changed by XSetPointerMapping(); one can read this mapping by the XGetPointerMapping() function call.

The System automatically grabs the pointer between the ButtonPress and ButtonRelease events and presents it to the client window where the button was pressed. So the client always receives two button events (press, release). Only one client can select this button events.

The distribution of the ButtonRelease events can be controlled by XSelectInput calls where one can select OwnerGrabButtonMask. This will cause the ButtonRelease event to be send to the same application where the presses event took place.

XEvent event;
unsigned int button;
display = XOpenDisplay(...
screen = DefaultScreen( display...
XSelectInput(display, RootWindow(display, screen), ButtonPressMask | ButtonReleaseMask | ExpousureMask,
gc = XCreateGC( display
XNextEvent(display, &event) // trap any event
switch( event.type)
case ButtonPress:
button = event.xbutton.button;
...XCheckTypedEvent(display, ButtonPress, event)
// process other button pressed events..
XMskEvent(display ButtonReleaseMask, &event)
if( event.xbutton.button == button)
// process released event

For details please also take a look into O'Reilly's XLib Programming Manual. Here you can find a detailed description with some examples.
Good luck


events, mouse, pointer, xlib

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse events geogauci Linux - Software 0 03-04-2009 02:59 PM
Keyboard events interrupting mouse events. miner49er Linux - Hardware 3 11-04-2008 05:16 AM
Mouse interrupting keyboard events, can't type while mouse is moving sandaili Linux - Hardware 0 08-04-2008 07:56 PM
Xlib custom events phanna Programming 1 09-12-2005 05:50 PM
Selecting Xlib events twice???? pihu Programming 1 07-15-2005 03:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration