Cocoa NSApplication 未激活但正在接收鼠标事件

发布于 2024-12-22 06:50:08 字数 694 浏览 2 评论 0原文

我遇到了一个奇怪的问题,我点击离开窗口。它放弃了主要和关键。然后我单击返回它,并且单击的视图不响应鼠标单击。但第二次单击确实会导致视图做出响应。就好像在第一次单击期间,窗口不是关键,而单击使其成为关键并能够处理第二次单击。

我对 NSApplication 进行了子类化并拦截了 sendEvent: 以查看我的应用程序是否在第一次单击事件期间获取了事件,确实如此。此外,我转储了响应者链,发现我的视图确实在链中,但直到第二次单击之前窗口并不是关键。我还注意到,当我收到第一次单击事件时,应用程序甚至没有处于活动状态。这怎么可能?应用程序不是应该在收到鼠标事件时激活吗?先有鸡还是先有蛋……

有什么想法/建议吗?

更新:阅读文档后我发现:

当用户在光标位于视图对象上时按下鼠标按钮时,会发送鼠标按下事件。如果包含视图的窗口不是关键窗口,则该窗口将成为关键窗口并丢弃鼠标按下事件。但是,视图可以通过重写 NSView 的acceptFirstMouse: 方法以返回 YES 来规避此默认行为。

这似乎就是正在发生的事情。但是,我尝试覆盖 acceptsFirstMouse:acceptsFirstResponder 但无济于事。我的观点仍然不是“接受第一只老鼠”。

谢谢!

I'm having a weird issue where I click away from my window. It resigns main and key. Then I click back to it and the view that was clicked on does not respond to the mouse click. But the second click does cause the view to respond. As if during the first click, the window wasn't key and the click caused it to become key and was able to handle the second click.

I subclassed NSApplication and intercepted sendEvent: to see if my application was getting the events during the first click event and indeed it was. Furthermore, I dumped the responder chain and saw that indeed my view was in the chain, but the window was not key until the second click. I also noticed that when I receive the first click event, the application isn't even active. How is this possible? Isn't an application supposed to become active when it receives a mouse event? Chicken/egg...

Any thoughts/sugggestions?

Update: After reading through the docs I found this:

Mouse-down events are sent when a user presses the mouse button while the cursor is over a view object. If the window containing the view is not the key window, the window becomes the key window and discards the mouse-down event. However, a view can circumvent this default behavior by overriding the acceptsFirstMouse: method of NSView to return YES.

So that appears to be what is happening. However, I tried overriding acceptsFirstMouse: and acceptsFirstResponder but to no avail. My views are still not "accepting first mouse".

Thanks!

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

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

发布评论

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

评论(1

掩耳倾听 2024-12-29 06:50:08

在10.10>在您的应用程序委托中 - 您可以挂钩鼠标事件,然后强制窗口变为活动状态。

- (void)applicationDidUpdate:(NSNotification *)notification {
     NSLog(@"did update");
    // [[NSApp mainWindow] makeKeyWindow];doesn't work. not sure why. 
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

- (void)applicationDidResignActive:(NSNotification *)notification {
   // your window lost focus here
}

- (void)applicationDidBecomeActive:(NSNotification *)notification {
   // 
}

in 10.10 > in your app delegate - you can hook into the mouse events then force the window to become active.

- (void)applicationDidUpdate:(NSNotification *)notification {
     NSLog(@"did update");
    // [[NSApp mainWindow] makeKeyWindow];doesn't work. not sure why. 
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

- (void)applicationDidResignActive:(NSNotification *)notification {
   // your window lost focus here
}

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