I have an NSButton subclass that I would like to make work with right mouse button clicks. Just overloading -rightMouseDown:
won\'t cut it, as I would like the
I've turned a left mouse click-and-hold into a fake right mouse down on a path control. I'm not sure this will solve all your problems, but I found that the key difference when I did this was changing the timestamp:
NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
location:[theEvent locationInWindow]
modifierFlags:[theEvent modifierFlags]
timestamp:CFAbsoluteGetTimeCurrent()
windowNumber:[theEvent windowNumber]
context:[theEvent context]
// I was surprised to find eventNumber didn't seem to need to be faked
eventNumber:[theEvent eventNumber]
clickCount:[theEvent clickCount]
pressure:[theEvent pressure]];
The other thing is that depending on your button type, its state
may be the value that is making it appear pushed or not, so you might trying poking at that.
UPDATE: I think I've figured out why rightMouseUp:
never gets called. Per the -[NSControl mouseDown:] docs, the button starts tracking the mouse when it gets a mouseDown
event, and it doesn't stop tracking until it gets mouseUp
. While it's tracking, it can't do anything else. I just tried, for example, at the end of a custom mouseDown:
:
[self performSelector:@selector(mouseUp:) withObject:myFakeMouseUpEvent afterDelay:1.0];
but this gets put off until a normal mouseUp:
gets triggered some other way. So, if you've clicked the right mouse button, you can't (with the mouse) send a leftMouseUp
, thus the button is still tracking, and won't accept a rightMouseUp
event. I still don't know what the solution is, but I figured that would be useful information.