Advertisement

Event handling in X11

Started by June 30, 2007 01:30 AM
9 comments, last by z_k 17 years, 4 months ago
Hi I wrote xlib program ,and use below code for event handling //****************** while (1) { if(XPending(dis)) { XNextEvent(dis, &ev); switch(ev.type) { case KeyPress: { cout<<"key press"<<endl; exit(0); break; } } } } //****************** when set window attribute //***** att.override_redirect=True; XChangeWindowAttributes( dis, win, CWOverrideRedirect, &att ); //***** event handling don't work ,but when comment this code ,event handling is correct so when press any key program exit please guide me thank you
Why are you using xlib? xlib isn't really intended for application programmers, there are plenty of higher level, less ugly, and even cross platform libraries that can do whatever it is you need. Need input? SDL is your lib. GUI? gtk/gtkmm (AND LIBGLADE FOR THE LOVE OF GOD!) is for you.

etc etc etc.

You'd need a pretty strong reason to use xlib.

(sorry i know that wasn't very constructive, but it needed to be said)

cheers
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Advertisement
I must using xlib becuase it is low level
What are you trying to do with it? I'm there are very few cases in which you should be using xlib.
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

In your idea why this problem occur ....
i couldn't event handling when i use below attribute for my window
//***************
att.override_redirect=True;
XChangeWindowAttributes( dis, win, CWOverrideRedirect, &att );
//***************
You don't need the override thingie for input, but events:

att.event_mask = KeyPressMask;XChangeWindowAttributes( dis, win, CWEventMask, &att );


Check out the source code for the Quake games, they got a lot of useful stuff on X.
Advertisement

when i using below code
att.event_mask = KeyPressMask;
XChangeWindowAttributes( dis, win, CWEventMask, &att );

my program limit to keyboard event only ,......
i need both event handling (keyboard and mouse in program)

any way....
when i set below attribute for window
att.override_redirect=True;
XChangeWindowAttributes( dis, win, CWOverrideRedirect, &att );
(i use this attribute for full screen window)

event handling don't work ....
It's all handled with bitmasks:

/* Enable redirection override. */att.override_redirect = True;/* Mask all wanted event bits together. */att.event_mask = KeyPressMask | KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | etc...;/* Tell window to take care of both override redirection and input events. */XChangeWindowAttributes( dis, win, CWOverrideRedirect | CWEventMask, &att );
thanks coelurus
but when set below attibute for window
att.override_redirect=True;
and
att.event_mask =KeyReleaseMask | PointerMotionMask | ButtonPressMask| KeyPressMask ;
and
XChangeWindowAttributes( dis, win, CWOverrideRedirect | CWEventMask, &att );

//*****************event handling****************
while (1)
{
if(XPending(dis))
{
XNextEvent(dis, &(ev));
switch(ev.type)
{
case KeyPress:
exit(0);
break;
case ButtonPress:
exit(0);
break;
}
}
}
//***************************

only when i press mouse button ,program exit ,but pressing any key don't effect program :(
i think when window override ,can't take keyboard event ....... ,any idea?
Don't remember what happens to keyboard input when you do the override thingy, but what you say sounds reasonable. Grabbing the keyboard will fix it:

XGrabKeyboard(dis, win, true, GrabModeAsync, GrabModeAsync, CurrentTime);


Make sure to ungrab and regrab whenever you should.

I would strongly suggest you take a look at some existing source code, X has a rather steep learning curve. The simplest sources are games, but they still cover the most important parts.

This topic is closed to new replies.

Advertisement