提问人:Kamweti C 提问时间:10/4/2023 最后编辑:Lajos ArpadKamweti C 更新时间:10/4/2023 访问量:34
C# 模拟鼠标输入
C# Simulate mouse inputs
问:
我正在开发一个应用程序,通过模拟鼠标输入来帮助残疾人与 PC 交互。
我已经取得了一些进展,但我在鼠标左拖动方面遇到了挑战。我希望应用程序在选择“鼠标左拖动”选项 2 秒后模拟鼠标左拖动,并在鼠标停止移动 2 秒后停止拖动。以下是我正在使用的一些代码:
public partial class frmClicker : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
private const int MOUSEEVENTF_MIDDLEUP = 0x40;
private const int MOUSEEVENTF_MOVE = 0x01;
private const int MOUSEEVENTF_WHEEL = 0x0800;
public frmClicker()
{
``InitializeComponent();
}
public void DoMouseLeftClick()
{
Thread.Sleep(defaultDelay);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
Thread.Sleep(defaultDelay);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
public void DoMouseLeftDoubleClick()
{
Thread.Sleep(defaultDelay);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
Thread.Sleep(defaultDelay);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Thread.Sleep(50);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
public void DoMouseLeftDrag()
{
Thread.Sleep(defaultDelay);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
Thread.Sleep(defaultDelay);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
Thread.Sleep(dragRelease);
mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
public void DoMouseRightClick()
{
Thread.Sleep(defaultDelay);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
Thread.Sleep(defaultDelay);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
}
public void DoMouseRightDoubleClick()
{
Thread.Sleep(defaultDelay);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
Thread.Sleep(defaultDelay);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
Thread.Sleep(50);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
}
}
答: 暂无答案
评论
mouse_event(*DOWN | *UP ...
mouse_event(*DOWN | *DOWN ...