Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching double click in Cocoa OSX

NSResponder seems to have no mouse double click event. Is there an easy way to catch a double click?

like image 525
Mike2012 Avatar asked Feb 02 '10 00:02

Mike2012


People also ask

How do I get double-click to work?

To double-click, press your left mouse button twice quickly. If you're using a laptop with a touchpad, you can also double-tap the touchpad. Because there is no clicking on a smartphone, there is no double-clicking on a smartphone. However, some phones and programs may support double or triple tapping the screen.

What is double-clicking in mouse?

Definition of double-click : the act of selecting something on a computer screen by quickly pressing a button on a mouse or some other device two times A double click with the mouse will start the program.


4 Answers

The mouseDown: and mouseUp: methods take an NSEvent object as an argument with information about the clicks, including the clickCount.

like image 104
Chuck Avatar answered Oct 24 '22 01:10

Chuck


Generally applications look at clickCount == 2 on -[mouseUp:] to determine a double-click.

One refinement is to keep track of the location of the mouse click on the -[mouseDown:] and see that the delta on the mouse up location is small (5 points or less in both the x and the y).

like image 21
JCalhoun Avatar answered Oct 24 '22 01:10

JCalhoun


The problem with plain clickCount solution is that double click is considered simply as two single clicks. I mean you still get the single click. And if you want to react differently to that single click, you need something on top of mere click counting. Here's what I've ended up with (in Swift):

private var _doubleClickTimer: NSTimer?

// a way to ignore first click when listening for double click
override func mouseDown(theEvent: NSEvent) {
    if theEvent.clickCount > 1 {
        _doubleClickTimer!.invalidate()
        onDoubleClick(theEvent)
    } else if theEvent.clickCount == 1 { // can be 0 - if delay was big between down and up
        _doubleClickTimer = NSTimer.scheduledTimerWithTimeInterval(
            0.3, // NSEvent.doubleClickInterval() - too long
            target: self,
            selector: "onDoubleClickTimeout:",
            userInfo: theEvent,
            repeats: false
        )
    }
}


func onDoubleClickTimeout(timer: NSTimer) {
    onClick(timer.userInfo as! NSEvent)
}


func onClick(theEvent: NSEvent) {
    println("single")
}


func onDoubleClick(theEvent: NSEvent) {
    println("double")
}
like image 21
jayarjo Avatar answered Oct 24 '22 02:10

jayarjo


The NSEvents generated for mouseDown: and mouseUp: have a property called clickCount. Check if it's two to determine if a double click has occurred.

Sample implementation:

- (void)mouseDown:(NSEvent *)event {
    if (event.clickCount == 2) {
        NSLog(@"Double click!");
    }
}

Just place that in your NSResponder (such as an NSView) subclass.

like image 27
ArtOfWarfare Avatar answered Oct 24 '22 01:10

ArtOfWarfare