2017-06-18 56 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