Re: Button up and down cause two different actions
Re: Button up and down cause two different actions
- Subject: Re: Button up and down cause two different actions
- From: Shaun Wexler <email@hidden>
- Date: Fri, 17 Mar 2006 07:37:11 -0800
On Mar 17, 2006, at 1:35 AM, david camargo wrote:
I was just wondering if there might be a quick method
of configuring a button to have two different action
when holding a button down and then releasing it,
this is in contrast to a regular button that takes a
mouse down and a mouse up to initiate the buttons
action. Just hoping to get the effect of initiating of
an action and sustaining the action while the button
is pressed down and then ending the action when the
button is released, just like an electronic keyboard.
Any help is greatly appreciated.
@interface SKWInstantButton : NSButton
@end
@implementation SKWInstantButton
- (void)mouseDown:(NSEvent *)event
{
SEL action = [self action];
id target = [self target];
BOOL targetPerformedAction = NO;
// perform action immediately with minimal latency, before
highlighting, etc...
if (action) {
if (!target) {
targetPerformedAction = [NSApp sendAction:action
to:target from:self];
} else {
targetPerformedAction = NULL !=
SKWPerformIfObjectImplementsSelectorWithArg(target, action, self);
}
}
if (!targetPerformedAction) {
NSBeep();
}
else {
// apply "pressed" highlighting to button
NSCell *cell = [self cell];
[cell setHighlighted:YES];
// process events normally until mouseUp:, without tracking
the mouse
NSDate *distantFuture = [NSDate distantFuture];
BOOL stillDown = YES;
do {
NSEvent *nextEvent = [NSApp
nextEventMatchingMask:NSAnyEventMask untilDate:distantFuture
inMode:NSDefaultRunLoopMode dequeue:YES];
if ([nextEvent type] == NSLeftMouseUp) {
stillDown = NO;
} else {
[NSApp sendEvent:nextEvent];
}
} while (stillDown);
// send action again for one-shot toggle behavior without
extra action message tracking!
if ([self isContinuous]) {
if (!target) {
targetPerformedAction = [NSApp sendAction:action
to:target from:self];
} else {
targetPerformedAction = NULL !=
SKWPerformIfObjectImplementsSelectorWithArg(target, action, self);
}
}
// restore "normal" highlighting to button
[cell setHighlighted:NO];
}
}
@end
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden