@@ -64,11 +64,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6464 }
6565 }
6666
67+ @objc dynamic var isGridVisible : Bool = true {
68+ didSet {
69+ NotificationCenter . default. post (
70+ name: NSNotification . Name ( " gridVisibilityChanged " ) ,
71+ object: nil ,
72+ userInfo: [ " isVisible " : isGridVisible]
73+ )
74+ }
75+ }
76+
6777 private var transparencyLabel : NSTextField !
6878
6979 func applicationDidFinishLaunching( _ notification: Notification ) {
7080 DispatchQueue . main. async { [ weak self] in
7181 self ? . setupStatusItem ( )
82+ self ? . registerGlobalShortcut ( )
7283 }
7384 }
7485
@@ -103,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
103114 menuItem. view = containerView
104115 menu. addItem ( menuItem)
105116
106- // 添加透明度滑块
117+ // Add transparency slider
107118 let transparencyMenuItem = NSMenuItem ( )
108119 let transparencyContainerView = NSView ( frame: NSRect ( x: 0 , y: 0 , width: 200 , height: 40 ) )
109120
@@ -131,6 +142,41 @@ class AppDelegate: NSObject, NSApplicationDelegate {
131142 statusItem. menu = menu
132143 }
133144
145+ private func registerGlobalShortcut( ) {
146+ let options : NSDictionary = [ kAXTrustedCheckOptionPrompt. takeUnretainedValue ( ) as NSString : true ]
147+ let accessEnabled = AXIsProcessTrustedWithOptions ( options)
148+ print ( " Accessibility permission status: \( accessEnabled) " )
149+
150+ if !accessEnabled {
151+ print ( " Please grant Accessibility permission in System Settings. " )
152+ NSWorkspace . shared. open ( URL ( string: " x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility " ) !)
153+ }
154+
155+ print ( " Registering global shortcut for Command+G " )
156+
157+ // Add local monitor
158+ NSEvent . addLocalMonitorForEvents ( matching: . keyDown) { [ weak self] event in
159+ if event. modifierFlags. contains ( . command) && event. charactersIgnoringModifiers == " g " {
160+ print ( " Command+G detected in local monitor! " )
161+ self ? . toggleGridVisibility ( )
162+ return nil // Consume the event
163+ }
164+ return event
165+ }
166+
167+ // Keep global monitor
168+ NSEvent . addGlobalMonitorForEvents ( matching: . keyDown) { [ weak self] event in
169+ if event. modifierFlags. contains ( . command) && event. charactersIgnoringModifiers == " g " {
170+ print ( " Command+G detected in global monitor! " )
171+ self ? . toggleGridVisibility ( )
172+ }
173+ }
174+ }
175+
176+ private func toggleGridVisibility( ) {
177+ isGridVisible. toggle ( )
178+ }
179+
134180 @objc func quit( ) {
135181 NSApplication . shared. terminate ( nil )
136182 }
0 commit comments