AutoHotkey 语言 控制鼠标移动轨迹示例

AutoHotkey阿木 发布于 2025-06-12 22 次阅读


AutoHotkey Language: Controlling Mouse Movement Trajectory Example

AutoHotkey (AHK) is a powerful scripting language designed for automating the Windows GUI and general scripting. It is particularly useful for automating repetitive tasks, creating custom hotkeys, and even controlling the mouse and keyboard. In this article, we will delve into the topic of controlling the mouse movement trajectory using AutoHotkey. We will explore the basics of mouse control in AHK, discuss various methods to manipulate the mouse, and provide a comprehensive example to demonstrate how to create a custom script that controls the mouse movement trajectory.

Introduction to AutoHotkey

AutoHotkey is a scripting language that allows users to automate various tasks on their Windows systems. It is often used for creating keyboard shortcuts, automating repetitive tasks, and even controlling the mouse and other system components. AHK scripts are written in plain text and can be executed directly from the command line or by assigning them to hotkeys.

Basics of Mouse Control in AutoHotkey

Before we dive into the details of controlling the mouse movement trajectory, let's first understand the basic functions available for mouse control in AutoHotkey.

Moving the Mouse

To move the mouse to a specific position, you can use the `MouseGetPos` function to get the current position and then use the `MouseMove` function to set a new position. Here's an example:

ahk
; Get the current mouse position
MouseGetPos, x, y

; Move the mouse to the coordinates (100, 100)
MouseMove, 100, 100

Controlling the Mouse Speed

The `MouseMove` function allows you to control the speed of the mouse movement. The `Speed` parameter determines how fast the mouse moves. A higher value will result in faster movement, while a lower value will slow it down. Here's an example:

ahk
; Move the mouse to the coordinates (200, 200) at a speed of 10
MouseMove, 200, 200, 10

Clicking the Mouse

To simulate a mouse click, you can use the `Click` function. This function can be used to perform a single click, double-click, or even a right-click. Here's an example:

ahk
; Perform a single left-click at the current mouse position
Click

; Perform a double-click at the coordinates (300, 300)
Click, 2, 300, 300

; Perform a right-click at the coordinates (400, 400)
Click, Right, 400, 400

Controlling Mouse Movement Trajectory

Now that we have a basic understanding of mouse control in AutoHotkey, let's explore how to control the mouse movement trajectory. The trajectory refers to the path that the mouse follows when moving from one point to another.

Linear Movement

The simplest form of mouse movement is linear, where the mouse moves in a straight line from one point to another. We can achieve this by using the `MouseMove` function with the `Speed` parameter set to a constant value.

ahk
; Move the mouse from (100, 100) to (200, 200) in a straight line
MouseMove, 200, 200, 10

Curved Movement

To create a curved mouse movement trajectory, we can use a series of `MouseMove` commands with varying speeds and positions. This will create a smooth curve between the points.

ahk
; Move the mouse from (100, 100) to (200, 200) in a curved path
MouseMove, 150, 150, 10
MouseMove, 200, 200, 10

Complex Trajectories

For more complex trajectories, we can use a combination of `MouseMove` commands and mathematical functions to calculate intermediate positions. This allows us to create custom paths, such as circles, spirals, or even more intricate shapes.

ahk
; Move the mouse in a circular path with a radius of 100
Loop, 360
{
; Calculate the intermediate position using trigonometry
angle := A_Index 2 A_PI / 360
x := 100 Cos(angle)
y := 100 Sin(angle)

; Move the mouse to the intermediate position
MouseMove, %x%, %y%, 10
}

Example: Custom Mouse Movement Script

In this section, we will create a custom AutoHotkey script that controls the mouse movement trajectory. The script will move the mouse in a figure-eight pattern, which is a combination of two concentric circles.

ahk
; Define the radius of the circles
radius := 100

; Move the mouse in a figure-eight pattern
Loop, 2
{
; Move the mouse in a clockwise circle
Loop, %radius 2 A_PI / 10
{
angle := A_Index 2 A_PI / 10
x := radius Cos(angle)
y := radius Sin(angle)

; Move the mouse to the intermediate position
MouseMove, %x%, %y%, 10
}

; Move the mouse in a counterclockwise circle
Loop, %radius 2 A_PI / 10
{
angle := A_Index 2 A_PI / 10
x := radius Cos(angle)
y := -radius Sin(angle)

; Move the mouse to the intermediate position
MouseMove, %x%, %y%, 10
}
}

This script will create a figure-eight pattern by moving the mouse in a clockwise circle, then a counterclockwise circle, and repeating this process twice to complete the pattern.

Conclusion

In this article, we have explored the topic of controlling the mouse movement trajectory using AutoHotkey. We discussed the basics of mouse control in AHK, various methods to manipulate the mouse, and provided an example script that creates a custom mouse movement trajectory. By understanding these concepts, you can create powerful and versatile scripts to automate your tasks and enhance your productivity on the Windows platform.