【求助】Mac上实现注册热键功能的疑惑

背景

做一个小demo,需要实现热键功能。

通过搜索和问AI折腾了一番,基本实现了功能,通过Combine提供的RegisterEventHotKey函数实现。

不过这个操作需要把程序添加到系统【隐私与安全】里的【辅助功能】列表里才能生效。

如下图

问题

Max上的微信和企业微信,都提供了热键功能,用来启动屏幕截图。发现它们都没有在【辅助功能】列表里,如上图所示。

所以想问问各位大神,有什么方式实现热键,不用开启辅助功能权限。

实现代码

swift 复制代码
import SwiftUI
import Carbon
import Combine

class HotkeyViewModel: ObservableObject {
    private var hotKeyRef: EventHotKeyRef?
    @Published var isPermissionGranted: Bool = false
    
    init() {
        requestAccessibilityPermission()
    }
    
    private func requestAccessibilityPermission() {
        var trusted = false
        let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
        if #available(OSX 10.9, *) {
            trusted = AXIsProcessTrustedWithOptions(options)
        } else {
            trusted = AXIsProcessTrusted()
        }
        // 没有把程序添加到【辅助功能】列表里,trusted会为false, 注册热键会不成功
        if !trusted {
            print("Please enable accessibility permissions")
            return
        }
        isPermissionGranted = true
    }
    
    func registerHotkey() {
        var eventType = EventTypeSpec()
        eventType.eventClass = OSType(kEventClassKeyboard)
        eventType.eventKind = UInt32(kEventHotKeyPressed)
        
        let eventHandler: EventHandlerUPP = { (nextHandler, theEvent, userData) -> OSStatus in
            print("Hotkey pressed!")
            return noErr
        }
        
        var hotKeyID = EventHotKeyID()
        hotKeyID.signature = OSType(1)
        hotKeyID.id = UInt32(kVK_ANSI_A)
        
        let modifiers = UInt32(cmdKey | controlKey)
        RegisterEventHotKey(UInt32(kVK_ANSI_A), modifiers, hotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef)
        
        InstallEventHandler(GetEventDispatcherTarget(), eventHandler, 1, &eventType, nil, nil)
    }
}

struct ContentView: View {
    @StateObject var viewModel = HotkeyViewModel()
        
        var body: some View {
            VStack {
                Text("Accessibility Permission: \(viewModel.isPermissionGranted ? "Granted" : "Not Granted")")
                    .padding()
                Button("Register Hotkey") {
                    viewModel.registerHotkey()
                }
            }
        }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
相关推荐
奶糖的次元空间2 天前
iOS 学习笔记 - SwiftUI 和 简单布局
ios·swift
2501_915918413 天前
有没有Xcode 替代方案?在快蝎 IDE 中完成 iOS 开发的过程
ide·vscode·ios·个人开发·xcode·swift·敏捷流程
songgeb4 天前
Compositional layout in iOS
ios·swift·设计
1024小神4 天前
记录xcode项目swiftui配置APP加载启动图
前端·ios·swiftui·swift
wjm0410066 天前
ios学习路线-- swift基础2
学习·ios·swift
游戏开发爱好者86 天前
如何使用Instruments和Keymob进行Swift应用性能优化分析
开发语言·ios·性能优化·小程序·uni-app·iphone·swift
游戏开发爱好者87 天前
新的 iOS 开发工具体验,在快蝎 IDE 里完成应用开发与真机调试
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
东坡肘子7 天前
50 岁的苹果和 51 岁的我 -- 肘子的 Swift 周报 #127
人工智能·swiftui·swift
denggun123458 天前
Sendable 协议-Swift 结构化并发的核心安全保障
ios·swift
denggun123459 天前
结构化并发(Structured Concurrency)
开发语言·ios·swift