Skip to content

Instantly share code, notes, and snippets.

@iolate
Created April 10, 2022 11:31
Show Gist options
  • Save iolate/ca3353a6a9e2ea760bf097d9f990bfca to your computer and use it in GitHub Desktop.
Save iolate/ca3353a6a9e2ea760bf097d9f990bfca to your computer and use it in GitHub Desktop.
Use keyboard shortcuts for status bar app using SwiftUI
// https://github.com/sindresorhus/Preferences/issues/40
// https://lapcatsoftware.com/articles/working-without-a-nib-part-10.html
import Cocoa
@objc protocol EditMenuActions {
func redo(_ sender: AnyObject)
func undo(_ sender: AnyObject)
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
let mainMenu = NSMenu(title: "MainMenu")
let menuItem = mainMenu.addItem(withTitle: "", action: nil, keyEquivalent: "")
let submenu = NSMenu()
submenu.addItem(withTitle: "Close Window", action: #selector(NSWindow.performClose(_:)), keyEquivalent: "w")
submenu.addItem(withTitle: "Undo", action: #selector(EditMenuActions.undo(_:)), keyEquivalent: "z")
submenu.addItem(withTitle: "Redo", action: #selector(EditMenuActions.redo(_:)), keyEquivalent: "Z")
submenu.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x")
submenu.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c")
submenu.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v")
submenu.addItem(withTitle: "Select All", action: #selector(NSText.selectAll(_:)), keyEquivalent: "a")
mainMenu.setSubmenu(submenu, for: menuItem)
NSApp.mainMenu = mainMenu
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment