2013-03-05 88 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