欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

C# 模拟鼠标键盘操作示例源码

介绍 评论 失效链接反馈

完美模拟键盘鼠标操作首先录制 按键的顺序,然后点击【测试】按钮,即可自动执行 模拟鼠标键盘操作from clipboardC# 模拟鼠标键盘操作示例源码 常用C#方法-第2张 [DllImport("user32")] public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); [DllImport("user32.dll")] static extern bool SetCursorPos(int X, int Y); const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; [DllImport("user32.dll")] static extern short GetKeyState(int nVirtKey); [DllImport("user32.dll")] static extern void keybd_event( byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo ); public static int x = 0; public static int y = 0; public enum VirtualKeys : byte { VK_NUMLOCK = 0x90, //数字锁定键 VK_SCROLL = 0x91, //滚动锁定 VK_CAPITAL = 0x14, //大小写锁定 VK_A = 62, VK_LEFT = 37, //左箭头 VK_UP = 38,//上箭头 VK_RIGHT = 39,// 右箭头 VK_DOWN = 40,//  下箭头 VK_RETURN = 13, //回车 VK_DELETE = Keys.Delete } private enum MouseOper : byte { MOVE = 0x0001, /* mouse move */ LEFTDOWN = 0x0002, /* left button down */ LEFTUP = 0x0004, /* left button up */ RIGHTDOWN = 0x0008, /* right button down */ RIGHTUP = 0x0010, } public static bool GetState(KeyBord Key) { return (GetKeyState((int)Key) == 1); } private static void SetState(KeyBord Key, bool State) { if (State != GetState(Key)) { keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } public static void LeftButtonClick(int x, int y)//左键单击 { SetCurPos(x, y); mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0); } public static void LeftButtonDoubleClick(int x, int y)//左键双击 { SetCurPos(x, y); mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0); mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0); } public static void RightButtonClick(int x, int y)//右键单击 { SetCurPos(x, y); mouse_event((int)(MouseOper.RIGHTDOWN | MouseOper.RIGHTUP), 0, 0, 0, 0); } public static void SetCurPos(int x, int y) { SetCursorPos(x, y); } public static void PressKey(KeyBord key)//模拟按键按下和弹起 { keybd_event((byte)key, 0x45, 0, 0);//按键按下 // Simulate a key release keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);//按键弹起 } public static void Compositekey( KeyBord key1, KeyBord key2, KeyBord key3) { keybd_event((byte)key1, (byte)0, 0, 0);//key1按下 keybd_event((byte)key1, (byte)0, 0, 0);//key2按下 MoseKeyboard.PressKey(key3);//key3按下弹起 keybd_event((byte)key2, (byte)0, KEYEVENTF_KEYUP, 0);//key2弹起 keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起 } public static void Compositekey(KeyBord key1, KeyBord key2) { keybd_event((byte)key1, (byte)0, 0, 0);//key1按下 MoseKeyboard.PressKey(key2);//key3按下弹起 keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起 } public static bool ExcuteCmd(string cmd)//执行单行命令 { if (cmd == "") return false; //cmd格式是:延时,类型,(鼠标X,Y),命令 string[] str = cmd.Split(',');//分解命令 if(str.Length < 3 || str.Length >5)//命令是否正确 return false; try { Thread.Sleep(int.Parse(str[0]));//延时操作 if (str[1] == "KeyBord")//键盘操作 { string[] keys = str[2].Split(' ');//分解按键 if (keys.Length == 1)//单个健按下 { MoseKeyboard.PressKey(GetKeyByString(keys[0])); return true; } if (keys.Length == 2)//2个复合键按下 { MoseKeyboard.Compositekey(GetKeyByString(keys[0]),GetKeyByString( keys[1])); return true; } if (keys.Length == 2)//3个复合键按下 { MoseKeyboard.Compositekey(GetKeyByString(keys[0]), GetKeyByString(keys[1]),GetKeyByString(keys[2])); return true; } return false; } if (str[1] == "Mouse")//鼠标操作 { if (str[4] == "LeftButtonClick")//左键单击 { MoseKeyboard.LeftButtonClick(int.Parse(str[2]), int.Parse(str[3])); return true; } if (str[4] == "LeftButtonDoubleClick")//左键双击 { MoseKeyboard.LeftButtonDoubleClick(int.Parse(str[2]), int.Parse(str[3])); return true; } if (str[4] == "RightButtonClick")//右键单击 { MoseKeyboard.RightButtonClick(int.Parse(str[2]), int.Parse(str[3])); return true; } return false; } return false; } catch (Exception ex) { return false; } }

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复