...
代码块 | ||
---|---|---|
| ||
#include <X11/Xlib.h> #include <stdio.h> #include <stdlib.h> int main() { Display *display; Window window; XEvent event; int screen; // 打开与X服务器的连接 display = XOpenDisplay(NULL); if (display == NULL) { fprintf(stderr, "Cannot open display\n"); exit(1); } screen = DefaultScreen(display); // 创建一个窗口 window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 640, 480, 1, BlackPixel(display, screen), WhitePixel(display, screen)); // 选择感兴趣的事件类型 XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask); // 显示窗口 XMapWindow(display, window); // 事件循环 while (1) { XNextEvent(display, &event); // 处理暴露事件(窗口需要重新绘制) if (event.type == Expose) { XFillRectangle(display, window, DefaultGC(display, screen), 20, 20, 10, 10); } // 处理键盘按键按下事件 else if (event.type == KeyPress) { printf("Key pressed: %x\n", event.xkey.keycode); } // 处理键盘按键抬起事件 else if (event.type == KeyRelease) { printf("Key released: %x\n", event.xkey.keycode); } // 处理鼠标按键按下事件 else if (event.type == ButtonPress) { printf("Mouse button pressed: %x at (%d, %d)\n", event.xbutton.button, event.xbutton.x, event.xbutton.y); } // 处理鼠标按键抬起事件 else if (event.type == ButtonRelease) { printf("Mouse button released: %x at (%d, %d)\n", event.xbutton.button, event.xbutton.x, event.xbutton.y); } } // 关闭与X服务器的连接 XCloseDisplay(display); return 0; } |
...