夜火笔记

多屏幕鼠标一键跳转显示屏 AHK脚本

2023-09-19
笔记
6分钟
1149字

身为双屏用户,因为鼠标手的原因,一直想找个多屏幕一键鼠标跳转到另一个显示屏的软件,以减少鼠标长距离移动,减缓疼痛。但是一直没碰到想要的,不知道是都没这需求还是我找的方式不对。

自以为这需求偏系统底层了,需要C,C++之类的底层语言来实现,就一直没研究,今天突然想起来AutoHotkey,搜了下,还真有类似的实现 AutoHotkey配置鼠标光标在屏幕之间瞬移

不过他实现的脚本,对我右屏作为主屏的来说,有点问题,而且这种只能应对双屏,不能应对更多屏幕,不是那么完美。

于是又自己实现了一遍

使用

win + alt + x 将鼠标跳转到下一个屏幕的中心
win + alt + z 将鼠标跳转到上一个屏幕的中心
win + alt + c 显示鼠标当前所在位置的坐标

用win键来设置快捷键主要不想有冲突,至于使用起来按键多,麻烦的问题,我是用鼠标多余的功能键映射快捷键,使用鼠标的时候一键就触发了;使用触摸板的时候,也近一些

之前忘了触摸板的三指手势设置的是 power toys 的 鼠标跳转,把一部分手势换成这个快捷键,用起来更丝滑了。至于为什么不继续用 power toys ,从那个缩略图显示,再到点击缩略图,感觉太拖沓了,不如直接一键跳转来的爽利。

跳转后会自动激活鼠标所在的窗口,方便直接操作。

由于我只有2个显示屏,因此多余2个显示屏的使用,未经测试

代码

1
CoordMode, Mouse, Screen
2
MouseGetPos, xpos, ypos
3
4
SysGet, MCount, MonitorCount
5
6
before_monitor := 1 ; 记录上一个显示器
7
8
monitors := {}
9
; 循环所有的显示器 根据显示器的上右下左 获取中心点
10
Loop, %MCount%
11
{
12
    cur := A_Index
13
    SysGet, temp, monitor, %cur%
14
15
    ; MsgBox,显示器%cur% Left: %tempLeft%  -- Top:  %tempTop%  -- Right:  %tempRight%  -- Bottom:  %tempBottom%  -- x: %xpos%  -- y: %ypos%
92 collapsed lines
16
17
    ; 计算对应屏幕的中心点
18
    m_x:= (tempLeft+tempRight)//2
19
    m_y:= (tempTop+tempBottom)//2
20
21
    ; 指定鼠标跳转后所在 显示器的指定位置
22
    if(cur==1){
23
        ; 指定屏幕位置
24
        ; m_x := 1200
25
        ; m_y := 800
26
        ; 根据中心点偏移
27
        ; m_x := m_x+200
28
        ; m_y := m_y-300
29
    }
30
31
    monitors[cur] := {left: tempLeft,top: tempTop,right: tempRight,bottom: tempBottom,x: m_x,y: m_y}
32
}
33
34
35
36
; 显示鼠标当前坐标
37
#!C::
38
    MouseGetPos, xpos, ypos
39
    MsgBox, x: %xpos%  -- y: %ypos%
40
return
41
42
; 鼠标跳转到下一个显示器
43
#!X::
44
    the_next := 1
45
    MouseGetPos, xpos, ypos
46
    ; 循环所有的显示器
47
    Loop, %MCount%
48
    {
49
        cur := A_Index
50
51
        ;   monitors[cur] := {left: tempLeft,top: tempTop,right: tempRight,bottom: tempBottom,x: m_x,y: m_y}
52
53
        ; 判断当前鼠标所在的显示器
54
        if(xpos >= monitors[cur].Left)
55
        and (xpos <= monitors[cur].Right)
56
        and (ypos >= monitors[cur].Top)
57
        and (ypos <= monitors[cur].Bottom)
58
        {
59
            ; MsgBox, 改变前 显示器%cur%
60
            before_monitor := cur
61
            if(MCount= cur){
62
                the_next := 1
63
            }
64
            else{
65
                the_next += 1
66
            }
67
        }
68
    }
69
70
    ; MsgBox %the_next%
71
    MouseMove, monitors[the_next].x, monitors[the_next].y, 0
72
73
    ; 激活窗口
74
    MouseGetPos, mouseX, mouseY, winId
75
    WinActivate, ahk_id %winId%
76
    WinWaitActive, ahk_id %winId% ; 等待窗口激活
77
78
return
79
80
; 鼠标跳转到上一个显示器
81
#!Z::
82
    the_prev := before_monitor
83
    MouseGetPos, xpos, ypos
84
    ; 循环所有的显示器
85
    Loop, %MCount%
86
    {
87
        cur := A_Index
88
        ; 判断当前鼠标所在的显示器
89
        if(xpos >= monitors[cur].Left)
90
        and (xpos <= monitors[cur].Right)
91
        and (ypos >= monitors[cur].Top)
92
        and (ypos <= monitors[cur].Bottom)
93
        {
94
            ; MsgBox, 改变前 显示器%cur%
95
            before_monitor := cur
96
        }
97
    }
98
99
    ; MsgBox %the_prev%
100
    MouseMove, monitors[the_prev].x, monitors[the_prev].y, 0
101
102
    ; 激活窗口
103
    MouseGetPos, mouseX, mouseY, winId
104
    WinActivate, ahk_id %winId%
105
    WinWaitActive, ahk_id %winId% ; 等待窗口激活
106
107
return

github

懒得修改,想直接用的话可以到github下载exe文件:
https://github.com/xloong/multi_screen_mouse_jump

参考

AutoHotkey下载
AutoHotkey配置鼠标光标在屏幕之间瞬移
SysGet  
MouseMove
WinActivate

本文标题:多屏幕鼠标一键跳转显示屏 AHK脚本
文章作者:夜火/xloong
发布时间:2023-09-19
Copyright 2026
站点地图