我使用 SendInput 来模拟鼠标输入,到目前为止我可以成功地模拟左键、右键和中键。但是,我似乎无法正确模拟 xButtons。我正在定义一个输入结构如下:
Win32.INPUT i = new Win32.INPUT();
i.type = Win32.INPUTF.MOUSE;
i.mi.dx = 0;
i.mi.dy = 0;
i.mi.mouseData = 0;
i.mi.time = 0;
i.mi.dwExtraInfo = UIntPtr.Zero;
i.mi.dwFlags = MouseButton(e.Button, true);
Win32.SendInput(1, ref i, System.Runtime.InteropServices.Marshal.SizeOf(new Win32.INPUT()));
MouseButton 定义如下,我相信这就是问题所在。
private Win32.MOUSEEVENTF MouseButton(MouseButtons mb, bool up)
{
switch (mb)
{
case MouseButtons.Left:
return up ? Win32.MOUSEEVENTF.LEFTUP : Win32.MOUSEEVENTF.LEFTDOWN;
case MouseButtons.Right:
return up ? Win32.MOUSEEVENTF.RIGHTUP : Win32.MOUSEEVENTF.RIGHTDOWN;
case MouseButtons.Middle:
return up ? Win32.MOUSEEVENTF.MIDDLEUP : Win32.MOUSEEVENTF.MIDDLEDOWN;
case MouseButtons.XButton1:
return up ? Win32.MOUSEEVENTF.XBUTTON1|Win32.MOUSEEVENTF.XUP : Win32.MOUSEEVENTF.XBUTTON1 | Win32.MOUSEEVENTF.XDOWN;
case MouseButtons.XButton2:
return up ? Win32.MOUSEEVENTF.XBUTTON2 | Win32.MOUSEEVENTF.XUP : Win32.MOUSEEVENTF.XBUTTON2 | Win32.MOUSEEVENTF.XDOWN;
default:
return 0u;
}
}