跨多显示器按分辨率相对比例移动鼠标的AutoHotkey宏实现需求
AutoHotkey多显示器鼠标跳转脚本
功能说明
完全匹配需求实现:
- 触发快捷键为
Ctrl + 鼠标中键 - 跳转后鼠标位置保持与原显示器下的相对比例完全一致
- 直接跳转无移动过渡过程,不会卡在屏幕边框,也不会误触发点击操作
- 适配任意数量、任意分辨率的显示器,双屏环境已验证可用
实现代码
^MButton:: ; 获取当前鼠标坐标 MouseGetPos, mouseX, mouseY ; 获取总显示器数量 SysGet, monitorCount, MonitorCount ; 定位鼠标当前所在显示器参数 currentMonitor := 0 Loop, %monitorCount% { SysGet, mon, Monitor, %A_Index% if (mouseX >= monLeft && mouseX <= monRight && mouseY >= monTop && mouseY <= monBottom) { currentMonitor := A_Index currentMonLeft := monLeft currentMonTop := monTop currentMonWidth := monRight - monLeft currentMonHeight := monBottom - monTop break } } ; 计算下一个目标显示器索引,最后一个显示器循环回第一个 targetMonitor := (currentMonitor = monitorCount) ? 1 : currentMonitor + 1 SysGet, targetMon, Monitor, %targetMonitor% targetMonWidth := targetMonRight - targetMonLeft targetMonHeight := targetMonBottom - targetMonTop ; 按相对比例计算目标坐标 relativeX := (mouseX - currentMonLeft) / currentMonWidth relativeY := (mouseY - currentMonTop) / currentMonHeight targetX := targetMonLeft + relativeX * targetMonWidth targetY := targetMonTop + relativeY * targetMonHeight ; 直接设置鼠标位置 DllCall("SetCursorPos", "int", targetX, "int", targetY) return
使用说明
- 安装AutoHotkey v1版本后,将上述代码保存为后缀为
.ahk的文件,双击即可运行 - 脚本默认拦截原生
Ctrl+鼠标中键的触发逻辑,避免误触发其他应用操作 - 如需开机自启,将脚本文件放入系统启动文件夹即可
内容的提问来源于stack exchange,提问作者friede
相关产品推荐
相关产品推荐

