0

I have a command line application and I would like to receive global mousemove and keypress events as well as some notifications. However this will not work. The console Application does have accessibility rights when started.

There is no error printed but it simply does not work. What could be the issue?

The Notification is working while the NSEvent is not working.

NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown, handler: logEvent)
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.mouseMoved, handler: logEvent)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.willPowerOffNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.screensDidSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.sessionDidResignActiveNotification, object: nil)

@objc dynamic func logEvent(_ incomingEvent: NSEvent?) -> Void{
    print("here was a keypress or mousemove ")
}

@objc dynamic func setInactive(aNotification : NSNotification){
    Log("Event was called " + aNotification.name.rawValue)
}
Silve2611
  • 2,198
  • 2
  • 34
  • 55
  • 1
    It's a Command Line app, so I guess the apps "ends" before getting any event? How do you ensure that you keep your app alive? – Larme Jul 15 '22 at 14:16
  • I have RunLoop.main.run() in the main.swift file. The Application is running and doing something every x seconds. However it cannot capture mouse or key events – Silve2611 Jul 15 '22 at 14:19
  • `NSApplication` manages the main event loop, is it running? – Willeke Jul 15 '22 at 14:34
  • Not sure. I created a command line application. I have not been using NSApplication – Silve2611 Jul 15 '22 at 14:40
  • Does this answer your question? [How to get keyboard inputs (non string) in Command Line Tool application in Swift?](https://stackoverflow.com/questions/69923061/how-to-get-keyboard-inputs-non-string-in-command-line-tool-application-in-swif) – Willeke Jul 16 '22 at 07:51
  • I do not think so. As I said the permissions are there. I think I will try another solution. – Silve2611 Jul 17 '22 at 21:15

0 Answers0