从OS获取事件

我在Windows上工作,但我被困在Mac上。 我有Canon SDK,并在它上面构建了一个JNA包装器。 它适用于Windows,需要一些Mac帮助。 在sdk中,有一个函数可以注册回调函数。 基本上当相机发生事件时,它会调用回调函数。

在Windows上,注册后,我需要使用User32来获取事件并通过以下方式调度事件:

 private static final User32 lib = User32.INSTANCE; boolean hasMessage = lib.PeekMessage( msg, null, 0, 0, 1 ); // peek and remove if( hasMessage ){ lib.TranslateMessage( msg ); lib.DispatchMessage( msg ); //message gets dispatched and hence the callback function is called } 

在api中,我在Mac中找不到类似的类。 我怎么去这个?

PS:unix的JNA api非常广泛,我无法弄清楚要寻找什么。 参考可能会有所帮助

该解决方案使用Cocoa框架。 Cocoa已被弃用,我不知道任何其他替代解决方案。 但下面的作品就像魅力一样。

最后我找到了使用Carbon框架的解决方案。 这是我的MCarbon接口,它定义了我需要的调用。

  public interface MCarbon extends Library { MCarbon INSTANCE = (MCarbon) Native.loadLibrary("Carbon", MCarbon.class); Pointer GetCurrentEventQueue(); int SendEventToEventTarget(Pointer inEvent, Pointer intarget); int RemoveEventFromQueue(Pointer inQueue, Pointer inEvent); void ReleaseEvent(Pointer inEvent); Pointer AcquireFirstMatchingEventInQueue(Pointer inQueue,NativeLong inNumTypes,EventTypeSpec[] inList, NativeLong inOptions); //... so on } 

使用以下函数解决问题的解决方案:

  NativeLong ReceiveNextEvent(NativeLong inNumTypes, EventTypeSpec[] inList, double inTimeout, byte inPullEvent, Pointer outEvent); 

这样做了。 根据文件 –

 This routine tries to fetch the next event of a specified type. If no events in the event queue match, this routine will run the current event loop until an event that matches arrives, or the timeout expires. Except for timers firing, your application is blocked waiting for events to arrive when inside this function. 

如果不是ReceiveNextEvent ,那么上面的MCarbon类中提到的其他函数将是有用的。

我认为Carbon框架文档可以提供更多的见解和灵活性来解决问题。 除了Carbon之外,在论坛中人们已经提到了使用Cocoa解决问题,但我没有注意到。

编辑:感谢technomarge , 这里有更多信息