2010-06-23 114 views
2

在JNA,你怎么映射联合结构像Xlib的JNA联合结构映射

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; 

以下XEvent我希望能够以后投的XEvent在JNA其他事件(如XKeyEvent, XButtonEvent,XMotionEvent ...等),根据收到的事件的类型。

我不是要求所有上述结构的完整映射。用一个小例子来说明如何做到这一点就足够了。

谢谢

回答

1

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

这在这里描述。 here

该实现可以在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自己,但我认为这个想法是检查类型值,然后仅指相应的事件现场。其他人应该是空的。我不认为有可能通过演员阵容来完成。

+0

你试过了吗?我使用XNextEvent在XGrabKeyboard的contrib中尝试了代码。它返回XEvent,类型是KeyPress/KeyRelease,但是,当我访问event.xkey.keycode时,无论按下哪个键,我总是得到零。如果您尝试成功,那么我会将您的答案标记为正确答案,因为问题可能在我的代码中。 – Untitled 2010-06-23 21:39:26

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 
} 
+0

耶!很好的回答,我的SDL2映射帮助了我很多,谢谢。 – 2015-08-31 12:35:26