Fetching Global Events and Notifications in command line application with swift

64 views Asked by At

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)
}
0

There are 0 answers