Kashif
Kashif

Reputation: 4632

Capture clicks on menu bar for my macOS app

Below statement captures all clicks inside my app window but does not capture the clicks on the menu bar for my app. I am also unable to capture menu bar click for my app using addGlobalMonitorForEvents

NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown , handler: {
            (mouseEvent:NSEvent?) in ....

Upvotes: 1

Views: 263

Answers (1)

brimstone
brimstone

Reputation: 3400

For me, using the globalMonitor along with NSEvent's locationInWindow property seemed to output coordinates of the click, even when not on the window of the app. Here's my implementation:

NSEvent.addGlobalMonitorForEvents(matching: .leftMouseDown) { event in
    print(event.locationInWindow)
}
  • As a side note, the click coordinates go from (0,0) in the bottom left of my screen to (width,height) of my screen at the top right

Upvotes: 2

Related Questions