How to listen to global hotkeys with Swift in an OS X app?

前端 未结 6 1759
北荒
北荒 2020-12-12 17:08

I\'m trying to have a handler in my Mac OS X app written in Swift for a global (system-wide) hotkey combo but I just cannot find proper documentation for it. I\'ve read that

6条回答
  •  天命终不由人
    2020-12-12 18:00

    there is a pretty hacky, but also pretty simple workaround if your app has a Menu:

    • add a new MenuItem (maybe call it something like "Dummy for Hotkey")
    • in the attributes inspector, conveniently enter your hotkey in the Key Equivalent field
    • set Allowed when Hidden, Enabled and Hidden to true
    • link it with an IBAction to do whatever your hotkey is supposed to do

    done!

提交回复
热议问题