为什么我的 NSWindow 只在第一次接收 mouseOver 事件?

发布于 2024-09-02 03:12:29 字数 365 浏览 12 评论 0 原文

我有一个应用程序,使用 orderOut 和 orderFront 显示和隐藏无边框窗口。当它可见时,我希望当鼠标移到它上面时它成为关键窗口。 到目前为止,我已经这样做了:

  • 在 awakeFromNib 中,我已将其第一响应者设置为自身。
  • 在窗口的构造函数中,我将接受鼠标事件设置为“是”。
  • 在 mouseMoved 方法中,我使用 makeKeyAndOrderToFront。

我的问题是,这仅在我第一次将鼠标移到窗口上时才有效。 之后,它不会收到任何 mouseOver 事件。 我尝试检查firstResponder,但据我所知它永远不会从窗口发生变化。

有什么想法我可以做些什么来让它发挥作用吗?

I have an application where a borderless window is shown and hidden, using orderOut and orderFront. When it is visible, I want the it to become the key window when the mouse moves over it.
So far I've done this:

  • In awakeFromNib I have set its first responder to itself.
  • In the window's constructor I set accepts mouse events to YES.
  • In the mouseMoved method, I use makeKeyAndOrderToFront.

My problem is, that this only works the first time I move the mouse over the window.
After that, it doesn't receive any mouseOver events.
I've tried checking the firstResponder but as far as I can tell it never changes from the window.

Any ideas what I can do to get this working?

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

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

发布评论

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

评论(2

沉鱼一梦 2024-09-09 03:12:29

您需要添加跟踪区域 如果你想接收 mouseMoved 事件(我假设这就是你的意思,因为 Cocoa 没有 mouseOver 事件之类的东西)。

我编写了一个名为 Shroud 的小应用程序,它具有类似的功能 - 它隐藏一个无边框窗口,当您将鼠标移到菜单栏上时,该窗口会覆盖菜单栏。 代码足够简单,作为示例可能很有用。

You need to add a tracking area if you want to receive mouseMoved events (I assume that's what you mean as Cocoa has no such thing as a mouseOver event).

I wrote a little app called Shroud which does something similar — it hides a borderless window which covers the menu bar when you move the mouse over it. The code is simple enough it might be useful as an example.

夜血缘 2024-09-09 03:12:29

以下是在 @NicholasRiley答案

NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self.view frame] options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways owner:self userInfo:nil];
[self.view addTrackingArea:area];

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"mouseEntered");
}

-(void)mouseExited:(NSEvent *)theEvent {
    NSLog(@"mouseExited");
}

Here's an example written with the help of @NicholasRiley's answer:

NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self.view frame] options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways owner:self userInfo:nil];
[self.view addTrackingArea:area];

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"mouseEntered");
}

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