在 C 中禁用排队执行
Disable queueing execute in 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("button click") 一直在执行,但速度较慢。
如何打印 "button click x y" 一次并停止监听点击 5 秒?
我认为您正在寻找类似的东西:
/* 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 timeval
或struct timespec
结构。为了清楚起见,我在示例中没有使用它们。
我在 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("button click") 一直在执行,但速度较慢。
如何打印 "button click x y" 一次并停止监听点击 5 秒?
我认为您正在寻找类似的东西:
/* 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 timeval
或struct timespec
结构。为了清楚起见,我在示例中没有使用它们。