InputSimulator无法模拟鼠标点击。

3

我正在使用InputSimulator来模拟按键和鼠标点击。到目前为止,我测试过的每个键都可以正常工作,但鼠标按钮却不能。我是这样发送它们的:

private void button2_Click(object sender, EventArgs e) //In this example I am trying to simulate the left mouse button
    {
        System.Threading.Thread.Sleep(2000); 
        InputSimulator.SimulateKeyPress(VirtualKeyCode.LBUTTON);
    }

但是什么都没有发生。我做错了什么吗?
库:InputSimulator

似乎是一个已知问题(或许)[LButton模拟无法工作] (https://inputsimulator.codeplex.com/workitem/13006) - LarsTech
2个回答

7

最新版本的InputSimulator支持鼠标事件。以下是使用方法:

var sim = new InputSimulator();
sim.Mouse.LeftButtonClick();

请注意,二进制下载已过时,因此您需要从源代码构建该库。

我在哪里可以找到最新版本? - Mihai Bratulescu
在此处获取源代码:https://inputsimulator.codeplex.com/SourceControl/latest - yallie
这个项目很难说,因为当前的NuGet包“Windows Input Simulator”是1.0.4.0版本,发布于2013年12月12日。最新的源代码具有0.2.0.0的程序集版本(不是打字错误!)并且日期也是同一天(2013年12月12日)。嗯。 - Julius
实际上,版本0.2.0.0也在NuGet中以“WindowsInput”命名发布于2013年11月27日。如果我错了,请纠正我,但我猜“Windows Input Simulator”实际上是最新的,但“WindowsInput”版本落后(开发者Michael Noonan必须从0.2.0.0跳到1.0.4.0,但我不确定)。 - Julius
1
混乱并没有止息。同一人还有一个名为InputSimulatorPlus的工具。虽然Github上的示例表明主对象是静态调用InputSimulator.DoStuff(),但实际的NuGet包类需要实例化。 - Gichamba

3

我不太了解InputStimulator,但根据这篇文章,你可以使用以下方法模拟鼠标点击:

      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
      public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData,  int dwExtraInfo);

      private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
      private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
      private const int MOUSEEVENTF_LEFTUP = 0x0004;
      private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
      private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
      private const int MOUSEEVENTF_MOVE = 0x0001;
      private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
      private const int MOUSEEVENTF_RIGHTUP = 0x0010;
      private const int MOUSEEVENTF_WHEEL = 0x0800;
      private const int MOUSEEVENTF_XDOWN = 0x0080;
      private const int MOUSEEVENTF_XUP = 0x0100;

      //.................................
      //In your own function:

      int X = 1220;
      int Y = 13;
      mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, X, Y, 0, 0);
      mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

记得添加 using System.Runtime.InteropServices;



滚动条怎么样?你能给我一个水平和垂直滚动的例子吗? - Mihai Bratulescu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接