Receiving double-click event
Asked Answered
P

3

6

I've been searching the Apple docs and the only function I could find pertaining to a double click just returned the acceptable time between clicks for it to be considered a double click.

Can someone please show me an example of a double click event?

Plenty answered 19/8, 2011 at 23:0 Comment(3)
@duskwuff OSX (see tags)Plenty
@duskwuff: Also, iOS doesn't have clicks.Hendley
Right, I was wondering if you were looking for a (probably non-existent?) "double tap" event on iOS.Hominoid
F
16

Override the NSResponder method -mouseUp: and check the supplied event's clickCount. If clickCount == 2, then you're looking at a double-click. If it's 1, then a single click. 0, then they waited long enough between mouse down and mouse up that the system decided it's not a click, just distinct down then up events.

- (void)mouseUp:(NSEvent *)event
{
    NSInteger clickCount = [event clickCount];
    if (2 == clickCount) [self handleDoubleClickEvent:event];
}

This assumes the object handling the click is part of the responder chain. If not, you'll have to get your events another way, like subclassing NSApplication or NSWindow and overriding -sendEvent: to intercept the appropriate event before it gets passed along any further.

Fishwife answered 19/8, 2011 at 23:17 Comment(1)
It should be emphasized that “NSResponder” in this answer refers to the class in Cocoa (which NSView, NSWindow, and NSApplication inherit from), not the other answerer who named himself after it.Hendley
G
4

See NSEvent's -clickCount method.

Grizzled answered 19/8, 2011 at 23:18 Comment(0)
O
2

Note also that some NSControl has setDoubleAction: so that the selector registered via setDoubleAction: is sent to the target. See the official documentations of

Oculist answered 20/8, 2011 at 6:14 Comment(3)
The event (NSEvent object) is not passed to the target; an action message is sent to the target, but, as usual for an action message, the single argument is the control (table view or whatever) sending the message. I'm sure you didn't mean the NSEvent object, but I wanted to make sure that's clear.Hendley
Yes that's what I meant; I'll rephrase it. Thanks.Oculist
In my case I had to set a target for an NSTableView when running on OS prior to 10.10, but 10.10 worked OK without the target being set (self in my case)Madelyn

© 2022 - 2024 — McMap. All rights reserved.