# MacOS + SwiftUI global keybinding to bring window to front

Most guides I found focused on using the .keyboardShortcut() API on SwiftUI however this doesn't work when you need to have those keyboard shortcuts act when the app is in the background. This is a short and sweet document detailing how I set up global keybinding in my SwiftUI Lifecycle MacOS app (Thats a mouthful!).

# Install HotKey

First you need to install HotKey - Its a library that abstracts away all of the system calls needed to register your hotkeys. It features an incredibly simple API that will enable us to set up our command in one line.

In XCode, go to File > Swift Packages > Add Package Dependency then add the HotKey repo url (https://github.com/soffes/HotKey).

# Register Hotkey

Now you're ready to register your hotkey!

Find the <appname>App file in your project and add the import statement for HotKey.

import HotKey

Now, add a property initializer inside the App struct that looks like this:

let hotKey = HotKey(key: .r, modifiers: [.command, .option], keyDownHandler: {NSApp.activate(ignoringOtherApps: true)})

This is all you need to have a global hotkey to bring your window to the front!

# Example File

import SwiftUI
import HotKey

struct MacOSApp: App {
    // CMD + OPT + R
    let hotKey = HotKey(key: .r, modifiers: [.command, .option], keyDownHandler: {NSApp.activate(ignoringOtherApps: true)})
    var body: some Scene {
        WindowGroup {