一、winform
1.实时获取鼠标位置
public Form1()
{InitializeComponent();InitialTime();
}private void InitialTime()
{// 初始化 Timer 控件var timer = new System.Windows.Forms.Timer();timer.Interval = 100; // 设置为 100 毫秒,即每 0.1 秒更新一次timer.Tick += Timer_Tick; // 绑定 Tick 事件timer.Start(); // 启动 Timer
}// Timer 每次触发时,更新坐标信息
private void Timer_Tick(object sender, EventArgs e)
{// 获取当前鼠标坐标var mousePos = Cursor.Position;// 更新 Label 控件显示坐标label1.Text = $"X: {mousePos.X}, Y: {mousePos.Y}";
}
2.实时监控键盘输入
public Form2()
{InitializeComponent();this.KeyPreview = true; // 确保事件先在 Form 中触发this.KeyDown += MainForm_KeyDown;
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{// 检查是否按下了 ALT 和数字 1 键if (e.Alt && e.KeyCode == Keys.D1){MessageBox.Show("ALT + 1 被按下");}
}
二、操纵鼠标键盘的相关方法
public class MouseContronller
{// 导入 SetCursorPos 函数,用于设置鼠标位置[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern bool SetCursorPos(int x, int y);// 导入 mouse_event 函数,用于模拟鼠标点击[DllImport("user32.dll")]public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);// 导入 keybd_event 函数,用于模拟键盘按键[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);// 键盘事件常量const int KEYEVENTF_KEYDOWN = 0x0000; // 按键按下const int KEYEVENTF_KEYUP = 0x0002; // 按键松开// 鼠标点击的标志const int MOUSEEVENTF_LEFTDOWN = 0x0002;const int MOUSEEVENTF_LEFTUP = 0x0004;private const byte VK_CONTROL = 0x11;private const byte VK_V = 0x56;/// <summary>/// 移动到指定位置,并点击指定位置/// </summary>/// <param name="x"></param>/// <param name="y"></param>public static void MoveToAndClick(int x , int y){// 设置鼠标位置到 (x,y)SetCursorPos(x, y);// 模拟鼠标点击(左键按下和松开)mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // 按下mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // 松开}/// <summary>/// 移动到指定位置/// </summary>/// <param name="x"></param>/// <param name="y"></param>public static void MoveTo(int x, int y){// 设置鼠标位置到 (x,y)SetCursorPos(x, y);}/// <summary>/// 模拟键盘点击某个按键/// </summary>/// <param name="key"></param>public static void KeyClick(byte key){keybd_event(key, 0, KEYEVENTF_KEYDOWN, 0); // 按下keybd_event(key, 0, KEYEVENTF_KEYUP, 0); // 松开}/// <summary>/// 模拟键盘输入字符串(仅支持英文字母和数字)/// </summary>/// <param name="inputString"></param>public static void KeyboardInput(string inputString){// 获取当前 CapsLock 键的状态bool isCapsLockOn = Control.IsKeyLocked(Keys.CapsLock);Thread.Sleep(50); // 添加适当延迟foreach (char c in inputString){// 判断当前字符是否是大写bool isUpper = char.IsUpper(c);// 如果 CapsLock 状态与需要的大小写不匹配,按下 CapsLock 键if (isUpper && !isCapsLockOn || !isUpper && isCapsLockOn){KeyClick(0x14);// 按一下 CapsLock 键}// 按下字符键byte key = (byte)Char.ToUpper(c);KeyClick(key);// 按一下字符键// 如果 CapsLock 状态与需要的大小写不匹配,按下 CapsLock 键if (isUpper && !isCapsLockOn || !isUpper && isCapsLockOn){KeyClick(0x14);// 按一下 CapsLock 键}Thread.Sleep(50); // 添加适当延迟}}/// <summary>/// 【推荐】模拟键盘输入字符串(支持所有字符,本质是复制黏贴)/// </summary>/// <param name="inputString"></param>public static void KeyboardPlusInput(string inputString){Thread.Sleep(100); // 添加适当延迟Clipboard.SetText(inputString);Thread.Sleep(100); // 添加适当延迟// 按下 Ctrl 键keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYDOWN, 0);// 按下 V 键keybd_event(VK_V, 0, KEYEVENTF_KEYDOWN, 0);// 松开 V 键keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0);// 松开 Ctrl 键keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);}
}