2017-06-18 48 views
0

我有问题,我的睡眠功能禁用排队C.执行用C

,当我在此code睡眠功能使用这样的:

while(1) { 
     XNextEvent(display, &xevent); 
     switch (xevent.type) { 
      case MotionNotify: 
       break; 
      case ButtonPress: 
       printf("Button click: [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root); 
       sleep(5); 
       break; 
      case ButtonRelease: 
       break; 
     } 

它不为我工作得很好,因为printf的(“按钮点击”)正在执行,但速度较慢。

如何打印“按钮点击x y”一次并停止点击5秒钟?

+1

我不清楚你想要什么 - 在事件循环中“睡觉”不是X所做的。 – tofro

+0

当我点击屏幕上的任何地方时,我收到消息“按钮点击x,y” 当我点击快速5次时,我得到5条消息,但25秒后。即使我几次,我也只想得到一条消息。 – Adrian

回答

2

我认为你正在寻找的东西,如:

/* ignore_click is the time until mouse click is ignored */ 
time_t ignore_click = 0; 

while(1) { 
    XNextEvent(display, &xevent); 
    switch (xevent.type) { 
     case MotionNotify: 
      break; 
     case ButtonPress: 
      { 
       time_t now; 
       /* we read current time */ 
       time(&now); 

       if (now > ignore_click) 
       { 
        /* now is after ignore_click, mous click is processed */ 
        printf("Button click: [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root); 

        /* and we set ignore_click to ignore clicks for 5 seconds */ 
        ignore_click = now + 5; 
       } 
       else 
       { 
        /* click is ignored */ 
       } 
      } 
      break; 
     case ButtonRelease: 
      break; 
    } 
} 

上面写会忽略的点击次数4〜5秒钟的代码:time_t类型是第二精密结构...

要获得更多的精确时间,可以使用struct timevalstruct timespec结构。我没有在我的例子中使用它们来保持清晰。

+0

非常感谢,效果很好! – Adrian