Ram Patra
Ram Patra

Reputation: 16664

How to disable mouse clicks and mouse drags on a NSView?

I am making a mac app using Swift and this app has a custom view (a class extending NSView and overriding its draw method). Now, I want to disable all mouse clicks and mouse drags on this view and pass them on to the other applications running beneath my application.

I have tried the following ways (gleaned from Apple documentation and other SO questions) to disable clicks on my view and nothing worked for me so far:

1. Overriding hitTest inside my custom View class

override func hitTest(_ point: NSPoint) -> NSView? {
    let view = super.hitTest(point)
    return view == self ? nil : view
}

2. Overriding acceptsFirstMouse inside my custom View class

override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
    return false
}

3. Overriding mouseDown in ViewController as well as in my custom View class

override func mouseDown(with event: NSEvent) {
    // do nothing
}

4. Overriding mouseDragged in ViewController as well as in my custom View class

override func mouseDragged(with event: NSEvent) {
    // do nothing
}

Am I missing something?

Upvotes: 0

Views: 1188

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90531

This isn't handled at the view level, it's handled at the window level. You can set the ignoresMouseEvents property of the window to true.

The issue is that the Window Server will only dispatch an event to a single process. So, once it has arrived in your app, it's not going to another. And there's no feasible way for your app to forward it along, either.

Upvotes: 2

Related Questions