gpt4 book ai didi

cocoa nextEventMatchingMask 未接收 NSMouseMoved 事件

转载 作者:行者123 更新时间:2023-12-03 18:05:42 25 4
gpt4 key购买 nike

我创建了一个本地事件循环并显示了一个无边框窗口(源自 NSPanel),我发现在事件循环中没有收到 NSMouseMoved 事件,尽管我可以接收鼠标按钮向下/向上事件。

我应该怎么做才能获取 NSMouseMoved 事件?我发现将 float 窗口设置为关键窗口可以接收 NSMouseMoved 事件,但我不想更改关键窗口。看来这是可能的,因为我发现点击System Dock Bar中的测试应用程序图标后,我可以收到mousemoved事件,并且关键窗口/主窗口没有变化。

这是我的测试代码:(创建一个名为 FloatWindowTest 的 Cocoa App 项目,并放置一个与 onClick: IBAction 链接的按钮)。提前致谢!

-乔尼

#import "FloatWindowTestAppDelegate.h"

@interface FloatWindow : NSPanel
@end

@interface FloatWindowContentView : NSView
@end

@implementation FloatWindowTestAppDelegate

@synthesize window;

- (void)delayedAction:(id)sender
{
// What does this function do?
// 1. create a float window
// 2. create a local event loop
// 3. print out the events got from nextEventMatchingMask.
// 4. send it to float window.

// What is the problem?
// In local event loop, althrough the event mask has set NSMouseMovedMask
// there's no mouse moved messages received.
//

FloatWindow* floatWindow = [[FloatWindow alloc] init];

NSEvent* event = [NSApp currentEvent];
NSPoint screenOrigin = [[self window] convertBaseToScreen:[event locationInWindow]];
[floatWindow setFrameTopLeftPoint:screenOrigin];
[floatWindow orderFront:nil];


//Making the float window as Key window will work, however
//change active window is not anticipated.
//[floatWindow makeKeyAndOrderFront:nil];

BOOL done = NO;
while (!done)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSUInteger eventMask = NSLeftMouseDownMask|
NSLeftMouseUpMask|
NSMouseMovedMask|
NSMouseEnteredMask|
NSMouseExitedMask|
NSLeftMouseDraggedMask;

NSEvent* event = [NSApp nextEventMatchingMask:eventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];

//why I cannot get NSMouseMoved event??
NSLog(@"new event %@", [event description]);
[floatWindow sendEvent:event];
[pool drain];
}

[floatWindow release];
return;
}

-(IBAction)onClick:(id)sender
{
//Tried to postpone the local event loop
//after return from button's mouse tracking loop.
//but not fixes this problem.
[[NSRunLoop currentRunLoop]
performSelector:@selector(delayedAction:)
target:self
argument:nil
order:0
modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
}
@end



@implementation FloatWindow

- (id)init
{
NSRect contentRect = NSMakeRect(200,300,200,300);
self = [super initWithContentRect:contentRect
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:YES];

if (self) {
[self setLevel:NSFloatingWindowLevel];

NSRect frameRect = [self frameRectForContentRect:contentRect];
NSView* view = [[[FloatWindowContentView alloc]
initWithFrame:frameRect] autorelease];
[self setContentView:view];

[self setAcceptsMouseMovedEvents:YES];
[self setIgnoresMouseEvents:NO];
}
return self;
}

- (BOOL)becomesKeyOnlyIfNeeded
{
return YES;
}

- (void)becomeMainWindow
{
NSLog(@"becomeMainWindow");
[super becomeMainWindow];
}

- (void)becomeKeyWindow
{
NSLog(@"becomeKeyWindow");
[super becomeKeyWindow];
}

@end

@implementation FloatWindowContentView

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}

- (BOOL)acceptsFirstResponder
{
return YES;
}

- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
NSTrackingArea* area;
area = [[NSTrackingArea alloc] initWithRect:frameRect
options:NSTrackingActiveAlways|
NSTrackingMouseMoved|
NSTrackingMouseEnteredAndExited
owner:self
userInfo:nil];
[self addTrackingArea:area];
[area release];
}
return self;
}

- (void)drawRect:(NSRect)rect
{
[[NSColor redColor] set];
NSRectFill([self bounds]);
}

- (BOOL)becomeFirstResponder
{
NSLog(@"becomeFirstResponder");
return [super becomeFirstResponder];
}

@end

最佳答案

我想我找到了正确的答案。这在某种程度上与告诉 NSWindow 接受 MouseMoved 事件有关。但令我惊讶的是,应该接受 mouseMoved 事件的窗口不是 float 窗口,而是当前的按键窗口。所以解决办法很简单,只要在开始跟踪前让按键窗口接受鼠标移动事件,跟踪结束后恢复开关即可。

关于 cocoa nextEventMatchingMask 未接收 NSMouseMoved 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2565235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com