NSButton subclass that responds to right clicks

后端 未结 3 1219
余生分开走
余生分开走 2021-01-19 01:10

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

3条回答
  •  不知归路
    2021-01-19 01:26

    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.

提交回复
热议问题