Objective c 将CGEvents击键发送到后台应用程序
我正在尝试将enter键发送到后台应用程序,如下所示:Objective c 将CGEvents击键发送到后台应用程序,objective-c,cocoa,macos,nsevent,cgeventtap,Objective C,Cocoa,Macos,Nsevent,Cgeventtap,我正在尝试将enter键发送到后台应用程序,如下所示: CGEventRef a = CGEventCreateKeyboardEvent(eventSource, 36, true); CGEventRef b = CGEventCreateKeyboardEvent(eventSource, 36, false); CGEventPostToPSN(&psn, a); CGEventPostToPSN(&psn, b); 它不工作,我想这是因为应用程序必须是最前面的应用程序
CGEventRef a = CGEventCreateKeyboardEvent(eventSource, 36, true);
CGEventRef b = CGEventCreateKeyboardEvent(eventSource, 36, false);
CGEventPostToPSN(&psn, a);
CGEventPostToPSN(&psn, b);
它不工作,我想这是因为应用程序必须是最前面的应用程序才能接收击键?我说得对吗?如果是,我是否可以在不首先激活应用程序的情况下发送此事件?如果不是,那我做错了什么?
谢谢。后台应用程序不会对关键事件起作用。你有两个选项可以让你的应用程序在后台处理它们:和。
NSEvent
选项非常简单:
// A block callback to handle the events
NSEvent * (^monitorHandler)(NSEvent *);
monitorHandler = ^NSEvent * (NSEvent * theEvent){
NSLog(@"Got a keyDown: %d", [theEvent keyCode]);
// The block can return the same event, a different
// event, or nil, depending on how you want it to be
// handled later. In this case, being in the background,
// there won't be any handling regardless.
return theEvent;
};
// Creates an object that we don't own but must keep track of to
// remove later (see docs). Here, it is placed in an ivar.
monitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
handler:monitorHandler];
但你已经在玩弄事件点击,所以你可能只想走这条路
// Creates an object that must be CFRelease'd when we're done
CFMachPortRef tap = CGEventTapCreateForPSN(myOwnPSN,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
kCGEventKeyDown,
myEventTapCallback,
NULL);
回调很简单。有关信息,请参阅事件服务参考中的