【问题标题】:Global Shortcut Key Not Working MacOS - HotKey全局快捷键不起作用 MacOS - HotKey
【发布时间】:2021-01-07 05:05:35
【问题描述】:

我正在尝试使用 HotKey 实现快捷键“Command + Option + J”,但由于某种原因,它无法在视图控制器之外工作。该应用程序是作为下拉菜单栏实现的,因此没有实际的窗口可以放在前面。我期待我的信息被打印出来。我有两个按钮,注册和取消注册,当我注册并打印出我看到它已注册的组合键时,我相信它正在工作。不幸的是,当我在另一个窗口打开或在桌面视图中按下组合键时,不会打印任何评论。非常感谢任何帮助。

//
//  ViewController.swift
//

import Cocoa
import AppKit
import HotKey
import Carbon

class ViewController: NSViewController {

    @IBOutlet var pressedLabel: NSTextField!
    
    private var hotKey: HotKey? {
        didSet {
            guard let hotKey = hotKey else {
                pressedLabel.stringValue = "Unregistered"
                return
            }

            pressedLabel.stringValue = "Registered"
            hotKey.keyDownHandler = { [weak self] in
            NSApplication.shared.orderedWindows.forEach({ (window) in
               print("woo")
            })
            }
    }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        register(self)
        // Do any additional setup after loading the view.
    }
    
    @IBAction func unregister(_ sender: Any?) {
        hotKey = nil
        print("the hot key is ", hotKey?.keyCombo)

        
    }
    

    @IBAction func register(_ sender: Any) {
        hotKey = HotKey(keyCombo: KeyCombo(key: .j, modifiers: [.command, .shift])
        )
    }
    
}

【问题讨论】:

标签: swift macos cocoa hotkeys


【解决方案1】:

这可能是一个参考问题

private var hotKey: HotKey?

HotKey 配置旨在保持活力。它处理全局按键事件,然后调用回调。如果将它放入视图控制器,然后从屏幕上移除视图控制器的窗口或以其他方式释放它,它的私有 HotKey 引用也将消失,因此事件处理程序将被有效地销毁。

如果是这种情况,您可以将hotKey 属性移动到AppDelegate。然后属性引用将长期存在(确切地说,直到您退出应用程序)。

如果这有帮助,我强烈建议您将逻辑封装在一个小助手中。为简单起见,可以存储为单例。

class HotKeyController {
    static var instance = HotKeyController()
    private init() { }

    var hotKey: HotKey? {
        didSet {
            hotKey.keyDownHandler = { [weak self] in
                NSApplication.shared.orderedWindows.forEach({ (window) in
                   print("woo")
                })
            }
        }
    }
}

然后在视图控制器中使用它:

class ViewController: NSViewController {

    @IBOutlet var pressedLabel: NSTextField!
    
    private var hotKey: HotKey? {
        get { 
            return HotKeyController.instance.hotKey
        }
        set {
            HotKeyController.instance.hotKey = newValue

            guard let hotKey = newValue else {
                pressedLabel.stringValue = "Unregistered"
                return
            }

            pressedLabel.stringValue = "Registered"
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        register(self)
        // Do any additional setup after loading the view.
    }
    
    @IBAction func unregister(_ sender: Any?) {
        hotKey = nil
        print("the hot key is ", hotKey?.keyCombo)
    }
    

    @IBAction func register(_ sender: Any) {
        hotKey = HotKey(keyCombo: KeyCombo(key: .j, modifiers: [.command, .shift]))
        print("the hot key is ", hotKey?.keyCombo)
    }
}

从长远来看,您将希望在HotKeyController 的私有初始化程序中实现基于UserDefaults 的热键存储和恢复,以使设置“固定”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-09
    • 2020-02-06
    • 2015-04-28
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多