Mac OSX 上系统范围内的滚动事件识别并将焦点设置到不同的窗口

发布于 2024-12-03 03:32:35 字数 862 浏览 1 评论 0原文

我正在我的可可应用程序中注册全局鼠标滚轮事件。我的目标是拥有某种后台应用程序,当用户在另一个应用程序的窗口中滚动时,能够聚焦另一个应用程序的窗口。如果可以使用 Objective-C 和 Cocoa,如果我想这样做,我需要走什么路线?

我的事件注册代码如下所示:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [NSEvent addGlobalMonitorForEventsMatchingMask:
    NSScrollWheelMask handler:^(NSEvent * ev) {
        NSLog(@"%@", ev.description);
    }];
}

这可行,但是在事件中捕获的数据(如窗口或 windowid)我似乎无法操作 - 并且窗口 id 似乎也不是正确的,因为我可以获得窗口列表并在其中获得不同的 ID - 只是屏幕位置似乎是准确的。所以三个问题来解决这个谜题:

  1. 如何获取某个位置的窗口或窗口 id? 屏幕?
  2. 如果我只能得到一个窗口id,我怎样才能找到合适的 要操作的应用程序或窗口对象?
  3. 我想我需要辅助功能 API 来操作 窗口并赋予其焦点。这是如何运作的?

也许这些都是简单的任务,但我以前从未编写过 Mac-Cocoa 应用程序。在建议阅读文档之前,您应该知道我已经扫描了所有文档,并且我通过示例学习比阅读书籍更好:-)

编辑:我刚刚发现我可能会使用 ProcessManager 将应用程序带到前面。如果您认为这是一个可能的解决方案,我可以获取屏幕上某个点上窗口的进程 ID 吗?

EDIT2:我不想使用 Carbon API。

I'm registering for global mouse wheel events in my cocoa application. My goal is to have some kind of background application to be able to focus a window of another application when the user scrolls in it's window. If possible with Objective-C and Cocoa, what route would I need to go if I wanted to do this?

My code for the event registering looks like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [NSEvent addGlobalMonitorForEventsMatchingMask:
    NSScrollWheelMask handler:^(NSEvent * ev) {
        NSLog(@"%@", ev.description);
    }];
}

This works, but the data captured in the event (like the window or the windowid) I don't seem to be able to manipulate - and the window id doesn't even seem to be the correct one, as I can get a list of windows and get a different id in there - just the screen position seems to be accurate. So three questions to solve this riddle:

  1. How can I get a window or window id at a certain location on the
    screen?
  2. If I can only get a window id, how can I find the appropriate
    application or window object to manipulate?
  3. I guess I would need the accessibility API for manipulating the
    window and giving it focus. How does that work?

Maybe these are simple tasks, but I've not ever written a Mac-Cocoa application before. Before suggesting documentations to read, you should know that I already scanned all the documentation, and that I better learn by example than by reading books :-)

EDIT: I just found out that I might use the ProcessManager to bring the application to the front. If you think this is a possible solution, ho can I get the process id for the window on a certain point on the screen?

EDIT2: I don't want to use Carbon APIs.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文