这个代码实例代码也是相对来说比较长的。这个代码里面讲了鼠标和键盘事件的处理。
- 注册事件
XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | Button1MotionMask | Button2MotionMask | StructureNotifyMask);
- 事件监听
XNextEvent(display, &an_event);
- 窗口大小 暴露 变化事件
handle_expose 中对窗口进行了重绘。case Expose: /* redraw our window. */ handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose, width, height, pixels); break;
- 窗口变化事件
case ConfigureNotify: /* update the size of our window, for expose events. */ width = an_event.xconfigure.width; height = an_event.xconfigure.height; break;
- 鼠标点击松开事件
handle_button_down 使用 XDrawPoint 进行画点,如果是黑的就画成白的,如果是白的就画成黑的case ButtonPress: /* invert the pixel under the mouse pointer. */ handle_button_down(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break;
- 鼠标移动事件
handle_drag 也是画点,但是有个状态判断,不满足 Button1Mask Button2Mask 状态,所以没有在屏幕上画。case MotionNotify: /* invert the pixel under the mouse pointer. */ handle_drag(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break;
- 键盘释放事件
键盘释放,窗口退出case KeyPress: /* exit the application by braking out of the events loop. */ done = 1; break;