Search code examples
swiftuiglobal-hotkey

Use HotKey Library in SwiftUI


I'm trying to use this library: soffes/HotKey in an SwiftUI Application for MacOS. The author describes the usage as follows:

  1. Set up Hotkey:
let hotKey = HotKey(key: .r, modifiers: [.command, .option])
  1. set the keyDownHandler and get callbacks for when your hot key is pressed:
hotKey.keyDownHandler = {
  print("Pressed at \(Date())")
}

My problem is that I don't know where I have to put the Handler. Any Ideas?


Solution

  • Okay so I figured it out. The keyDownHandlerreally just defines what should happen when a hotkey is pressed so you can just put it into the .onAppear {} after your body View