Re: No double click event available to an NSView?
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