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)
}
6
  • 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? Commented Jul 15, 2022 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 Commented Jul 15, 2022 at 14:19
  • NSApplication manages the main event loop, is it running? Commented Jul 15, 2022 at 14:34
  • Not sure. I created a command line application. I have not been using NSApplication Commented Jul 15, 2022 at 14:40
  • Does this answer your question? How to get keyboard inputs (non string) in Command Line Tool application in Swift? Commented Jul 16, 2022 at 7:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.