• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: No double click event available to an NSView?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: No double click event available to an NSView?


  • Subject: Re: No double click event available to an NSView?
  • From: Ken Tozier <email@hidden>
  • Date: Sun, 4 Feb 2007 19:47:00 -0500

Well this proved to be a bit trickier than I imagined...

If a custom NSView subclass needs to respond in different ways to both single clicks and double clicks, you need to start with the assumption that all mouse downs are the first click of a double click. In the event the second click doesn't arrive in the defined double-click time, you need to set up an NSTimer with the selector of a single click handler. If a second click does arrive within the double click interval pass the event to a double click handler.

Here's the code to save others from having to reinvent this particular wheel

Ken


@interface Foo : NSView { NSTimeInterval lastMouseDown; NSTimeInterval doubleClickInterval; }

@end


@implementation Foo

- (id) init
{
	self = [super init]
	if (self)
	{
		doubleClickInterval			= (double) GetDblTime() / 60.0;
	}

	return self
}


- (void) mouseDown:(NSEvent *) inEvent { NSTimeStamp newMouseDown = [inEvent timestamp];

	if (lastMouseDown == 0)
	{
		// set the mouse down time
		lastMouseDown	= newMouseDown

		// Assume all mouse downs are the first click of a
		// double click and set a timer to fire a single
		// click handler if a second click doesn't arrive in time
		[NSTimer scheduledTimerWithTimeInterval: doubleClickInterval
					target: self
					selector: @selector(handleSingleClick:)
					userInfo: [inEvent retain]
					repeats: NO];
	}
	else if (newMouseDown - lastMouseDown <= doubleClickInterval)
	{
		// it's a double click
		[self handleDoubleClick: inEvent];
	}
}


- (void) handleSingleClick:(NSTimer *) inTimer { // since the handleDoubleClick method sets lastMouseDown to 0 // if lastMouseDown != 0 we're dealing with a single click if (lastMouseDown != 0) { NSEvent *singleClickEvent = [inTimer userInfo];

		// reset flag
		lastMouseDown		= 0;

		// do single click stuff

// since singleClickEvent was retained my the view, it needs to be explicitly released.
[singleClickEvent release];
}
}


- (void) handleDoubleClick:(NSEvent *) inEvent
{
	// reset flag
	lastMouseDown		= 0;

	// do double click stuff
}

@end




On Feb 4, 2007, at 10:41 AM, Ricky Sharp wrote:


On Feb 4, 2007, at 9:04 AM, Ken Tozier wrote:

I searched the cocoa archives, Apple's Cocoa documentation and Googled and didn't find any built in detection for double clicks in an NSView. Is that right? Or is it labeled something else?

See NSEvent's clickCount

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: No double click event available to an NSView?
      • From: Scott Stevenson <email@hidden>
References: 
 >No double click event available to an NSView? (From: Ken Tozier <email@hidden>)
 >Re: No double click event available to an NSView? (From: Ricky Sharp <email@hidden>)

  • Prev by Date: Re: NSString inaccurate Hash
  • Next by Date: Finding Source of Apple Event
  • Previous by thread: Re: No double click event available to an NSView?
  • Next by thread: Re: No double click event available to an NSView?
  • Index(es):
    • Date
    • Thread