Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JNA联合结构映射_Java_Mapping_Structure_Union_Jna - Fatal编程技术网

Java JNA联合结构映射

Java JNA联合结构映射,java,mapping,structure,union,jna,Java,Mapping,Structure,Union,Jna,在JNA中,如何从Xlib映射如下XEvent这样的联合结构 typedef union _XEvent { int type; /* must not be changed */ XAnyEvent xany; XKeyEvent xkey; XButtonEvent xbutton; XMotionEvent xmotion; XCrossingEvent xcrossing; XFocusChangeEvent xfocus;

在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、XButtoneEvent、XMotionEvent…等等)

我并不是在要求上面所有结构的完整映射。用一个小例子清楚地解释一下如何做就足够了


谢谢

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,但我相信我的想法是检查类型值,然后只参考相应的事件字段。其他值应为空。我认为通过强制转换不可能做到这一点。

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

  • 使用您喜欢的任何方法(例如XNextEvent)获取XEvent
  • 使用类型字段确定事件的类型
  • 根据类型,使用字段名(字符串形式)调用readfield方法,并将返回值强制转换为字段名的事件类型
  • 例如:

    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
    }
    

    你试过了吗?我在contrib和XGrabKeyboard以及XNextEvent中尝试了代码。它返回XEvent,类型为KeyPress/KeyRelease,但是,当我访问event.xkey.keycode时,无论按下哪个键,我总是得到零。如果您尝试成功,那么我会将您的答案标记为正确答案,因为问题可能在我的代码中的其他地方。耶!回答得很好,在SDL2映射方面对我帮助很大,谢谢。