基本信息
源码名称:C# 模拟鼠标键盘操作示例源码
源码大小:0.28M
文件格式:.rar
开发语言:C#
更新时间:2018-03-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 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;
            }
        }