我想在用户将使用鼠标滚轮时执行代码。我编写了简单的示例应用程序,但无法正常工作。为什么我的应用程序对鼠标滚轮没有反应?
#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问题:为什么在调用之后,除非换行符位于格式字符串中,否则printf不会刷新?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句