gpt4 book ai didi

macos - 是否可以检测 Magic Mouse/Trackpad 是否被触摸?

转载 作者:行者123 更新时间:2023-12-03 16:20:35 26 4
gpt4 key购买 nike

只是想知道如果用户触摸 Magic Mouse 或 Trackpad,是否有办法在 Cocoa 中接收回调?

我研究了 Quartz Events,但似乎我只能在鼠标移动或单击等情况下获得回调。

请注意,即使我的应用程序未处于事件状态,我也希望收到回调。这是一个后台实用程序应用程序。此外,它不能使用私有(private)框架,因为它将成为 Mac App Store 应用程序。

最佳答案

您可以使用此代码来捕获事件:(创建一个新的 Cocoa 应用程序并将其放入应用程序委托(delegate)中)

NSEventMask eventMask = NSEventMaskGesture | NSEventMaskMagnify | NSEventMaskSwipe | NSEventMaskRotate | NSEventMaskBeginGesture | NSEventMaskEndGesture;

CGEventRef eventTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef eventRef, void *userInfo) {
NSEvent *event = [NSEvent eventWithCGEvent:eventRef];

// only act for events which do match the mask
if (eventMask & NSEventMaskFromType([event type])) {
NSLog(@"eventTapCallback: [event type] = %ld", [event type]);
}

return [event CGEvent];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, kCGEventMaskForAllEvents, eventTapCallback, nil);
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0), kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
}

但是..沙箱可能会阻止您使用CGEventTapCreate,因为本质上,它允许应用程序监听整个事件系统,这不是很安全。如果您可以接受不使用沙箱,则当在触摸板上进行新触摸时,将调用 eventTapCallback

关于macos - 是否可以检测 Magic Mouse/Trackpad 是否被触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9833522/

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