Re: Right (or cntl)-clicking a button
Re: Right (or cntl)-clicking a button
- Subject: Re: Right (or cntl)-clicking a button
- From: Sherm Pendley <email@hidden>
- Date: Fri, 18 Apr 2003 22:40:47 -0400
On Friday, April 18, 2003, at 06:07 PM, Justin Lundy wrote:
I have a simple button that calls a method when clicked (crazy, I
know!). Short of subclassing, how can I find out if it was a
right-click (cntl-click), or a left-click (regular click)?
In your action method, you can get the button's parent window object by
send it a "window" message. You can get the current NSEvent object from
the window object by sending it a "currentEvent" message.
Once you have the event object, you can send it a "modifierFlags"
message, which returns an unsigned int. AND that int with the mask you
wish to check for - which would be NSControlKeyMask in your case; a full
list of masks is given in the NSEvent docs.
Here's some example code. All the usual caveats apply - typed in email,
untested, etc.:
- (IBACTION) buttonClicked: (NSButton) sender {
if ([[[sender window] currentEvent] modifierFlags] &&
NSControlKeyMask) {
// Handle control-click
} else {
// Handle ordinary click
}
}
sherm--
C programmers never die - they're just cast into void.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.