该解决方案使用 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
上面的类会有用。
I think Carbon
框架文档 http://developer.apple.com/legacy/mac/library/documentation/Carbon/reference/Carbon_Event_Manager_Ref/Carbon_Event_Manager_Ref.pdf将为解决问题提供更多见解和灵活性。除了Carbon
,在论坛中人们提到了使用Cocoa
,但我不知道。
编辑:感谢技术利润 https://stackoverflow.com/users/17175/technomage, 更多信息here https://stackoverflow.com/questions/15759993/thread-getting-blocked-in-receivenextevent/15782744?noredirect=1#15782744