2013-03-05 84 views
25

我在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:JNAapi对于UNIX是广泛的,我无法弄清楚要寻找什么。 reference可能有帮助

+0

你可能会想看看GCEventRef,https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference .html – 2013-03-08 05:53:10

+0

JNA的unix平台映射大部分都是针对X11的,并没有太多专门针对OS X的。[Rococoa](http://code.google.com/p/rococoa/)有更多的方法OS X映射。 – technomage 2013-03-08 16:35:16

+0

@technomage我现在不能转向Rococoa,因为那样我将不得不重写代码。 Windows已经正常工作。我只是需要一些技术来获得消息 – Jatin 2013-03-09 08:32:45

回答

3

此解决方案使用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

+0

很酷。太糟糕碳虽然被弃用。 – 2013-04-03 10:59:58

+0

@AmigableClarkKant任何其他选择? – Jatin 2013-04-03 11:21:12

+0

对不起,不是我所知道的。 (但可能会有。) – 2013-04-03 11:22:12