一、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);
}
}到此这篇关于c#进行操作鼠标和键盘的示例详解的文章就介绍到这了,更多相关c#操作鼠标和键盘内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论