gpt4 book ai didi

objective-c - 将 CGEvents 击键发送到后台应用程序

转载 作者:搜寻专家 更新时间:2023-10-30 20:11:59 25 4
gpt4 key购买 nike

我正在尝试将回车键发送到这样的后台应用程序:

CGEventRef a = CGEventCreateKeyboardEvent(eventSource, 36, true);
CGEventRef b = CGEventCreateKeyboardEvent(eventSource, 36, false);
CGEventPostToPSN(&psn, a);
CGEventPostToPSN(&psn, b);

它不起作用,我认为这是因为该应用程序必须是接收击键的最前面的应用程序?我对吗?如果是这样,我是否可以在不先激活应用程序的情况下发送此事件?如果没有,那我做错了什么?谢谢。

最佳答案

处于后台的应用程序不会对关键事件采取行动。您有两个选项可以让您的应用程序在后台处理它们:Event Taps+[NSEvent addLocalMonitorForEventsMatchingMask:] . 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);

回调很简单。参见 Callbacks在事件服务引用中获取信息。

关于objective-c - 将 CGEvents 击键发送到后台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com