x11 events

这个代码实例代码也是相对来说比较长的。这个代码里面讲了鼠标和键盘事件的处理。

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

猜你喜欢

转载自blog.csdn.net/u012939880/article/details/108191129