响应右键单击的NSButton子类 [英] NSButton subclass that responds to right clicks

查看:357
本文介绍了响应右键单击的NSButton子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSButton子类,我想使鼠标右键单击工作。只是重载 -rightMouseDown:不会剪切它,因为我想要与常规点击相同的行为(例如按钮被按下,用户可以取消

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 same kind of behaviour as for regular clicks (e.g. the button is pushed down, the user can cancel by leaving the button, the action is sent when the mouse is released, etc.).

到目前为止,我尝试的是重载 -rightMouse { Down,Up,Dragged} ,更改事件以指示鼠标左键单击,然后将其发送到 -mouse {Down,Up,Dragged} 。现在这显然是一个黑客最多,而事实证明Mac OS X不喜欢这一切。我可以点击按钮,但在释放后,按钮仍然推入。

What I have tried so far is overloading -rightMouse{Down,Up,Dragged}, changing the events to indicate the left mouse button clicks and then sending it to -mouse{Down,Up,Dragged}. Now this would clearly be a hack at best, and as it turns out Mac OS X did not like it all. I can click the button, but upon release, the button remains pushed in.

我可以模仿自己的行为,这不应该太复杂。

I could mimic the behaviour myself, which shouldn't be too complicated. However, I don't know how to make the button look pushed in.

在你说Do not!之前,这是一个非常规的Mac OS X行为,应该避免:我已经考虑这一点,右键单击可以大大改善工作流。基本上,按钮循环通过4个状态,我想右键单击,使其循环反向。这不是一个基本的功能,但它会很好。如果你仍然想说不要!,那么让我知道你的想法。非常感谢!

Before you say "Don't! It's an unconventional Mac OS X behaviour and should be avoided": I have considered this and a right click could vastly improve the workflow. Basically the button cycles through 4 states, and I would like a right click to make it cycle in reverse. It's not an essential feature, but it would be nice. If you still feel like saying "Don't!", then let me know your thoughts. I appreciate it!

谢谢!

编辑:这是我改变活动的尝试我改变了类型,所以我做了一个新的,复制所有的信息,我的意思是,我知道这是框架清楚地告诉我不要这样,但我给它一个,你这样做):

This was my attempt of changing the event (you can't change the type, so I made a new one, copying all information across. I mean, I know this is the framework clearly telling me Don't Do This, but I gave it a go, as you do):

// I've contracted all three for brevity
- (void)rightMouse{Down,Up,Dragging}:(NSEvent *)theEvent {
    NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouse{Down,Up,Dragging} location:[theEvent locationInWindow] modifierFlags:[theEvent modifierFlags] timestamp:[theEvent timestamp] windowNumber:[theEvent windowNumber] context:[theEvent context] eventNumber:[theEvent eventNumber] clickCount:[theEvent clickCount] pressure:[theEvent pressure]];
    [self mouse{Down,Up,Dragging}:event]; 
}



更新:我注意到 -mouseUp: code>从来没有发送到NSButton,如果我把它改为NSControl,它是。我不知道为什么会这样,直到弗朗西斯·麦克格鲁指出它包含自己的事件处理循环。现在,这也有意义,为什么我可以重新路由 -rightMouseDown:,但按钮不会上升发布。这是因为它是自己抓取新的事件,我不能拦截和从右到左鼠标按钮事件转换。

UPDATE: I noticed that -mouseUp: was never sent to NSButton, and if I changed it to an NSControl, it was. I couldn't figure out why this was, until Francis McGrew pointed out that it contains its own event handling loop. Now, this also made sense to why before I could reroute the -rightMouseDown:, but the button wouldn't go up on release. This is because it was fetching new events on its own, that I couldn't intercept and convert from right to left mouse button events.

推荐答案

NSButton正在进入鼠标跟踪循环。要改变这个,你必须继承NSButton并创建自己的自定义跟踪循环。尝试此代码:

NSButton is entering a mouse tracking loop. To change this you will have to subclass NSButton and create your own custom tracking loop. Try this code:

- (void) rightMouseDown:(NSEvent *)theEvent 
{
    NSEvent *newEvent = theEvent;
    BOOL mouseInBounds = NO;

    while (YES) 
        {
            mouseInBounds = NSPointInRect([newEvent locationInWindow], [self convertRect:[self frame] fromView:nil]);
            [self highlight:mouseInBounds];

            newEvent = [[self window] nextEventMatchingMask:NSRightMouseDraggedMask | NSRightMouseUpMask];

            if (NSRightMouseUp == [newEvent type])
            {
                break;
            }
        }
    if (mouseInBounds) [self performClick:nil];
}

希望它会为你工作。

这篇关于响应右键单击的NSButton子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