AutoHotkey 语言 通过 AutoHotkey 创建自定义鼠标热键

AutoHotkey阿木 发布于 2025-06-11 36 次阅读


AutoHotkey Language: Creating Custom Mouse Hotkeys

AutoHotkey (AHK) is a scripting language designed for automating the Windows GUI and general scripting. It is particularly useful for creating custom hotkeys that can trigger various actions, including mouse movements and clicks. In this article, we will delve into the world of AutoHotkey and explore how to create custom mouse hotkeys. We will cover the basics of the language, the syntax for defining hotkeys, and some advanced techniques to enhance your mouse automation.

Introduction to AutoHotkey

AutoHotkey is a powerful tool for Windows users who want to automate repetitive tasks or create custom keyboard and mouse shortcuts. It allows you to write scripts that can be executed in the background, without the need for a user interface. This makes it ideal for automating tasks such as file management, web browsing, and even gaming.

Basic Syntax

Before we dive into creating custom mouse hotkeys, let's briefly go over the basic syntax of AutoHotkey. An AutoHotkey script consists of lines of code that are executed sequentially. Each line can be a command or a label. Here's a simple example:

ahk
MsgBox, Hello, World!

This script will display a message box with the text "Hello, World!" when executed.

Creating a Custom Mouse Hotkey

To create a custom mouse hotkey, you need to define a hotkey using the `^` (Ctrl), `!` (Alt), `` (Win), or `+` (Shift) modifiers, followed by a key or a combination of keys. For mouse hotkeys, we use the `LButton`, `RButton`, `MButton`, `WheelUp`, `WheelDown`, and other mouse-related keywords.

Here's an example of a simple script that creates a hotkey that moves the mouse cursor to the top-left corner of the screen when the Ctrl+Alt+M combination is pressed:

ahk
^!m:: ; Ctrl+Alt+M hotkey
CoordMode, Mouse, Screen ; Set the coordinate mode to screen
MouseGetPos, , , WinUnderMouse ; Get the position of the mouse cursor
WinGetPos, WinX, WinY, WinWidth, WinHeight, ahk_class AutoHotkey ; Get the position and size of the active window
MouseMove, 0, 0 ; Move the mouse cursor to the top-left corner of the screen
return

In this script, `CoordMode, Mouse, Screen` sets the coordinate mode to screen, which means the coordinates are relative to the entire screen. `MouseGetPos` retrieves the current position of the mouse cursor, and `WinGetPos` retrieves the position and size of the active window. Finally, `MouseMove` moves the mouse cursor to the top-left corner of the screen.

Advanced Mouse Hotkey Techniques

Now that you have a basic understanding of how to create a custom mouse hotkey, let's explore some advanced techniques to enhance your mouse automation.

Dynamic Coordinates

Sometimes, you may want to move the mouse to a dynamic position based on the current cursor position or window size. Here's an example that moves the mouse 100 pixels to the right and 50 pixels down from its current position:

ahk
^!m:: ; Ctrl+Alt+M hotkey
CoordMode, Mouse, Screen ; Set the coordinate mode to screen
MouseGetPos, MX, MY ; Get the current position of the mouse cursor
MouseMove, MX + 100, MY + 50 ; Move the mouse cursor 100 pixels to the right and 50 pixels down
return

Clicking and Double-Clicking

AutoHotkey allows you to simulate mouse clicks and double-clicks using the `Click` and `DoubleClick` functions. Here's an example that simulates a double-click at the current mouse position:

ahk
^!c:: ; Ctrl+Alt+C hotkey
CoordMode, Mouse, Screen ; Set the coordinate mode to screen
MouseGetPos, MX, MY ; Get the current position of the mouse cursor
Click, , , Down ; Simulate a mouse click (left button down)
Sleep, 100 ; Wait for 100 milliseconds
Click, , , Up ; Simulate a mouse click (left button up)
return

Holding the Mouse Button

You can also simulate holding down the mouse button using the `Click, , , Down` and `Click, , , Up` functions. Here's an example that holds down the right mouse button for 2 seconds:

ahk
^!h:: ; Ctrl+Alt+H hotkey
CoordMode, Mouse, Screen ; Set the coordinate mode to screen
MouseGetPos, MX, MY ; Get the current position of the mouse cursor
Click, Right, , Down ; Simulate a right mouse button down
Sleep, 2000 ; Wait for 2 seconds
Click, Right, , Up ; Simulate a right mouse button up
return

Using the Mouse Wheel

AutoHotkey also allows you to simulate scrolling with the mouse wheel. Here's an example that scrolls the mouse wheel up by one unit:

ahk
^!w:: ; Ctrl+Alt+W hotkey
WheelUp ; Simulate a mouse wheel up scroll
return

Conclusion

Creating custom mouse hotkeys in AutoHotkey can greatly enhance your productivity and automate repetitive tasks. By using the basic syntax and advanced techniques covered in this article, you can create scripts that move the mouse, click, double-click, hold buttons, and scroll the mouse wheel. With a little creativity, the possibilities are endless. Happy scripting!