Global modifier key press detection in Swift
Asked Answered
S

1

13

I'm trying to use Carbon's function RegisterEventHotKey to create a hotkey for when the command key is pressed. I'm using it like so:

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

However, it doesn't call handler when I only use the command key. The handler is called if I replace cmdKey with any other non-modifier key code.

Does anyone have any suggestions that would allow the app to globally recognize when the command key is pressed? Thanks.

Spearwort answered 30/1, 2017 at 1:22 Comment(0)
H
18

You can add Global Monitor For Events matching .flagsChanged to your view controller so you can check its modifierFlags intersection with deviceIndependentFlagsMask and check the resulting keys.

Declaration

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

installs an event monitor that receives copies of events posted to other applications. Events are delivered asynchronously to your app and you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application. Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted()). Note that your handler will not be called for events that are sent to your own application.

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")
            }
        }
    }
}
Hoxie answered 30/1, 2017 at 4:50 Comment(4)
Amazing. My previous implementation was a little different now that I checked. Yours works like a charm. Thanks so much!Spearwort
I dont understand why, but when my CapsLock is Activated the "cases" stop to work. And every print is "no modifier keys are pressed"Roderic
https://mcmap.net/q/905893/-how-to-detect-caps-lock-status-on-macos-with-swift-without-a-windowHoxie
Did you try to match their cases? like case [.capsLock, .control, .option, .command]?Hoxie

© 2022 - 2024 — McMap. All rights reserved.