【发布时间】:2019-11-02 17:16:23
【问题描述】:
我正在制作一个 AI,并希望它能够充当玩家。我使用一些代码来点击鼠标,这很有效。然后我编写了一些代码让鼠标点击随机位置,但它停止工作。
我变了
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
到
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
但它似乎没有改变任何东西,除了它按下的位置。
我还让它在 mouseDown 和 mouseUp 之间等待 0.5 秒,但是当我运行 rndPlay(); 时它仍然不起作用,当我尝试只做 press1(); 时它确实起作用了。
public static void MoveTo(int x, int y)
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
}
public static void LeftClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y, 0, 0);
}
public static void LeftMouseClick(int x, int y)
{
MoveTo(x, y);
LeftClick();
}
public static void press1()
{
LeftMouseClick(28000, 25000);
last2Moves("1");
}
public static void press2()
{
LeftMouseClick(33000, 25000);
last2Moves("2");
}
//etc
public static void rndPlay()
{
for (int r = 0; moveSucces != true && r < 10000; r++)
{
Random rndP = new Random();
int rndPress = rndP.Next(1, 10);
if (rndPress == 1)
{
press1();
}
if (rndPress == 2)
{
press2();
}
//etc
}
}
当我运行rndPlay(); 时,我看到鼠标在移动,但似乎没有点击。是否有另一种方法可以让鼠标点击的位置更好/更快?
【问题讨论】:
-
考虑
WM_MOUSEMOVE,lParam 参数决定了鼠标的位置。低位字指定 x 坐标,高位字指定光标的 y 坐标。对 x 使用 16 位,对 y 使用 16 位意味着它无法处理大于 32767 的坐标。 -
你的鼠标坐标好像很大。
-
@Phil1970 当指定
MOUSEEVENTF_ABSOLUTE时,鼠标相对位置以缇表示,在标准化的、设备无关的(65535,65535)坐标系中。读取/写入函数时需要转换/标准化标准屏幕坐标,计算 TwipsPerPixelX/Y 值。人们可能想改用SendInput。mouse_event长期处于deprecated状态。