LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 11-26-2009, 11:56 PM   #1
abhinav.zoso
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?

Code:
#include
#include
#include
#include
#include
#include

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);

while(1)
{
XNextEvent(display,&event);
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;
}
}
XCloseDisplay(display);
return 0;
}
 
Old 11-27-2009, 06:16 AM   #2
mknirsch
Member
 
Registered: Feb 2003
Location: Germany
Distribution: SLES 10, SuSe 11.x
Posts: 31

Rep: Reputation: 16
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.

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

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
Martin
 
  


Reply

Tags
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 01:59 PM
Keyboard events interrupting mouse events. miner49er Linux - Hardware 3 11-04-2008 04:16 AM
Mouse interrupting keyboard events, can't type while mouse is moving sandaili Linux - Hardware 0 08-04-2008 06:56 PM
Xlib custom events phanna Programming 1 09-12-2005 04:50 PM
Selecting Xlib events twice???? pihu Programming 1 07-15-2005 02:00 PM


All times are GMT -5. The time now is 05:43 PM.

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