autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本

autohotkey 热键

Introduction

介绍

In an interesting question here at Experts Exchange, a member requested this:

在Experts Exchange的一个有趣的问题中 ,一位成员提出了以下要求:

Code for using Windows AutoHotkey to immediately move mouse pointer to particular monitor

使用Windows AutoHotkey立即将鼠标指针移动到特定监视器的代码

The member has "...several users who have 8 or more monitors connected to their Windows 10 computers." The requirement is to write an AutoHotkey script that defines a series of keyboard shortcuts (aka "hotkeys") to move the mouse pointer to each monitor. In particular:

该成员具有“ ...将8个或8个以上监视器连接到其Windows 10计算机的几个用户。” 要求是编写一个AutoHotkey脚本,该脚本定义一系列键盘快捷键(也称为“热键”),以将鼠标指针移至每个监视器。 特别是:

Alt+Ctrl+1 ==> move mouse to monitor 1

Alt + Ctrl + 1 ==>将鼠标移至监视器1

Alt+Ctrl+2 ==> move mouse to monitor 2

Alt + Ctrl + 2 ==>将鼠标移至监视器2

Alt+Ctrl+3 ==> move mouse to monitor 3

Alt + Ctrl + 3 ==>将鼠标移至显示器3

Alt+Ctrl+4 ==> move mouse to monitor 4

Alt + Ctrl + 4 ==>将鼠标移至监视器4

Alt+Ctrl+5 ==> move mouse to monitor 5

Alt + Ctrl + 5 ==>将鼠标移至监视器5

Alt+Ctrl+6 ==> move mouse to monitor 6

Alt + Ctrl + 6 ==>将鼠标移至监视器6

Alt+Ctrl+7 ==> move mouse to monitor 7

Alt + Ctrl + 7 ==>将鼠标移动到显示器7

Alt+Ctrl+8 ==> move mouse to monitor 8

Alt + Ctrl + 8 ==>将鼠标移动到显示器8

Alt+Ctrl+9 ==> move mouse to monitor 9

Alt + Ctrl + 9 ==>将鼠标移至监视器9

I wrote a no-frills script (Version 1) and posted it at the EE thread. After that, however, I decided to write this EE article with an enhanced script (Version 2).

Version 1 Features

我编写了简洁的脚本(版本1)并将其发布在EE线程上 。 但是,在那之后,我决定用增强的脚本(第2版)来撰写此EE文章。

版本1功能

The following features are in V1:

V1中具有以下功能:

• Defines the nine hotkeys shown above.

•定义上面显示的九个热键。

• Defines Alt+Ctrl+0 (zero) as a hotkey to exit the script.

•将Alt + Ctrl + 0 (零)定义为热键以退出脚本。

• Allows other "modifier" keys to be used for the hotkeys rather than hard-coding Alt+Ctrl ("!^"), as follows:

•允许将其他“修饰符”键用于热键,而不是硬编码Alt + Ctrl (“ !^ ”),如下所示:

! (exclamation mark) ==> (感叹号)==> Alt Alt ^ (caret, circumflex, hat) ==> ^ (插入符号,抑扬符,帽子)==> Ctrl Ctrl + (plus sign) ==> + (加号)==> Shift Shift # (hash mark, pound sign, octothorpe) ==> (井号,井号,八叉戟)==> Win (Windows logo key) Win (Windows徽标键)

You may use any number of those modifiers, and they may be in any order, that is, "!^" (Alt+Ctrl) behaves the same as "^!" (Ctrl+Alt).

您可以使用任意数量的这些修饰符,并且它们可以按任何顺序排列,即,“ !^ ”( Alt + Ctrl )的行为与“ ^! ”( Ctrl + Alt )相同。

• Allows horizontal (X) and vertical (Y) offsets to be specified. That is, the mouse pointer will be moved to the specified number of pixels (OffsetX) from the left edge of the monitor and the specified number of pixels (OffsetY) from the top edge of the monitor.

