实现“鼠标跟随焦点"在 MacOS 上 [英] Implement "Mouse Follows Focus" on MacOS

查看:56
本文介绍了实现“鼠标跟随焦点"在 MacOS 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个显示器,在应用程序之间移动时,通过键盘通常会更快,所以我倾向于通过键盘快捷键进行大部分窗口管理/导航.但是,有时需要鼠标.我的问题是,当我改变活动窗口时,我的鼠标可以在我当前的任何屏幕上,所以我必须先找到它,然后将点拖到我需要点击的地方.

I have multiple monitors, and when moving between apps, it is normally quicker to do that via the keyboard, so I tend to do most of my window management/navigation via keyboard shortcuts. However, there are times when the mouse is necessary. My problem is, that when I change the active window, my mouse can be on any of my current screens, so I have to find it first, and then drag the point to where I need to click.

我想要的是:
A) 当我通过 cmd-Tab 切换活动窗口时,让鼠标移动到活动窗口的中心.
B) 有一个键盘快捷键,我可以在需要时按下以快速将鼠标指针调用到活动窗口

What I would like tis to either:
A) Have the mouse move to the center of the active window when I switch active windows via cmd-Tab.
B) Have a keyboard shortcut I can press to quickly call the mouse pointer to the active window if I need it

我发现了许多黑客"和单独支持 FFM 的应用程序,但我想要 MFF.有人知道怎么做吗?

I have found many "hacks" and apps which individually support FFM, but I want MFF. Anyone know how to do this?

推荐答案

基本概念很简单,虽然在实现上有一些怪癖.下面的脚本应该执行您在 (A) 中的要求,但需要注意的是它无法区分如何打开应用程序.只要应用程序处于活动状态,它就会运行——通过 cmd-tab、单击停靠栏图标、双击应用程序图标等——如果没有打开的窗口或发生其他一些错误情况,它将静默失败.

The basic concept is simple enough, though there are quirks in the implementation. The following script should do what you ask for in (A), with the caveat that it can't distinguish how an app is opened. It will operate whenever an app becomes active — through cmd-tab, a click on the dock icon, a double-click on the app icon, etc. — and will fail silently if there are no open windows or some other error condition occurs.

将以下脚本复制到脚本编辑器中,并将其保存为保持打开状态的应用程序(从文件格式下拉菜单中选择应用程序",然后单击运行处理程序后保持打开状态"复选框).您需要在安全首选项中为其授予可访问性权限,并且每次编辑脚本应用程序时都必须关闭和打开这些权限.

Copy the following script into the Script Editor, and save it as an stay-open application (select 'Application' from the File Format pulldown menu, and click the 'stay open after run handler' checkbox). You'll need to give it Accessibility permissions in security preferences, and you'll have to toggle those permissions off and on again every time you edit the script application.

use AppleScript version "2.4"
use framework "AppKit"
use scripting additions

property NSWorkspace : class "NSWorkspace"

on run
    set workSp to NSWorkspace's sharedWorkspace()
    set notifCent to workSp's notificationCenter()
    tell notifCent to addObserver:me selector:"activeAppHasChanged:" |name|:"NSWorkspaceDidActivateApplicationNotification" object:(missing value)
end run

on idle
    -- we don't use the idle loop, so tell the system let the app sleep. this comes out of idle once an hour
    return 3600
end idle

on activeAppHasChanged:notif
    set targetApp to (notif's userInfo's valueForKey:"NSWorkspaceApplicationKey")
    set targetAppName to (targetApp's localizedName) as text
    tell application "System Events"
        tell process targetAppName
            try
                set {xpos, ypos} to position of first window
                set {w, h} to size of first window
                my mouseMove(xpos + w / 2, ypos + h / 2)
            end try
        end tell
    end tell
end activeAppHasChanged:

on mouseMove(newX, newY)
    do shell script "
    
/usr/bin/python <<END

from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
from Quartz.CoreGraphics import kCGEventMouseMoved
import time

def mouseEvent(posx, posy):
          theEvent = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (posx,posy), kCGMouseButtonLeft)
          CGEventPost(kCGHIDEventTap, theEvent)

mouseEvent(" & newX & "," & newY & ");

END"
end mouseMove

启动脚本并让它在后台运行.每次切换应用程序时,光标都会在应用程序的第一个打开窗口中居中.如果您更喜欢选项 (B) - 键盘快捷键路线 - 这也是可行的,但它是一种有点不同的方法.询问您是否需要帮助实施它.

Launch the script and leave it running in the background. Every time you switch apps, it will center the cursor in the first open window of the application. If you prefer option (B) — the keyboard shortcut route — that's also doable, but it's a bit different approach. Ask if you need help implementing it.

这篇关于实现“鼠标跟随焦点"在 MacOS 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