Global modifier key press detection in Swift

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.

  • Cocoa/Obj-C - Open file when dragging it to application icon
  • How do I create a Delete-Line Keyboard shortcut in Xcode 8? The Xcode 3 solutions do not work anymore
  • NSString initWithData returns null
  • OpenMP support in Xcode 6 (clang 3.5)
  • Xcode and Curses.h with Error opening terminal
  • How to specify which GDB I can use in XCode on MacOS
  • Does anyone have any suggestions that would allow the app to globally recognize when the command key is pressed? Thanks.

    Solutions Collect From Internet About “Global modifier key press detection in Swift”

    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.


    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() {
            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")
                    print("no modifier keys are pressed")