Skip to content

生成鼠标移动轨迹 - GenerateMouseTrajectory

函数简介

根据当前 真实鼠标轨迹 配置,生成从起点到终点的轨迹点数组,供二次开发预览、分析或自行按点回放。

MoveTo / MoveToEx 的关系:上述移动接口在 EnableRealMouse 开启且移动距离 ≥ MinMouseTrajectory 时,内部使用与 GenerateMouseTrajectory 同一套算法 逐步移动鼠标。本接口只 生成数据、不移动鼠标

轨迹形态由 SetConfig / SetConfigByKey 中的配置项控制,详见下文 轨迹参数配置

返回数据结构(JSON 数组,每个元素为一个轨迹点):

json
[
  {"x": 108, "y": 105, "deltaX": 8, "deltaY": 5, "time": 7},
  {"x": 116, "y": 110, "deltaX": 8, "deltaY": 5, "time": 8}
]
字段名说明
x该点绝对 X 坐标。
y该点绝对 Y 坐标。
deltaX相对上一点的 X 轴偏移(像素)。
deltaY相对上一点的 Y 轴偏移(像素)。
time到达该点的等待时间(毫秒)。

接口名称

GenerateMouseTrajectory

DLL调用

long GenerateMouseTrajectory(long ola, int startX, int startY, int endX, int endY);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
startX整数型起点X坐标。
startY整数型起点Y坐标。
endX整数型终点X坐标。
endY整数型终点Y坐标。

轨迹参数配置

以下配置项通过 SetConfig 批量设置,或通过 SetConfigByKey 逐项设置。完整字段说明见 SetConfig 文档 真实鼠标轨迹 小节。

配置项类型默认值说明
EnableRealMousebooltrue是否启用真实鼠标轨迹。MoveTo / MoveToEx 是否走轨迹也受此开关控制。
RealMouseMode整数6轨迹预设模式,见下表。设为 6(自定义) 时,下方 RealMouse* 参数才生效。
MinMouseTrajectory整数50最小轨迹距离(像素)。移动距离 小于 该值时不生成轨迹,直接定位。
RealMouseBaseTimePer100Pixels整数100每移动 100 像素的基础耗时(毫秒),越大移动越慢。
RealMouseFlowFlag整数161速度曲线组合标志,按位 OR 叠加,见下表。
RealMouseNoise双精度浮点数5.0噪声强度,越大路径抖动越明显。
RealMouseDeviation整数5路径偏差,越大曲线弯曲程度越高。
RealMouseMinSteps整数300最小步数,控制轨迹点数量下限。
RealMouseTimeToSteps双精度浮点数2.5步间时间系数,影响各点间隔。
RealMouseOvershoots整数0过冲点数,>0 时终点附近会有轻微 overshoot,更拟人。
MouseDriftCheckTime整数0漂移检测间隔(毫秒),0=关闭。仅影响 MoveTo 实际移动,不影响本接口生成的点列。

RealMouseMode 预设

名称特点
1标准计算机用户日常办公风格,速度与曲率适中。
2游戏玩家移动更快、路径更直接。
3非光学鼠标(慢)移动较慢,适合模拟老旧鼠标。
4平衡版本速度与拟真度折中。
5机器人模式接近直线,几乎无弯曲。
6自定义模式使用下方 RealMouseNoiseRealMouseDeviation 等参数自行调节。

RealMouseFlowFlag 标志位

将需要的标志 相加 后写入 RealMouseFlowFlag(默认 161 = 1 + 32 + 128):

效果
1变化速度曲线
2中断移动
4另一种中断模式
8慢启动
16另一种慢启动
32锯齿状移动
64停止移动
128调整移动
256随机移动
512恒定速度

如何设置轨迹参数

方式一:SetConfig 批量设置(推荐)

csharp
// 使用预设模式 1(标准计算机用户),并启用轨迹
ola.SetConfig(@"{
    ""EnableRealMouse"": true,
    ""RealMouseMode"": 1
}");
python
# 自定义模式:调慢速度、增加弯曲
ola.SetConfig("""{
    "EnableRealMouse": true,
    "RealMouseMode": 6,
    "RealMouseBaseTimePer100Pixels": 150,
    "RealMouseNoise": 6.0,
    "RealMouseDeviation": 8,
    "MinMouseTrajectory": 30
}""")

方式二:SetConfigByKey 逐项设置

csharp
ola.SetConfigByKey("RealMouseMode", "2");           // 切换为游戏玩家模式
ola.SetConfigByKey("RealMouseNoise", "3.5");        // 降低噪声
ola.SetConfigByKey("MinMouseTrajectory", "80");     // 80 像素以内直接定位

