c - 如何对 Xlib 中的鼠标滚轮使用react?

标签 c mousewheel xlib

我想在用户使用鼠标滚轮时执行代码。我编写了简单的示例应用程序,但它不工作。为什么我的应用程序对鼠标滚轮没有反应?

#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
   Display *d;
   Window w;
   XEvent e;
   char *msg = "Hello, World!";
   int s;

   d = XOpenDisplay(NULL);
   if (d == NULL) {
      fprintf(stderr, "Cannot open display\n");
      exit(1);
   }

   s = DefaultScreen(d);
   w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                           BlackPixel(d, s), WhitePixel(d, s));
    XSelectInput(d, w, ExposureMask | ButtonPressMask);
   XMapWindow(d, w);

   while (1) {
      XNextEvent(d, &e);
      if (e.type == Expose) {
         XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
         XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
      }
      if (e.type == ButtonPress){
        switch (e.xbutton.button){
            case Button4:
                printf("Scrolled up");
                break;
            case Button5:
                printf("Scrolled down");
                break;
            default:
                printf("cos");
        }
      }
   }

   XCloseDisplay(d);
   return 0;
}

最佳答案

您的代码是正确的,您没有立即看到结果的原因是因为 fprintf缓冲数据直到换行。将调试行替换为 printf("Scrolled up\n");你会立即看到结果。或者你可以做setbuf(stdout, NULL);禁用缓冲。

请参阅这个相关的 SO 问题:Why does printf not flush after the call unless a newline is in the format string?

关于c - 如何对 Xlib 中的鼠标滚轮使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296311/

相关文章:

C:是否可以有一个以函数指针数组作为其参数之一的递归函数?

c - PKCS5_PBKDF2_HMAC : binary password

python - 在 Python 中通过 Xlib 找出鼠标按钮状态

c++ - 如何使用 xlib 创建游戏循环

c++ - 鼠标按键模拟——右键和上下文菜单问题

c - 该程序打印不需要的东西

c - 这个程序有内存泄漏吗?

javascript - 如何在 jQuery 中禁用(完全)和启用任何鼠标监听器?

javascript - 如何让鼠标滚轮事件在 jQuery 中只触发一次?