gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:47:36 28 4
gpt4 key购买 nike

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

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

有什么想法/建议吗?

更新:阅读完文档后,我发现了这一点:

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.

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

谢谢!

最佳答案

在 10.10 中 > 在您的应用程序委托(delegate)中 - 您可以 Hook 鼠标事件,然后强制窗口变为事件状态。

- (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 {
//
}

关于Cocoa NSApplication 未激活但正在接收鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8605205/

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