3

在 JNA 中,如何从 Xlib 映射如下 XEvent 的联合结构

typedef union _XEvent {
    int type;    /* must not be changed */
    XAnyEvent xany;
    XKeyEvent xkey;
    XButtonEvent xbutton;
    XMotionEvent xmotion;
    XCrossingEvent xcrossing;
    XFocusChangeEvent xfocus;
    XExposeEvent xexpose;
    XGraphicsExposeEvent xgraphicsexpose;
    XNoExposeEvent xnoexpose;
    XVisibilityEvent xvisibility;
    XCreateWindowEvent xcreatewindow;
    XDestroyWindowEvent xdestroywindow;
    XUnmapEvent xunmap;
    XMapEvent xmap;
    XMapRequestEvent xmaprequest;
    XReparentEvent xreparent;
    XConfigureEvent xconfigure;
    XGravityEvent xgravity;
    XResizeRequestEvent xresizerequest;
    XConfigureRequestEvent xconfigurerequest;
    XCirculateEvent xcirculate;
    XCirculateRequestEvent xcirculaterequest;
    XPropertyEvent xproperty;
    XSelectionClearEvent xselectionclear;
    XSelectionRequestEvent xselectionrequest;
    XSelectionEvent xselection;
    XColormapEvent xcolormap;
    XClientMessageEvent xclient;
    XMappingEvent xmapping;
    XErrorEvent xerror;
    XKeymapEvent xkeymap;
    long pad[24];
} XEvent;

我希望稍后能够根据接收到的事件类型将 JNA 中的 XEvent 转换为其他事件(如 XKeyEvent、XButtonEvent、XMotionEvent ...等)。

我并不是要求对上述所有结构进行完整映射。一个关于如何做的小例子的清晰解释就足够了。

谢谢

4

2 回答 2

2

使用 JNA contrib (com.sun.jna.platform.X11) 中定义的映射,然后执行以下操作:

  1. 使用您喜欢的任何方法(例如 XNextEvent)获取 XEvent。
  2. 使用类型字段确定事件的类型。
  3. 根据类型,使用字段名(字符串)调用方法readFiled,并将返回值转换为字段名的事件类型。

例子:

XEvent event = new XEvent();
X11.INSTANCE.XNextEvent(display, event);
if(event.type == X11.KeyPress) {
    XKeyEvent xKey = (XKeyEvent)event.readField("xkey");
    // you can now use xKey.keycode and other fields
}
于 2010-06-25T00:35:13.077 回答
1

JNA 的源代码已经提供了 xlib 的示例。

这在此处进行了描述。 这里

该实现可以在 contrib 文件夹下的 jna 源代码中找到。

特别是对于 XEvent,它被定义为:

    public static class XEvent extends Union {
    public int type;
    public XAnyEvent xany;
    public XKeyEvent xkey;
    public XButtonEvent xbutton;
    public XMotionEvent xmotion;
    public XCrossingEvent xcrossing;
    public XFocusChangeEvent xfocus;
    public XExposeEvent xexpose;
    public XGraphicsExposeEvent xgraphicsexpose;
    public XNoExposeEvent xnoexpose;
    public XVisibilityEvent xvisibility;
    public XCreateWindowEvent xcreatewindow;
    public XDestroyWindowEvent xdestroywindow;
    public XUnmapEvent xunmap;
    public XMapEvent xmap;
    public XMapRequestEvent xmaprequest;
    public XReparentEvent xreparent;
    public XConfigureEvent xconfigure;
    public XGravityEvent xgravity;
    public XResizeRequestEvent xresizerequest;
    public XConfigureRequestEvent xconfigurerequest;
    public XCirculateEvent xcirculate;
    public XCirculateRequestEvent xcirculaterequest;
    public XPropertyEvent xproperty;
    public XSelectionClearEvent xselectionclear;
    public XSelectionRequestEvent xselectionrequest;
    public XSelectionEvent xselection;
    public XColormapEvent xcolormap;
    public XClientMessageEvent xclient;
    public XMappingEvent xmapping;
    public XErrorEvent xerror;
    public XKeymapEvent xkeymap;
    public NativeLong[] pad = new NativeLong[24];
}

我自己还在学习 JNA,但我相信这个想法是检查类型值,然后只引用相应的事件字段。其他的应该为空。我认为通过演员阵容是不可能做到的。

于 2010-06-23T20:09:27.997 回答