•允许指定水平( X )和垂直( Y )偏移量。 也就是说,鼠标指针将从显示器的左边缘移动到指定的像素数( OffsetX ),从显示器的顶部边缘移动到指定的像素数( OffsetY )。

• The script does not have a system tray (notification area) icon. I did this intentionally, as the default AutoHotkey context menu (exposed via a right-click on the system tray icon, and containing Exit) is likely to be confusing for many users.

•该脚本没有系统托盘(通知区域)图标。 我故意这样做,因为默认的AutoHotkey上下文菜单(通过在系统任务栏图标上单击鼠标右键显示,并且包含Exit )可能会使许多用户感到困惑。

Article Update 31-Dec-2019: In private correspondence, an EE member asked me to enhance the script with an option to start it automatically when Windows starts. I did this and replaced the attached V2 script with the V3 one. Note that V3 is backward compatible with V2, as the V3 default is not to start with Windows (same as V2). Also, I modified all the article sections below to contain V3 content rather than V2.

Version 3 Features

文章更新2019年12月31日:在私人通信中,一位EE成员要求我增强脚本,并带有一个选项,可以在Windows启动时自动启动该脚本。 我这样做了,并用V3替换了随附的V2脚本。 请注意,V3与V2向后兼容,因为V3的默认值不是从Windows(与V2相同)启动。 另外,我修改了以下所有文章部分,以包含V3内容而不是V2。

版本3的功能

V3 has all the features documented above in V1, except for two modifications. First, it displays an icon in the system tray (V1 does not). The default is a monitor icon, but it can be changed to a mouse icon (see below). The icons look like this (may vary depending on the version of Windows, as it uses icons from a built-in Windows file):

V3具有V1上面记录的所有功能,但有两个修改。 首先,它在系统托盘中显示一个图标(V1则不)。 默认值为监视器图标,但可以将其更改为鼠标图标(见下文)。 图标如下所示(由于Windows版本使用内置Windows文件中的图标,因此可能会有所不同):

Second, V3 changes the meaning of the Alt+Ctrl+0 hotkey (of course, you may use modifiers other than Alt+Ctrl). In V1, it exits the script, which is a necessary feature because there is no tray icon with a context menu containing an Exit choice. In V3, Alt+Ctrl+0 moves the mouse to the Primary monitor, regardless of what monitor number the Primary monitor is (exiting the script is done differently in V3, discussed below).

其次,V3更改了Alt + Ctrl + 0热键的含义(当然,您可以使用Alt + Ctrl以外的修饰符)。 在V1中,它退出脚本,这是必需的功能,因为没有带有包含“ 退出”选项的上下文菜单的任务栏图标。 在V3中,无论主监视器是哪个监视器编号, Alt + Ctrl + 0都会将鼠标移到“主监视器” (退出脚本在V3中的处理方式有所不同,如下所述)。

In addition to those two changes, V3 has the following new features:

除了这两项更改之外,V3还具有以下新功能:

• If OffsetX is set to -1, it moves the mouse to the horizontal center of the monitor. Otherwise, it acts the same as in V1, i.e., it is the pixel offset from the left edge of the monitor.

•如果将OffsetX设置为-1 ,它将鼠标移至显示器的水平中心。 否则,其作用与V1中相同,即,它是距监视器左边缘的像素偏移。

• If OffsetY is set to -1, it moves the mouse to the vertical center of the monitor. Otherwise, it acts the same as in V1, i.e., it is the pixel offset from the top edge of the monitor.

•如果OffsetY设置为-1 ,它将鼠标移动到显示器的垂直中心。 否则,它的作用与V1中相同,即,它是距监视器顶部边缘的像素偏移。

• Displays a tooltip when hovering on the system tray icon. The tooltip gives brief usage instructions, like this:

•悬停在系统任务栏图标上时显示工具提示 。 工具提示提供了简短的用法说明,如下所示:

• Displays this context menu when right-clicking on the system tray icon:

•右键单击系统任务栏图标时显示此上下文菜单

autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本_第1张图片

• Selecting the Show Primary Monitor Number menu item displays this dialog:

