mouseDown 时禁用 mouseEntered 事件(NSEvents Mac)

发布于 2024-11-25 09:28:31 字数 269 浏览 1 评论 0 原文

我创建了一个 NSButton 类,当滚动按钮时,它很高兴地检测到 mouseEntered 和 mouseExited 事件。但是,一旦 mouseDown 事件发生,只要鼠标按下,mouseEntered 事件就不再被调用,直到鼠标按钮被抬起。

因此,当调用 mouseDown 事件时,不再调用 mouseEntered 或 MouseExited 事件,滚动到其他按钮时也不会调用 mouseDown ,直到我放开最初的 mouseDown 。

所以我想在鼠标按下时检测鼠标何时进入。

I have created an NSButton class, and when rolling over my buttons it happily detects mouseEntered and mouseExited events. But as soon as the mouseDown event occurs, so long as the mouse it down, mouseEntered events are no longer called until the mouse button is lifted.

So, when the mouseDown event is called, mouseEntered or MouseExited events are no longer called, nor is mouseDown called when rolling over other buttons until I let go of the initial mouseDown.

So I would like to detect when my mouse enters while the mouse is down as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉黛浅 2024-12-02 09:28:31

结果我只需要将 NSTrackingEnabledDuringMouseDrag 添加到我的 NSTrackingAreaOptions 中。 mouseEntered 和 mouseExited 事件现在在用鼠标向下拖动时触发。

Turns out I just needed to add NSTrackingEnabledDuringMouseDrag to my NSTrackingAreaOptions. mouseEntered and mouseExited events now fire when dragging with mouse down.

浅蓝的眸勾画不出的柔情 2024-12-02 09:28:31

当 NSButton 接收到鼠标按下事件时,它会输入 私人跟踪循环,处理所有在鼠标抬起之前发布的鼠标事件。您可以设置自己的跟踪循环来根据鼠标位置执行操作:

- (void) mouseDown:(NSEvent *)event {

    BOOL keepTracking = YES;
    NSEvent * nextEvent = event;

    while( keepTracking ){

        NSPoint mouseLocation = [self convertPoint:[nextEvent locationInWindow]
                                          fromView:nil];
        BOOL mouseInside = [self mouse:mouseLocation inRect:[self bounds]];
        // Draw highlight conditional upon mouse being in bounds
        [self highlight:mouseInside];

        switch( [nextEvent type] ){
            case NSLeftMouseDragged:
                /* Do something interesting, testing mouseInside */
                break;
            case NSLeftMouseUp:
                if( mouseInside ) [self performClick:nil];
                keepTracking = NO;
                break;
            default:
                break;
        }

        nextEvent = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
    }
}

When an NSButton receives a mouse down event, it enters a private tracking loop, handling all the mouse events that are posted until it gets a mouse up. You can set up your own tracking loop to do things based on the mouse location:

- (void) mouseDown:(NSEvent *)event {

    BOOL keepTracking = YES;
    NSEvent * nextEvent = event;

    while( keepTracking ){

        NSPoint mouseLocation = [self convertPoint:[nextEvent locationInWindow]
                                          fromView:nil];
        BOOL mouseInside = [self mouse:mouseLocation inRect:[self bounds]];
        // Draw highlight conditional upon mouse being in bounds
        [self highlight:mouseInside];

        switch( [nextEvent type] ){
            case NSLeftMouseDragged:
                /* Do something interesting, testing mouseInside */
                break;
            case NSLeftMouseUp:
                if( mouseInside ) [self performClick:nil];
                keepTracking = NO;
                break;
            default:
                break;
        }

        nextEvent = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
    }
}
很酷不放纵 2024-12-02 09:28:31

当按下鼠标左键时,开始拖动。如果我没记错的话,拖动期间不会发送鼠标移动事件,这可能是您没有收到 mouseEnteredmouseExited 消息的原因之一。但是,如果您实现 NSDraggingDestination 协议并将您的视图注册为所拖动数据类型的可能接收者,您将获得 draggingEntereddraggingExited消息代替。

拖放编程主题的 rel="nofollow">拖动目标部分。

When the left mouse button is down, dragging begins. If I remember correctly, mouse moved events aren't sent during drags, and that's probably one reason that you don't get mouseEntered and mouseExited messages. However, if you implement the NSDraggingDestination protocol and register your view as a possible recipient of the type of data being dragged, you'll get draggingEntered and draggingExited messages instead.

Read about it in the Dragging Destinations section of Drag and Drop Programming Topics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文