即使光标移出电影后,也会继续跟踪鼠标拖动事件

时间:2021-08-14 20:24:07

I have few views aligned in grids in parent view (All being NSView's )

我在父视图中的网格中对齐的视图很少(全部是NSView的)

I am overriding -(void)mouseDown:(NSEvent *)event - (void)mouseDragged:(NSEvent *)theEvent for some custom drawing in child view subclass

我重写 - (void)mouseDown:(NSEvent *)event - (void)mouseDragged:(NSEvent *)theEvent for child view子类中的一些自定义绘图

To be specific, I draw some rectangle boxes during mouse drag in child view's.

具体来说,我在子视图的鼠标拖动过程中绘制了一些矩形框。

Problem: when cursor moves out of the child view( during mouse drag ) , obviously, I am not able to track the event and hence I cannot resize the rectangle. I want to track the mouse movements even outside the application window... (for now just the drag event )

问题:当光标移出子视图时(鼠标拖动期间),显然,我无法跟踪事件,因此无法调整矩形的大小。我想跟踪应用程序窗口外的鼠标移动...(现在只是拖动事件)

Is there any obvious or complex way to achieve this.....

有没有明显或复杂的方法来实现这一点.....

Thanks in Advance

提前致谢

Rajesh

2 个解决方案

#1


- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint point;
    while (1) {
        theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
        point = [self convertPoint: [theEvent locationInWindow] fromView: nil];

        // do something with point

        if ([theEvent type] == NSLeftMouseUp)
            break;
    }
}

#2


I want to present an alternative to the accepted answer which doesn't involve intercepting events in a while loop.

我想提出一个替代接受的答案,它不涉及在while循环中拦截事件。

Handle the mouse dragged event:

处理鼠标拖动事件:

- (void)mouseDragged:(NSEvent *)theEvent
{
}

This will fire if you start a drag inside an NSView and will continue to fire if you drag the mouse outside it.

如果在NSView内部开始拖动,则会触发,如果将鼠标拖到外部,则会继续触发。

#1


- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint point;
    while (1) {
        theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
        point = [self convertPoint: [theEvent locationInWindow] fromView: nil];

        // do something with point

        if ([theEvent type] == NSLeftMouseUp)
            break;
    }
}

#2


I want to present an alternative to the accepted answer which doesn't involve intercepting events in a while loop.

我想提出一个替代接受的答案,它不涉及在while循环中拦截事件。

Handle the mouse dragged event:

处理鼠标拖动事件:

- (void)mouseDragged:(NSEvent *)theEvent
{
}

This will fire if you start a drag inside an NSView and will continue to fire if you drag the mouse outside it.

如果在NSView内部开始拖动,则会触发,如果将鼠标拖到外部,则会继续触发。