C#鼠标键盘操作用于桌面脚本

项目中添加这两个类KeyBord ,MoseKeyboard(文末)

对于有些软件获取不到具体控件的句柄,那就只能用鼠标键盘操作了

比如想执行一个粘贴操作,可以这样写,通常需要在新起的线程中写,不然复制到粘贴板那会报错

Clipboard.SetDataObject("123456", true);//复制到粘贴板

MoseKeyboard.ExcuteCmd("1000,Mouse,1597,442,LeftButtonClick");  //点击文本框

MoseKeyboard.ExcuteCmd("1000,KeyBord,Control+V");//粘贴

起线程传入数组参数示例

 string[] array = new string[]
 {
  "100,Mouse,1017,242,LeftButtonClick",
  "1000,Mouse,891,514,RightButtonClick"
 };
 Thread ThreadPause = new Thread(new ParameterizedThreadStart(RunTest));
 ThreadPause.Start(array);

 private void RunTest(object obj)
 {
 string[] strArray = (string[])obj;
 }

传入的string参数如为分别为,时间间隔,操作类型,屏幕坐标xy,操作,

这里用按键精灵显示(我的资源中有https://download.csdn.net/download/m0_37137902/14900615)

通常脚本需要截取字符串,比如截取 ABC中A和C之间的B,可以用下面这个方法

string str = GetStringOfStartAndEnd()("A", "B", "ABC");
 public Func<string, string, string, string> GetStringOfStartAndEnd()
        {
            return (x, y, z) =>
            {
                string str = string.Empty;
                int start = z.IndexOf(x);
                int end = z.IndexOf(y);
                if (end > start)
                {
                    str = z.Substring(start + x.Length, end - start - x.Length);
                }
                return str;
            };
        }

直接取到末尾用这个方法,比如想得到abcd中,b以后的字段(当然这两个方法没啥难度,经常用就写到这方便后面复制)

string str = GetStringOfStartAndEndHttp()("abcd", "b");
public Func<string, string, string> GetStringOfStartAndEndHttp()
        {
            return (x, y) =>
            {
                string str = string.Empty;
                int start = x.IndexOf(y) + y.Length;
                int length = x.Length - start;
                str = x.Substring(start, length);
                return str;
            };
        }

脚本通常也需要获取url返回的值


        public string HttpGet(string url)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

经常会把执行命令放在txt里,可以用数组的形式读取出来

string[] array=File.ReadAllLines(Application.StartupPath + @"\mytxt.txt", Encoding.Default);

按键枚举类

 public enum KeyBord : int
    {
        // 摘要:
        //     从键值提取修饰符的位屏蔽。
        Modifiers = -65536,
        //
        // 摘要:
        //     Backspace 键。
        Backspace = 8,
        //
        // 摘要:
        //     Tab 键。
        Tab = 9,
        //
        // 摘要:
        //     LINEFEED 键。
        LineFeed = 10,
        //
        // 摘要:
        //     Clear 键。
        Clear = 12,
        //
        // 摘要:
        //     Enter 键。
        Enter = 13,
        //
        // 摘要:
        //     Return 键。
        Return = 13,
        //
        // 摘要:
        //     Shift 键。
        ShiftKey = 16,
        //
        // 摘要:
        //     Ctrl 键。
        ControlKey = 17,
        //
        // 摘要:
        //     Alt 键。
        Menu = 18,
        //
        // 摘要:
        //     Pause 键。
        Pause = 19,
        //
        // 摘要:
        //     Caps Lock 键。
        CapsLock = 20,
        //
        // 摘要:
        //     Caps Lock 键。
        Capital = 20,
        //
        // 摘要:
        //     IME Kana 模式键。
        KanaMode = 21,
        //
        // 摘要:
        //     IME Hanguel 模式键。(为了保持兼容性而设置;使用 HangulMode)
        HanguelMode = 21,
        //
        // 摘要:
        //     IME Hangul 模式键。
        HangulMode = 21,
        //
        // 摘要:
        //     IME Junja 模式键。
        JunjaMode = 23,
        //
        // 摘要:
        //     IME 最终模式键。
        FinalMode = 24,
        //
        // 摘要:
        //     IME Kanji 模式键。
        KanjiMode = 25,
        //
        // 摘要:
        //     IME Hanja 模式键。
        HanjaMode = 25,
        //
        // 摘要:
        //     Esc 键。
        Escape = 27,
        //
        // 摘要:
        //     IME 转换键。
        IMEConvert = 28,
        //
        // 摘要:
        //     IME 非转换键。
        IMENonconvert = 29,
        //
        // 摘要:
        //     IME 接受键。已过时,请改用 System.Windows.Forms.Keys.IMEAccept。
        IMEAceept = 30,
        //
        // 摘要:
        //     IME 接受键,替换 System.Windows.Forms.Keys.IMEAceept。
        IMEAccept = 30,
        //
        // 摘要:
        //     IME 模式更改键。
        IMEModeChange = 31,
        //
        // 摘要:
        //     空格键。
        Space = 32,
        //
        // 摘要:
        //     Page Up 键。
        Prior = 33,
        //
        // 摘要:
        //     Page Up 键。
        PageUp = 33,
        //
        // 摘要:
        //     Page Down 键。
        Next = 34,
        //
        // 摘要:
        //     Page Down 键。
        PageDown = 34,
        //
        // 摘要:
        //     End 键。
        End = 35,
        //
        // 摘要:
        //     Home 键。
        Home = 36,
        //
        // 摘要:
        //     向左键。
        Left = 37,
        //
        // 摘要:
        //     向上键。
        Up = 38,
        //
        // 摘要:
        //     向右键。
        Right = 39,
        //
        // 摘要:
        //     向下键。
        Down = 40,
        //
        // 摘要:
        //     Select 键。
        Select = 41,
        //
        // 摘要:
        //     Print 键。
        Print = 42,
        //
        // 摘要:
        //     EXECUTE 键。
        Execute = 43,
        //
        // 摘要:
        //     Print Screen 键。
        PrintScreen = 44,
        //
        // 摘要:
        //     Print Screen 键。
        Snapshot = 44,
        //
        // 摘要:
        //     Ins 键。
        Insert = 45,
        //
        // 摘要:
        //     DeL 键。
        Delete = 46,
        //
        // 摘要:
        //     Help 键。
        Help = 47,
        //
        // 摘要:
        //     0 键。
        D0 = 48,
        //
        // 摘要:
        //     1 键。
        D1 = 49,
        //
        // 摘要:
        //     2 键。
        D2 = 50,
        //
        // 摘要:
        //     3 键。
        D3 = 51,
        //
        // 摘要:
        //     4 键。
        D4 = 52,
        //
        // 摘要:
        //     5 键。
        D5 = 53,
        //
        // 摘要:
        //     6 键。
        D6 = 54,
        //
        // 摘要:
        //     7 键。
        D7 = 55,
        //
        // 摘要:
        //     8 键。
        D8 = 56,
        //
        // 摘要:
        //     9 键。
        D9 = 57,
        //
        // 摘要:
        //     A 键。
        A = 65,
        //
        // 摘要:
        //     B 键。
        B = 66,
        //
        // 摘要:
        //     C 键。
        C = 67,
        //
        // 摘要:
        //     D 键。
        D = 68,
        //
        // 摘要:
        //     E 键。
        E = 69,
        //
        // 摘要:
        //     F 键。
        F = 70,
        //
        // 摘要:
        //     G 键。
        G = 71,
        //
        // 摘要:
        //     H 键。
        H = 72,
        //
        // 摘要:
        //     I 键。
        I = 73,
        //
        // 摘要:
        //     J 键。
        J = 74,
        //
        // 摘要:
        //     K 键。
        K = 75,
        //
        // 摘要:
        //     L 键。
        L = 76,
        //
        // 摘要:
        //     M 键。
        M = 77,
        //
        // 摘要:
        //     N 键。
        N = 78,
        //
        // 摘要:
        //     O 键。
        O = 79,
        //
        // 摘要:
        //     P 键。
        P = 80,
        //
        // 摘要:
        //     Q 键。
        Q = 81,
        //
        // 摘要:
        //     R 键。
        R = 82,
        //
        // 摘要:
        //     S 键。
        S = 83,
        //
        // 摘要:
        //     T 键。
        T = 84,
        //
        // 摘要:
        //     U 键。
        U = 85,
        //
        // 摘要:
        //     V 键。
        V = 86,
        //
        // 摘要:
        //     W 键。
        W = 87,
        //
        // 摘要:
        //     X 键。
        X = 88,
        //
        // 摘要:
        //     Y 键。
        Y = 89,
        //
        // 摘要:
        //     Z 键。
        Z = 90,
        //
        // 摘要:
        //     左 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。
        LWin = 91,
        //
        // 摘要:
        //     右 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。
        RWin = 92,
        //
        // 摘要:
        //     应用程序键(Microsoft Natural Keyboard,人体工程学键盘)。
        Apps = 93,
        //
        // 摘要:
        //     计算机睡眠键。
        Sleep = 95,
        //
        // 摘要:
        //     数字键盘上的 0 键。
        NumPad0 = 96,
        //
        // 摘要:
        //     数字键盘上的 1 键。
        NumPad1 = 97,
        //
        // 摘要:
        //     数字键盘上的 2 键。
        NumPad2 = 98,
        //
        // 摘要:
        //     数字键盘上的 3 键。
        NumPad3 = 99,
        //
        // 摘要:
        //     数字键盘上的 4 键。
        NumPad4 = 100,
        //
        // 摘要:
        //     数字键盘上的 5 键。
        NumPad5 = 101,
        //
        // 摘要:
        //     数字键盘上的 6 键。
        NumPad6 = 102,
        //
        // 摘要:
        //     数字键盘上的 7 键。
        NumPad7 = 103,
        //
        // 摘要:
        //     数字键盘上的 8 键。
        NumPad8 = 104,
        //
        // 摘要:
        //     数字键盘上的 9 键。
        NumPad9 = 105,
        //
        // 摘要:
        //     乘号键。
        Multiply = 106,
        //
        // 摘要:
        //     加号键。
        Add = 107,
        //
        // 摘要:
        //     分隔符键。
        Separator = 108,
        //
        // 摘要:
        //     减号键。
        Subtract = 109,
        //
        // 摘要:
        //     句点键。
        Decimal = 110,
        //
        // 摘要:
        //     除号键。
        Divide = 111,
        //
        // 摘要:
        //     F1 键。
        F1 = 112,
        //
        // 摘要:
        //     F2 键。
        F2 = 113,
        //
        // 摘要:
        //     F3 键。
        F3 = 114,
        //
        // 摘要:
        //     F4 键。
        F4 = 115,
        //
        // 摘要:
        //     F5 键。
        F5 = 116,
        //
        // 摘要:
        //     F6 键。
        F6 = 117,
        //
        // 摘要:
        //     F7 键。
        F7 = 118,
        //
        // 摘要:
        //     F8 键。
        F8 = 119,
        //
        // 摘要:
        //     F9 键。
        F9 = 120,
        //
        // 摘要:
        //     F10 键。
        F10 = 121,
        //
        // 摘要:
        //     F11 键。
        F11 = 122,
        //
        // 摘要:
        //     F12 键。
        F12 = 123,
        //
        // 摘要:
        //     F13 键。
        F13 = 124,
        //
        // 摘要:
        //     F14 键。
        F14 = 125,
        //
        // 摘要:
        //     F15 键。
        F15 = 126,
        //
        // 摘要:
        //     F16 键。
        F16 = 127,
        //
        // 摘要:
        //     F17 键。
        F17 = 128,
        //
        // 摘要:
        //     F18 键。
        F18 = 129,
        //
        // 摘要:
        //     F19 键。
        F19 = 130,
        //
        // 摘要:
        //     F20 键。
        F20 = 131,
        //
        // 摘要:
        //     F21 键。
        F21 = 132,
        //
        // 摘要:
        //     F22 键。
        F22 = 133,
        //
        // 摘要:
        //     F23 键。
        F23 = 134,
        //
        // 摘要:
        //     F24 键。
        F24 = 135,
        //
        // 摘要:
        //     Num Lock 键。
        NumLock = 144,
        //
        // 摘要:
        //     Scroll Lock 键。
        Scroll = 145,
        //
        // 摘要:
        //     左 Shift 键。
        LShiftKey = 160,
        //
        // 摘要:
        //     右 Shift 键。
        RShiftKey = 161,
        //
        // 摘要:
        //     左 Ctrl 键。
        LControlKey = 162,
        //
        // 摘要:
        //     右 Ctrl 键。
        RControlKey = 163,
        //
        // 摘要:
        //     左 Alt 键。
        LMenu = 164,
        //
        // 摘要:
        //     右 Alt 键。
        RMenu = 165,
        //
        // 摘要:
        //     浏览器后退键(Windows 2000 或更高版本)。
        BrowserBack = 166,
        //
        // 摘要:
        //     浏览器前进键(Windows 2000 或更高版本)。
        BrowserForward = 167,
        //
        // 摘要:
        //     浏览器刷新键(Windows 2000 或更高版本)。
        BrowserRefresh = 168,
        //
        // 摘要:
        //     浏览器停止键(Windows 2000 或更高版本)。
        BrowserStop = 169,
        //
        // 摘要:
        //     浏览器搜索键(Windows 2000 或更高版本)。
        BrowserSearch = 170,
        //
        // 摘要:
        //     浏览器收藏夹键(Windows 2000 或更高版本)。
        BrowserFavorites = 171,
        //
        // 摘要:
        //     浏览器主页键(Windows 2000 或更高版本)。
        BrowserHome = 172,
        //
        // 摘要:
        //     静音键(Windows 2000 或更高版本)。
        VolumeMute = 173,
        //
        // 摘要:
        //     减小音量键(Windows 2000 或更高版本)。
        VolumeDown = 174,
        //
        // 摘要:
        //     增大音量键(Windows 2000 或更高版本)。
        VolumeUp = 175,
        //
        // 摘要:
        //     媒体下一曲目键(Windows 2000 或更高版本)。
        MediaNextTrack = 176,
        //
        // 摘要:
        //     媒体上一曲目键(Windows 2000 或更高版本)。
        MediaPreviousTrack = 177,
        //
        // 摘要:
        //     媒体停止键(Windows 2000 或更高版本)。
        MediaStop = 178,
        //
        // 摘要:
        //     媒体播放暂停键(Windows 2000 或更高版本)。
        MediaPlayPause = 179,
        //
        // 摘要:
        //     启动邮件键(Windows 2000 或更高版本)。
        LaunchMail = 180,
        //
        // 摘要:
        //     选择媒体键(Windows 2000 或更高版本)。
        SelectMedia = 181,
        //
        // 摘要:
        //     启动应用程序一键(Windows 2000 或更高版本)。
        LaunchApplication1 = 182,
        //
        // 摘要:
        //     启动应用程序二键(Windows 2000 或更高版本)。
        LaunchApplication2 = 183,
        //
        // 摘要:
        //     OEM 1 键。
        Oem1 = 186,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 分号键(Windows 2000 或更高版本)。
        OemSemicolon = 186,
        //
        // 摘要:
        //     任何国家/地区键盘上的 OEM 加号键(Windows 2000 或更高版本)。
        Oemplus = 187,
        //
        // 摘要:
        //     任何国家/地区键盘上的 OEM 逗号键(Windows 2000 或更高版本)。
        Oemcomma = 188,
        //
        // 摘要:
        //     任何国家/地区键盘上的 OEM 减号键(Windows 2000 或更高版本)。
        OemMinus = 189,
        //
        // 摘要:
        //     任何国家/地区键盘上的 OEM 句点键(Windows 2000 或更高版本)。
        OemPeriod = 190,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 问号键(Windows 2000 或更高版本)。
        OemQuestion = 191,
        //
        // 摘要:
        //     OEM 2 键。
        Oem2 = 191,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 波形符键(Windows 2000 或更高版本)。
        Oemtilde = 192,
        //
        // 摘要:
        //     OEM 3 键。
        Oem3 = 192,
        //
        // 摘要:
        //     OEM 4 键。
        Oem4 = 219,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 左括号键(Windows 2000 或更高版本)。
        OemOpenBrackets = 219,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 管道键(Windows 2000 或更高版本)。
        OemPipe = 220,
        //
        // 摘要:
        //     OEM 5 键。
        Oem5 = 220,
        //
        // 摘要:
        //     OEM 6 键。
        Oem6 = 221,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 右括号键(Windows 2000 或更高版本)。
        OemCloseBrackets = 221,
        //
        // 摘要:
        //     OEM 7 键。
        Oem7 = 222,
        //
        // 摘要:
        //     美式标准键盘上的 OEM 单/双引号键(Windows 2000 或更高版本)。
        OemQuotes = 222,
        //
        // 摘要:
        //     OEM 8 键。
        Oem8 = 223,
        //
        // 摘要:
        //     OEM 102 键。
        Oem102 = 226,
        //
        // 摘要:
        //     RT 102 键的键盘上的 OEM 尖括号或反斜杠键(Windows 2000 或更高版本)。
        OemBackslash = 226,
        //
        // 摘要:
        //     Process Key 键。
        ProcessKey = 229,
        //
        // 摘要:
        //     用于将 Unicode 字符当作键击传递。Packet 键值是用于非键盘输入法的 32 位虚拟键值的低位字。
        Packet = 231,
        //
        // 摘要:
        //     Attn 键。
        Attn = 246,
        //
        // 摘要:
        //     Crsel 键。
        Crsel = 247,
        //
        // 摘要:
        //     Exsel 键。
        Exsel = 248,
        //
        // 摘要:
        //     ERASE EOF 键。
        EraseEof = 249,
        //
        // 摘要:
        //     Play 键。
        Play = 250,
        //
        // 摘要:
        //     Zoom 键。
        Zoom = 251,
        //
        // 摘要:
        //     保留以备将来使用的常数。
        NoName = 252,
        //
        // 摘要:
        //     PA1 键。
        Pa1 = 253,
        //
        // 摘要:
        //     Clear 键。
        OemClear = 254,
        //
        // 摘要:
        //     从键值提取键代码的位屏蔽。
        KeyCode = 65535,
        //
        // 摘要:
        //     Shift 修改键。
        Shift = 65536,
        //
        // 摘要:
        //     Ctrl 修改键。
        Control = 131072,
        //
        // 摘要:
        //     Alt 修改键。
        Alt = 262144,
    }

键盘操作MoseKeyboard类

 class MoseKeyboard
    {
        [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;
            }
        }

        public static KeyBord GetKeyByString(string key)
        {
            if (key == "A")
                return KeyBord.A;
            if (key == "B")
                return KeyBord.B;
            if (key == "C")
                return KeyBord.C;
            if (key == "D")
                return KeyBord.D;
            if (key == "E")
                return KeyBord.E;
            if (key == "F")
                return KeyBord.F;
            if (key == "G")
                return KeyBord.G;
            if (key == "H")
                return KeyBord.H;
            if (key == "I")
                return KeyBord.I;
            if (key == "J")
                return KeyBord.J;
            if (key == "K")
                return KeyBord.K;
            if (key == "L")
                return KeyBord.L;
            if (key == "M")
                return KeyBord.M;
            if (key == "N")
                return KeyBord.N;
            if (key == "O")
                return KeyBord.O;
            if (key == "P")
                return KeyBord.P;
            if (key == "Q")
                return KeyBord.Q;
            if (key == "R")
                return KeyBord.R;
            if (key == "S")
                return KeyBord.S;
            if (key == "T")
                return KeyBord.T;
            if (key == "U")
                return KeyBord.U;
            if (key == "V")
                return KeyBord.V;
            if (key == "W")
                return KeyBord.W;
            if (key == "X")
                return KeyBord.X;
            if (key == "Y")
                return KeyBord.Y;
            if (key == "Z")
                return KeyBord.Z;//A-Z
            if (key == "F1")
                return KeyBord.F1;
            if (key == "F2")
                return KeyBord.F2;
            if (key == "F3")
                return KeyBord.F3;
            if (key == "F4")
                return KeyBord.F4;
            if (key == "F5")
                return KeyBord.F5;
            if (key == "F6")
                return KeyBord.F6;
            if (key == "F7")
                return KeyBord.F7;
            if (key == "F8")
                return KeyBord.F8;
            if (key == "F9")
                return KeyBord.F9;
            if (key == "F10")
                return KeyBord.F10;
            if (key == "F11")
                return KeyBord.F11;
            if (key == "F12")
                return KeyBord.F12;//F1-F12
            if (key == "Esc")
                return KeyBord.Escape;
            if (key == "0")
                return KeyBord.D0;
            if (key == "1")
                return KeyBord.D1;
            if (key == "2")
                return KeyBord.D2;
            if (key == "3")
                return KeyBord.D3;
            if (key == "4")
                return KeyBord.D4;
            if (key == "5")
                return KeyBord.D5;
            if (key == "6")
                return KeyBord.D6;
            if (key == "7")
                return KeyBord.D7;
            if (key == "8")
                return KeyBord.D8;
            if (key == "9")
                return KeyBord.D9;//0-9
            if (key == "-")
                return KeyBord.OemMinus;
            if (key == "=")
                return KeyBord.Oemplus;
            if (key == @"\")
                return KeyBord.OemBackslash;
            if (key == "BackSpace")
                return KeyBord.Backspace;
            if (key == "Tab")
                return KeyBord.Tab;
            if (key == "CapsLock")
                return KeyBord.CapsLock;
            if (key == "Windows")
                return KeyBord.LWin;
            if (key == "Alt")
                return KeyBord.Alt;
            if (key == "Shift")
                return KeyBord.ShiftKey;
            if (key == "Control")
                return KeyBord.ControlKey;
            if (key == "[")
                return KeyBord.OemOpenBrackets;
            if (key == "]")
                return KeyBord.Oem6;
            if (key == ";")
                return KeyBord.OemSemicolon;
            if (key == "'")
                return KeyBord.OemQuotes;
            if (key == ",")
                return KeyBord.Oemcomma;
            if (key == ".")
                return KeyBord.OemPeriod;
            if (key == "?")
                return KeyBord.OemQuestion;
            if (key == "Space")
                return KeyBord.Space;
            if (key == "<")
                return KeyBord.Oemcomma;
            if (key == ">")
                return KeyBord.OemPeriod;
            if (key == "/")
                return KeyBord.OemQuestion;
            if (key == "~")
                return KeyBord.Oem3;
            if (key == "Inst")/控制键盘
                return KeyBord.Insert;
            if (key == "Home")
                return KeyBord.Home;
            if (key == "End")
                return KeyBord.End;
            if (key == "Enter")
                return KeyBord.Enter;
            if (key == "PageDw")
                return KeyBord.PageDown;
            if (key == "PageUp")
                return KeyBord.PageUp;
            if (key == "Left")
                return KeyBord.Left;
            if (key == "Right")
                return KeyBord.Right;
            if (key == "Up")
                return KeyBord.Up;
            if (key == "Down")
                return KeyBord.Down;
            if (key == "PrintScreen")
                return KeyBord.PrintScreen;
            if (key == "Scroll")
                return KeyBord.Scroll;
            if (key == "Num0")//数字键盘
                return KeyBord.NumPad0;
            if (key == "Num1")
                return KeyBord.NumPad1;
            if (key == "Num2")
                return KeyBord.NumPad2;
            if (key == "Num3")
                return KeyBord.NumPad3;
            if (key == "Num4")
                return KeyBord.NumPad4;
            if (key == "Num5")
                return KeyBord.NumPad5;
            if (key == "Num6")
                return KeyBord.NumPad6;
            if (key == "Num7")
                return KeyBord.NumPad7;
            if (key == "Num8")
                return KeyBord.NumPad8;
            if (key == "Num9")
                return KeyBord.NumPad9;
            if (key == "NumAdd")
                return KeyBord.Add;
            if (key == "Num/")
                return KeyBord.Divide;
            if (key == "Num*")
                return KeyBord.Multiply;
            if (key == "Num-")
                return KeyBord.Subtract;
            if (key == "NumEnter")
                return KeyBord.Enter;
            if (key == "Num.")
                return KeyBord.OemPeriod;
            if (key == "NumLock")
                return KeyBord.NumLock;


            return KeyBord.T;
        }

    }

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#提供了一个`System.Windows.Forms`命名空间,其中包含了许多与用户界面相关的及方法,可以用来模拟键盘鼠标操作。 下面是一些常用的模拟键盘鼠标操作的方法: 1. 模拟按键操作 可以使用`SendKeys`中的`Send`方法来模拟按键操作。例如,要模拟按下“a”键,可以使用以下代码: ``` SendKeys.Send("a"); ``` 2. 模拟组合键操作 如果要模拟组合键操作,例如Ctrl+C复制操作,可以将组合键的按键值用“+”连接起来,如下所示: ``` SendKeys.Send("^c"); ``` 其中,“^”表示Ctrl键,“+”表示Shift键,“%”表示Alt键,“{F1}”表示F1键,等等。 3. 模拟鼠标点击操作 可以使用`System.Windows.Forms.Cursor`中的`Position`属性来获取当前鼠标的位置,然后使用`System.Windows.Forms.Mouse`中的`LeftClick`或`RightClick`方法来模拟鼠标左键或右键点击操作。例如,要模拟在屏幕上(100,100)的位置进行左键点击操作,可以使用以下代码: ``` Cursor.Position = new Point(100, 100); Mouse.LeftClick(); ``` 4. 模拟鼠标移动操作 可以使用`System.Windows.Forms.Cursor`中的`Position`属性来设置鼠标的位置,从而模拟鼠标移动操作。例如,要将鼠标移到屏幕上(200,200)的位置,可以使用以下代码: ``` Cursor.Position = new Point(200, 200); ``` 需要注意的是,模拟键盘鼠标操作可能会对系统产生影响,因此在使用时要谨慎,并且尽量避免在用户不知情的情况下进行操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值