•选择“ 显示主监视器编号”菜单项将显示以下对话框:

autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本_第2张图片

• Selecting the Show Monitor Names menu item displays this dialog:

•选择“ 显示监视器名称”菜单项将显示以下对话框:

autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本_第3张图片

• Selecting the Change Tray Icon menu item changes the icon to a mouse if it is currently a monitor; to a monitor if it is currently a mouse. The default icon is the monitor (but the default can be easily changed to the mouse in the AutoHotkey source code).

•如果选择“ 更改任务栏图标”菜单项,则该图标将变为鼠标(如果它当前是监视器)。 显示器(如果当前是鼠标)。 默认图标是监视器(但是可以在AutoHotkey源代码中将鼠标轻松更改为默认图标)。

• Selecting the Start with Windows (On/Off toggle) menu item toggles that setting, i.e., if it is Off (the default), it turns On; if it is On, it turns Off. When it is On, a checkmark appears next to it in the context menu:

•选择与Windows中的开始(开/关切换)菜单项切换该设置,也就是说,如果是 (默认设置),它打开 ; 如果打开 ,则关闭 。 当它为On时 ,上下文菜单中的它旁边会出现一个复选标记:

autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本_第4张图片

The method used to start the script with Windows is to create a shortcut (a .LNK file) and put it in the Startup folder (the one for the logged in user, not the Public one). The icon for it in the Startup folder looks like this:

在Windows上启动脚本的方法是创建一个快捷方式( .LNK文件)并将其放在“ 启动”文件夹中(用于登录用户的文件夹,而不是“公用”文件夹)。 Startup文件夹中的图标如下所示:

Turning this off deletes the shortcut; turning it on creates the shortcut.

关闭此功能将删除快捷方式; 打开它会创建快捷方式。

• Selecting the Exit menu item exits (terminates/quits) the script, thereby removing the icon from the system tray and undefining all the hotkeys. To make sure that this wasn't selected by accident, it displays this dialog, with No as the default button (so that an accidental Enter key won't cause an unwanted exit):

•选择“ 退出”菜单项可退出(终止/退出)脚本,从而从系统任务栏中删除该图标并取消定义所有热键。 为了确保不是偶然选择的,它将显示该对话框,默认按钮为No (这样,意外的Enter键不会导致意外退出):

autohotkey 热键_键盘快捷方式(热键),用于将鼠标移至多显示器配置中的显示器-AutoHotkey脚本_第5张图片

Version 3 Script


版本3脚本

As noted at the top of this article, the EE member specifically asked for an AutoHotkey solution. If other readers of this article are not familiar with AutoHotkey, my EE article will get you going on it:

如本文顶部所述,EE成员专门要求提供AutoHotkey解决方案。 如果本文的其他读者不熟悉AutoHotkey,我的EE文章将帮助您继续进行下去:

AutoHotkey - Getting Started AutoHotkey-入门

Here is the script in a code block (it is also attached as a file at the end of the article for easy downloading):

这是代码块中的脚本(为了方便下载,该脚本也作为文件附在本文的结尾):

; Joe Winograd 31-Dec-2019 Version 3
#Warn,UseUnsetLocal ; warning on uninitialized variables
#NoEnv ; avoid checking empty variables to see if they are environment variables
#SingleInstance Force ; replace old instance immediately
SetBatchLines,-1 ; run at maximum speed

Gosub,InitializeVars ; initialize all variables
Gosub,ConfigureInitialTray ; configure initial system tray (notification area)

