Question

I'm having some understanding problem of event-handling with Xlib functions.

My question would be - how do i press a key during an animation, without disturbing the animation.

My setup so far, is that i have some animation in a while loop and want to achieve a KeyPress event which modifies a parameter.

It looks something like this

while(1){

XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//
}}
//...Some animation where parameter a is used to modify animation...//
}

Now, the problem is that i have to press the key consistently to get the animation on my screen, otherwise nothing appears. I've tried some multiple code-modifications, with KeyRelease etc. but i don't have clou, really.

Trivially said - i need to hook a key during animation without the XNextEvent process, waiting for any event. But without the XNextEvent statement in my code, conditional statements for KeyPress event checking aren't working.

I guess formally this would mean:

while(1){

if(report.type==KeyPress) {
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//
}}
//...Some animation where parameter a is used to modify animation...//
}

Was it helpful?

Solution

Use XPending() to check for XEvents before getting them with XNextEvent().

XPending() returns then number of events in the event queue so modify your loop:

   while(1){
     if (XPending(dis) > 0) {
       XNextEvent(dis, &report);
       switch (report.type) {
         case KeyPress:
           if (XLookupKeysym(&report.xkey, 0) == XK_space){
              //...modify parameter a..//
           }
        }
     }
     //...Some animation where parameter a is used to modify animation...//
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top