【问题标题】:Global modifier key press detection in SwiftSwift 中的全局修饰键按下检测
【发布时间】:2017-01-30 01:22:14
【问题描述】:

我正在尝试使用 Carbon 的函数 RegisterEventHotKey 创建一个热键,用于按下命令键。我是这样使用它的:

InstallEventHandler(GetApplicationEventTarget(), handler, 1, &eventType, nil, nil)
RegisterEventHotKey(UInt32(cmdKey), 0, hotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef)

但是,当我只使用命令键时,它不会调用handler。如果我将 cmdKey 替换为任何其他非修饰键代码,则会调用处理程序。

有没有人有任何建议可以让应用程序在按下命令键时全局识别?谢谢。

【问题讨论】:

    标签: swift macos macos-carbon


    【解决方案1】:

    您可以将与 .flagsChanged 匹配的事件的全局监视器添加到您的视图控制器,以便您可以检查其 modifierFlags 与 deviceIndependentFlagsMask 的交集并检查结果键。

    声明

    class func addGlobalMonitorForEvents(matching mask: NSEventMask, handler block: @escaping (NSEvent) -> Void) -> Any?
    

    安装一个事件监视器来接收发布到的事件的副本 其他应用。事件异步传递到您的应用程序 你只能观察事件;您不能修改或以其他方式 阻止事件被传递到它的原始目标 应用。只有在可访问性的情况下才能监控与键相关的事件 已启用,或者您的应用程序是否受信任可访问性访问 (see AXIsProcessTrusted())。请注意,您的处理程序不会被调用 用于发送到您自己的应用程序的事件。

    import Cocoa
    class ViewController: NSViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) {
                switch $0.modifierFlags.intersection(.deviceIndependentFlagsMask) {
                case [.shift]:
                    print("shift key is pressed")
                case [.control]:
                    print("control key is pressed")
                case [.option] :
                    print("option key is pressed")
                case [.command]:
                    print("Command key is pressed")
                case [.control, .shift]:
                    print("control-shift keys are pressed")
                case [.option, .shift]:
                    print("option-shift keys are pressed")
                case [.command, .shift]:
                    print("command-shift keys are pressed")
                case [.control, .option]:
                    print("control-option keys are pressed")
                case [.control, .command]:
                    print("control-command keys are pressed")
                case [.option, .command]:
                    print("option-command keys are pressed")
                case [.shift, .control, .option]:
                    print("shift-control-option keys are pressed")
                case [.shift, .control, .command]:
                    print("shift-control-command keys are pressed")
                case [.control, .option, .command]:
                    print("control-option-command keys are pressed")
                case [.shift, .command, .option]:
                    print("shift-command-option keys are pressed")
                case [.shift, .control, .option, .command]:
                    print("shift-control-option-command keys are pressed")
                default:
                    print("no modifier keys are pressed")
                }
            }
        }
    }
    

    【讨论】:

    • 太棒了。现在我检查了我之前的实现有点不同。你的作品就像一个魅力。非常感谢!
    • 我不明白为什么,但是当我的 CapsLock 被激活时,“案例”停止工作。并且每个打印都是“没有按下修饰键”
    • 您是否尝试过匹配他们的案例?喜欢case [.capsLock, .control, .option, .command]?
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2014-09-22
    • 2017-05-25
    • 2012-10-11
    • 2014-06-05
    相关资源
    最近更新 更多