vy32
vy32

Reputation: 29645

OSX: Track mouse position when not the key application

I am writing an OSX application in Swift and want to track the mouse position even when the application is not the key application. This code sequence works for my View, but the view stops receiving mouseMoved events when the application is no longer the main application:

    window!.acceptsMouseMovedEvents = true
    window!.makeMainWindow()
    window!.makeKeyAndOrderFront(self)
    window!.makeFirstResponder(self)

How do I continue to get the mouseMoved applications when my application is no longer key?

Upvotes: 0

Views: 688

Answers (1)

user887210
user887210

Reputation:

In order for it to work accessibility needs to be enabled or your app has to be trusted for accessibility. You can then call the method:

addGlobalMonitorForEventsMatchingMask(_:handler:)

This registers a handler method for the particular event type for which you want to receive notifications.

Here's Apple's documentation on monitoring events.

Upvotes: 1

Related Questions