NSResponder seems to have no mouse double click event. Is there an easy way to catch a double click?
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.
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.
The mouseDown:
and mouseUp:
methods take an NSEvent object as an argument with information about the clicks, including the clickCount
.
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).
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")
}
The NSEvent
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With