设置完成后,调用 GenerateMouseTrajectory 即可按新参数生成轨迹;MoveTo / MoveToEx 也会自动使用相同配置。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 从 (100, 100) 到 (800, 600) 生成轨迹(参数取自当前 SetConfig)
auto result = ola.GenerateMouseTrajectory(100, 100, 800, 600);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 从 (100, 100) 到 (800, 600) 生成轨迹
var result = ola.GenerateMouseTrajectory(100, 100, 800, 600);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
result = ola.GenerateMouseTrajectory(100, 100, 800, 600)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
// 从 (100, 100) 到 (800, 600) 生成轨迹
var result = ola.GenerateMouseTrajectory(100, 100, 800, 600);
cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.GenerateMouseTrajectory(100, 100, 800, 600)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
result = ola.GenerateMouseTrajectory(100, 100, 800, 600)
text
.局部变量 ola, OLAPlug
ola.创建 ()
result = ola.GenerateMouseTrajectory(100, 100, 800, 600)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 从 (100, 100) 到 (800, 600) 生成轨迹
var result = ola.GenerateMouseTrajectory(100, 100, 800, 600);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
自动 result = ola.GenerateMouseTrajectory(100, 100, 800, 600)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 从 (100, 100) 到 (800, 600) 生成轨迹(参数取自当前 SetConfig)
auto result = ola.GenerateMouseTrajectory(100, 100, 800, 600);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = GenerateMouseTrajectory(instance, 100, 100, 800, 600);
if (ptr != 0) {
    char buffer[512] = {0};
    GetStringFromPtr(ptr, buffer, sizeof(buffer));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long GenerateMouseTrajectory(long ola, int startX, int startY, int endX, int endY);

long instance = CreateCOLAPlugInterFace();
long ptr = GenerateMouseTrajectory(instance, 100, 100, 800, 600);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string result = sb.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.GenerateMouseTrajectory(instance, 100, 100, 800, 600)
if ptr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(ptr, buf, 512)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

回放轨迹数据

生成结果为 JSON 字符串(SDK 直接返回字符串;原生 DLL 需先用 GetStringFromPtr 读取)。解析后按点逐步移动即可模拟完整轨迹。

回放要点

  1. 先关闭内置轨迹:回放时若仍开启 EnableRealMouse,每步 MoveTo 会再次生成轨迹,导致路径变形。建议回放前 SetConfigByKey("EnableRealMouse", "False"),或逐步使用 MoveToWithoutSimulator / MoveR
  2. time 字段延时:相邻两点之间调用 Delay 等待对应毫秒数,还原移动节奏。
  3. 两种回放方式
    • 绝对坐标:用每点的 xy 定位(MoveToWithoutSimulator)。
    • 相对偏移:用每点的 deltaXdeltaY 逐步偏移(MoveR),适合起点已是当前光标位置时。
python
import json
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
ola.SetConfigByKey("EnableRealMouse", "False")  # 回放时关闭内置轨迹

json_str = ola.GenerateMouseTrajectory(100, 100, 800, 600)
points = json.loads(json_str)

# 方式一:绝对坐标 + MoveToWithoutSimulator
for pt in points:
    ola.MoveToWithoutSimulator(pt["x"], pt["y"])
    ola.Delay(pt["time"])

# 方式二:相对偏移 + MoveR(调用前光标应在起点 100,100)
# for pt in points:
#     ola.MoveR(pt["deltaX"], pt["deltaY"])
#     ola.Delay(pt["time"])
csharp
using System.Text.Json;
using OLAPlug;

var ola = new OLAPlugServer();
ola.SetConfigByKey("EnableRealMouse", "False");

var json = ola.GenerateMouseTrajectory(100, 100, 800, 600);
using var doc = JsonDocument.Parse(json);

foreach (var pt in doc.RootElement.EnumerateArray())
{
    ola.MoveToWithoutSimulator(pt.GetProperty("x").GetInt32(), pt.GetProperty("y").GetInt32());
    ola.Delay(pt.GetProperty("time").GetInt32());
}
cpp
#include "OLAPlugServer.h"
#include <nlohmann/json.hpp>  // 或任意 JSON 库

OLAPlugServer ola;
ola.SetConfigByKey("EnableRealMouse", "False");

auto jsonStr = ola.GenerateMouseTrajectory(100, 100, 800, 600);
auto points = nlohmann::json::parse(jsonStr);

for (const auto& pt : points) {
    ola.MoveToWithoutSimulator(pt["x"], pt["y"]);
    ola.Delay(pt["time"]);
}
python
import json
from ctypes import CDLL, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()

ptr = ola.GenerateMouseTrajectory(instance, 100, 100, 800, 600)
buf = create_string_buffer(ola.GetStringSize(ptr) + 1)
ola.GetStringFromPtr(ptr, buf, len(buf))
ola.FreeStringPtr(ptr)
points = json.loads(buf.value.decode("utf-8"))

ola.SetConfigByKey(instance, "EnableRealMouse", "False")
for pt in points:
    ola.MoveToWithoutSimulator(instance, pt["x"], pt["y"])
    ola.Delay(instance, pt["time"])

若只需 直接移动、不关心中间路径,可跳过本接口,直接调用 MoveTo(内置轨迹)或 MoveToWithoutSimulator(瞬移)。

返回值

返回值说明
(返回值)字符串指针地址,包含JSON格式的轨迹点数组数据。返回 0 表示失败。

注意事项

项目说明
释放内存DLL调用返回字符串指针地址,需要调用 FreeStringPtr 接口释放内存。
生成结果依赖 调用时刻 的 SetCon生成结果依赖 调用时刻 的 SetConfig 轨迹配置;修改配置后需重新调用本接口才能看到新轨迹。
EnableRealMouse 为 faEnableRealMouse 为 false,或起终点距离 < MinMouseTrajectoryMoveTo 不会走轨迹;此时本接口仍可按当前配置生成点列,供对比或自定义回放。
直接移动、不走轨迹需要 直接移动、不走轨迹 时,请使用 MoveToWithoutSimulator,或将 EnableRealMouse 设为 false。