2

Is it possible to ignore the first click if a double click is detected?

The following example will still log the one click even before the two clicks. I want to ignore the single click if there is a double click event.

- (void)mouseUp:(NSEvent *)theEvent {

    if (theEvent.clickCount > 1) {
        NSLog(@"two clicks %ld", theEvent.clickCount);
    } else {
        NSLog(@"one click %ld", theEvent.clickCount);
    }
} 

The purpose of doing this, is when the user single clicks in an NSView I want to perform a method that makes another view or window to appear. However, if the user double clicks inside the NSView then I want a different view or window to appear. The following steps outline this work flow:

  • user single clicks the NSView to make ViewA appear
  • user double clicks the NSView to make ViewB appear

The problem that I'm having is the single click is always detected therefore ViewA will always appear. But I do not want ViewA to appear if there is a double click, I just want ViewB to appear when there is a double click.

wigging
  • 8,492
  • 12
  • 75
  • 117

1 Answers1

5

Unless, you can go back in time, you will need to cancel the previous single tap event somehow.

  1. The easiest way: delay the execution on single tap event, and cancel it out if double click is detected.

    [self performSelector:@selector(singleTap) withObject:nil afterDelay:[NSEvent doubleClickInterval]];
    
    /// cancel out
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
    
  2. Or, you can execute single tap action immediately, and write a code to cancel it out if double click is detected. This should be very easy too. Because, if it's hard, there might be something wrong with your UX design.

Bird
  • 1,129
  • 7
  • 10
  • Can you give an example of implementing the 2nd option? – wigging May 18 '14 at 04:22
  • It depends, what are you going to do with single tap and double tap? – Bird May 18 '14 at 04:26
  • My goal is to call a different method when the user single clicks or double clicks. I do not want the single click method to be called if there is a double click. – wigging May 18 '14 at 04:30
  • I mean what are the users seeing? For the example, single tap select document and double tap to open it. This is very easy because, there's no need to cancel out the single tap at all. – Bird May 18 '14 at 05:01
  • @Gavin, when the first click happens, the computer doesn't know if there's going to be a second click. It can't see the future. It invokes `-mouseDown:` because that's what has happened, but there's no way for it or your code to distinguish between a lone click or the first part of a double-click because that's unknowable. Your only choice are to make the single-click action reversible/ignorable or to wait before doing anything until you're sure no second click has happened in the require time frame. That, of course, makes your UI seem sluggish. – Ken Thomases May 18 '14 at 05:47
  • 3
    @Bird, instead of using the arbitrary internal of 0.3 seconds, you should use the actual double-click interval, which you can obtain using `+[NSEvent doubleClickInterval]`. – Ken Thomases May 18 '14 at 05:49
  • @KenThomases I updated my question to help clarify what I"m trying to do. – wigging May 18 '14 at 15:46
  • I ended up using a `control-click` instead of having a different method called for a single click or double click. Thank you for the help though. – wigging May 18 '14 at 19:39
  • Somehow in iOS, a double tap cancels the single tap. Be curious to know how that's implemented. – bauerMusic Jan 02 '16 at 11:27