CoordMode,Mouse,Screen ; coordinates relative to entire desktop
SysGet,NumMons,MonitorCount ; get number of monitors
Hotkey,%HotkeyModifiers%0,MoveMousePrimary,On ; define Modifiers+0 hotkey to move mouse to Primary monitor regardless of its monitor number
Loop,%NumMons% ; process all monitors
{
  MonNum:=A_Index
  SysGet,Coordinates%MonNum%,Monitor,%MonNum% ; get coordinates for this monitor
  Left:=Coordinates%MonNum%Left
  Right:=Coordinates%MonNum%Right
  Top:=Coordinates%MonNum%Top
  Bottom:=Coordinates%MonNum%Bottom
  If (OffsetX=-1)
    MoveX:=Floor(0.5*(Right-Left)) ; center
  Else
    MoveX:=OffsetX
  If (OffsetY=-1)
    MoveY:=Floor(0.5*(Bottom-Top)) ; center
  Else
    MoveY:=OffsetY
  MoveToX[MonNum]:=Left+MoveX ; store mouse-move X coordinate for this monitor
  MoveToY[MonNum]:=Top+MoveY ; store mouse-move Y coordinate for this monitor
  Hotkey,%HotkeyModifiers%%MonNum%,MoveMouseMon,On ; define Modifiers+N hotkey
}
Return

InitializeVars:
TrayIconFile:=A_WinDir . "\System32\setupapi.dll" ; get icons from setupapi.dll file
TrayIconNumMon:="-35" ; monitor icon
TrayIconNumMou:="-2" ; mouse icon

; *** begin variables to change ***
TrayIconNum:=TrayIconNumMon ; default icon - TrayIconNumMon=monitor or TrayIconNumMou=mouse
OffsetX:=-1 ; move mouse to this number of pixels from left edge of monitor (-1 means center)
OffsetY:=-1 ; move mouse to this number of pixels from top edge of monitor (-1 means center)
; modifier keys:  ! is Alt   ^ is Ctrl   + is Shift   # is Win (the Windows logo key)
HotkeyModifiers:="!^" ; Alt+Ctrl - these are the modifier keys for the number keys (0-9)
; *** end variables to change ***

SplitPath,A_ScriptName,,,,ProgramName ; get name of script without path or extension
NumModifiers:=StrLen(HotkeyModifiers) ; get number of modifier keys
MoveToX:={} ; declare array for mouse-move X coordinates
MoveToY:={} ; declare array for mouse-move Y coordinates
Return

ConfigureInitialTray:
Menu,Tray,NoStandard ; do not use standard AutoHotkey context menu
Menu,Tray,Add,Show &Primary Monitor Number,ContextMenu
Menu,Tray,Add,Show Monitor &Names,ContextMenu
Menu,Tray,Add,Change Tray &Icon,ContextMenu
Menu,Tray,Add,Start with &Windows (On/Off toggle),ContextMenu
StartupLink:=A_Startup . "\" . ProgramName . ".lnk"
If (FileExist(StartupLink))
  Menu,Tray,Check,Start with &Windows (On/Off toggle)
Else
  Menu,Tray,Uncheck,Start with &Windows (On/Off toggle)
Menu,Tray,Add,E&xit,ContextMenu
Menu,Tray,Default,Show &Primary Monitor Number
HotkeyModifiersTip:=StrReplace(HotkeyModifiers,"+","Shift+") ; do this first so we don't catch the other plus signs
HotkeyModifiersTip:=StrReplace(HotkeyModifiersTip,"!","Alt+")
HotkeyModifiersTip:=StrReplace(HotkeyModifiersTip,"^","Ctrl+")
HotkeyModifiersTip:=StrReplace(HotkeyModifiersTip,"#","Win+")
TrayTip:=HotkeyModifiersTip . "N to move mouse to monitor N (1-9)`n" . HotkeyModifiersTip . "0 to move mouse to Primary monitor`nRight-click for context menu"
Menu,Tray,Tip,%TrayTip%
Menu,Tray,Icon,%TrayIconFile%,%TrayIconNum%
Return

