【问题标题】:Get events from OS从操作系统获取事件
【发布时间】:2013-02-19 10:29:42
【问题描述】:

我在 Windows 上工作,但我在 Mac 上被困在这里。我有佳能 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 非常广泛,我不知道要查找什么。 reference 可能会有所帮助

【问题讨论】:

  • JNA 的大多数 unix 平台映射都是针对 X11 的,而专门针对 OS X 的映射并不多。Rococoa 通过 OS X 映射提供了更多。
  • @technomage 我现在不能转移到 Rococoa,因为那时我将不得不重写代码。 Windows 1 已经可以正常工作了。我只需要一些技巧来获取信息
  • 你应该多读一点关于 rococoa 是做什么的。它是 JNA 之上的一个库,用于方便访问使用 ObjectiveC 的 OS X 库。该项目还有更多的社区专门参与使用来自 Java 的 OS X 库。但最终,您需要一些 OSX 专业知识来确定合适的 OS X 事件 API(除非您的 Canon SDK 提供示例)。
  • 如果您要提供佳能 SDK 附带的 OSX 的本机代码示例,您将更有可能得到完整的响应。

标签: java macos java-native-interface jna


【解决方案1】:

此解决方案使用 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 框架documentation 将提供更多洞察力和灵活性来解决问题。除了Carbon,论坛里有人提到过使用Cocoa解决问题,但我不知道。

编辑:感谢technomarge,更多信息here

【讨论】:

  • 酷。可惜 Carbon 已被弃用。
  • @AmigableClarkKant 还有其他选择吗?
  • 对不起,我不知道。 (但可能有。)
猜你喜欢
  • 2013-11-11
  • 2010-11-02
  • 2014-08-28
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 2019-08-01
相关资源
最近更新 更多