ContextMenu:
If (A_ThisMenuItem="Show &Primary Monitor Number")
{
  SysGet,PrimaryMonNum,MonitorPrimary ; get number of Primary monitor
  MsgBox,4160,%ProgramName%,Primary monitor number is %PrimaryMonNum%
  Return
}
If (A_ThisMenuItem="Show Monitor &Names")
{
  MonNames:=""
  Loop,%NumMons% ; process all monitors
  {
    MonNum:=A_Index
    SysGet,MonName,MonitorName,%MonNum% ; get name of this monitor
    MonNames:=MonNames . MonNum . "=" . MonName . "`n"
  }
  MsgBox,4160,%ProgramName%,Monitor names are:`n%MonNames%
  Return
}
If (A_ThisMenuItem="Change Tray &Icon")
{
  If (TrayIconNum=TrayIconNumMou)
    TrayIconNum:=TrayIconNumMon
  Else
    TrayIconNum:=TrayIconNumMou
  Menu,Tray,Icon,%TrayIconFile%,%TrayIconNum%
  Return
}
If (A_ThisMenuItem="Start with &Windows (On/Off toggle)")
{
  If (FileExist(StartupLink))
  {
    ; it's on, so this click turns it off
    Menu,Tray,Uncheck,Start with &Windows (On/Off toggle)
    FileDelete,%StartupLink%
    Return
  }
  Else
  {
    ; it's off, so this click turns it on
    Menu,Tray,Check,Start with &Windows (On/Off toggle)
    FileCreateShortcut,%A_ScriptFullPath%,%StartupLink%,%A_ScriptDir%,,%ProgramName%,%TrayIconFile% ; in a DLL, only first icon is valid (in this case, it's a monitor)
    {
      If (ErrorLevel!=0)
      {
        MsgBox,4112,Fatal Error,Error Level=%ErrorLevel% trying to create Startup shortcut:`n%StartupLink%
        ExitApp
      }
    }
    Return
  }
}
If (A_ThisMenuItem="E&xit")
{
  MsgBox,4388,%ProgramName% - Terminate?,Are you sure you want to quit and deactivate all hotkeys?
  IfMsgBox,No
    Return
  ExitApp
}

MoveMouseMon:
MonNum:=SubStr(A_ThisHotkey,NumModifiers+1,1) ; monitor number is after the modifiers
X:=MoveToX[MonNum] ; retrieve horizontal mouse-move coordinate
Y:=MoveToY[MonNum] ; retrieve vertical mouse-move coordinate
MouseMove,%X%,%Y% ; move it!
Return

MoveMousePrimary:
SysGet,PrimaryMonNum,MonitorPrimary ; get number of Primary monitor
X:=MoveToX[PrimaryMonNum] ; retrieve horizontal mouse-move coordinate
Y:=MoveToY[PrimaryMonNum] ; retrieve vertical mouse-move coordinate
MouseMove,%X%,%Y% ; move it!
Return 

My hope is that the descriptive variable names along with the extensive comments in the script provide enough documentation for readers to modify it, but if you have any questions, post them here, and I'll try to assist.

我希望脚本中的描述性变量名称以及广泛的注释可以为读者提供足够的文档来对其进行修改,但是如果您有任何疑问,请将其张贴在此处,我将尽力提供帮助。

The script should work in all versions of Windows from XP through W10, both 32-bit and 64-bit, with up to nine monitors. I tested it on W7/64-bit with three monitors and W10/64-bit with two monitors — worked perfectly! If you use it on other configurations — a different version of Windows and/or a different number of monitors — I'll really appreciate it if you post your results as a comment under the article.

该脚本应在从XP到W10的所有Windows版本(32位和64位)中均可使用,最多可使用9个监视器。 我在带有三个监视器的W7 / 64位和带有两个监视器的W10 / 64位上进行了测试—完美! 如果您在其他配置(不同版本的Windows和/或不同数量的监视器)上使用它,如果将您的结果作为评论发表在本文下面,我将不胜感激。

If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

如果您发现本文有帮助,请单击下面的大拇指图标。 这使我知道什么对EE成员有价值,并为以后的文章提供了指导。 非常感谢! 问候乔

MoveMouseToMonitor.ahk

MoveMouseToMonitor.ahk

翻译自: https://www.experts-exchange.com/articles/33932/Keyboard-shortcuts-hotkeys-to-move-mouse-to-monitors-in-a-multi-monitor-configuration-AutoHotkey-Script.html

autohotkey 热键

你可能感兴趣的:(python,java,windows,linux,mac)