1. 程式人生 > >C# 模擬 滑鼠 鍵盤操作

C# 模擬 滑鼠 鍵盤操作

一.封裝一個Io_Api 類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Timers;
using System.Windows.Forms; //Keys列舉在這裡面
namespace io_apis
{
    //滑鼠事件常量
    public enum MouseEventFlag : uint
    {
        Move = 0x0001,
        LeftDown = 0x0002,
        LeftUp = 0x0004,
        RightDown = 0x0008,
        RightUp = 0x0010,
        MiddleDown = 0x0020,
        MiddleUp = 0x0040,
        XDown = 0x0080,
        XUp = 0x0100,
        Wheel = 0x0800,
        VirtualDesk = 0x4000,
        Absolute = 0x8000
    }

    //鍵盤事件常量
    public enum KeyEventFlag : int
    {
        Down = 0x0000,
        Up = 0x0002,
    }
    
    public class Io_Api
    {
        //滑鼠事件函式
        [DllImport("user32.dll", EntryPoint = "mouse_event")]
        public static extern void mouse_event(MouseEventFlag dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        //滑鼠移動函式
        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        private static extern int SetCursorPos(int x, int y);

        //鍵盤事件函式
        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(Byte bVk, Byte bScan, KeyEventFlag dwFlags, Int32 dwExtraInfo);

        //定時器
        private System.Timers.Timer atimer = new System.Timers.Timer();

        //自動釋放鍵值
        private Byte vbk;

        //初始化
        public Io_Api() {

            //設定定時器事件
            this.atimer.Elapsed += new ElapsedEventHandler(atimer_Elapsed);
            this.atimer.AutoReset = true;
        }


        //滑鼠操作 _dx,_dy 是滑鼠距離當前位置的二維移動向量
        public void mouse(MouseEventFlag _dwFlags,int _dx,int _dy)
        {
            mouse_event(_dwFlags, _dx, _dy, 0, 0);
        }

        //滑鼠操作
        public void mouse_click(string button="L",bool is_double=false)
        
        {
            switch (button){
                case "L": 
                    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                    mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
                    if (is_double) {
                        mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                        mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
                    }
                    break;
                case "R":
                    mouse_event(MouseEventFlag.RightDown, 0, 0, 0, 0);
                    mouse_event(MouseEventFlag.RightUp, 0, 0, 0, 0);
                    if (is_double) {
                        mouse_event(MouseEventFlag.RightDown, 0, 0, 0, 0);
                        mouse_event(MouseEventFlag.RightUp, 0, 0, 0, 0);
                    }
                    break;
                case "M":
                    mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, 0);
                    mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, 0);
                    if (is_double)
                    {
                        mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, 0);
                        mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, 0);
                    }
                    break;
            }
        }

        //滑鼠移動到 指定位置(_dx,_dy)
        public void mouse_move(int _dx, int _dy)
        {
            SetCursorPos(_dx, _dy);
        }

        //鍵盤操作
        public void keybd(Byte _bVk, KeyEventFlag _dwFlags)
        {
            keybd_event(_bVk, 0, _dwFlags, 0);
        }

        //鍵盤操作 帶自動釋放 dwFlags_time 單位:毫秒
        public void keybd(Byte __bVk, int dwFlags_time = 100)
        {

            this.vbk = __bVk;
            //設定定時器間隔時間
            this.atimer.Interval = dwFlags_time;
            keybd(this.vbk, KeyEventFlag.Down);
            this.atimer.Enabled = true;
        }

        //鍵盤操作 組合鍵 帶釋放
        public void keybd(Byte[] _bVk)
        {
            if (_bVk.Length >= 2)
            {
                //按下所有鍵
                foreach (Byte __bVk in _bVk){
                    keybd(__bVk, KeyEventFlag.Down);
                }
                //反轉按鍵排序
                _bVk=(Byte[])_bVk.Reverse().ToArray();

                //鬆開所有鍵
                foreach (Byte __bVk in _bVk)
                {
                    keybd(__bVk, KeyEventFlag.Up);
                }
            }
        }

        void atimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.atimer.Enabled = false;

            //釋放按鍵
            keybd(this.vbk, KeyEventFlag.Up);
        }

        //獲取鍵碼 這一部分 就是根據字串 獲取 鍵碼 這裡只列出了一部分 可以自己修改
        public Byte getKeys(string key)
        {
            switch (key) {
                case "A": return (Byte)Keys.A; 
                case "B": return (Byte)Keys.B; 
                case "C": return (Byte)Keys.C; 
                case "D": return (Byte)Keys.D; 
                case "E": return (Byte)Keys.E; 
                case "F": return (Byte)Keys.F; 
                case "G": return (Byte)Keys.G; 
                case "H": return (Byte)Keys.H; 
                case "I": return (Byte)Keys.I; 
                case "J": return (Byte)Keys.J; 
                case "K": return (Byte)Keys.K; 
                case "L": return (Byte)Keys.L; 
                case "M": return (Byte)Keys.M; 
                case "N": return (Byte)Keys.N; 
                case "O": return (Byte)Keys.O; 
                case "P": return (Byte)Keys.P; 
                case "Q": return (Byte)Keys.Q; 
                case "R": return (Byte)Keys.R; 
                case "S": return (Byte)Keys.S; 
                case "T": return (Byte)Keys.T; 
                case "U": return (Byte)Keys.U; 
                case "V": return (Byte)Keys.V; 
                case "W": return (Byte)Keys.W; 
                case "X": return (Byte)Keys.X; 
                case "Y": return (Byte)Keys.Y; 
                case "Z": return (Byte)Keys.Z;
                case "Add": return (Byte)Keys.Add;
                case "Back": return (Byte)Keys.Back;
                case "Cancel": return (Byte)Keys.Cancel;
                case "Capital": return (Byte)Keys.Capital;
                case "CapsLock": return (Byte)Keys.CapsLock;
                case "Clear": return (Byte)Keys.Clear;
                case "Crsel": return (Byte)Keys.Crsel;
                case "ControlKey": return (Byte)Keys.ControlKey;
                case "D0": return (Byte)Keys.D0;
                case "D1": return (Byte)Keys.D1;
                case "D2": return (Byte)Keys.D2;
                case "D3": return (Byte)Keys.D3;
                case "D4": return (Byte)Keys.D4;
                case "D5": return (Byte)Keys.D5;
                case "D6": return (Byte)Keys.D6;
                case "D7": return (Byte)Keys.D7;
                case "D8": return (Byte)Keys.D8;
                case "D9": return (Byte)Keys.D9;
                case "Decimal": return (Byte)Keys.Decimal;
                case "Delete": return (Byte)Keys.Delete;
                case "Divide": return (Byte)Keys.Divide;
                case "Down": return (Byte)Keys.Down;
                case "End": return (Byte)Keys.End;
                case "Enter": return (Byte)Keys.Enter;
                case "Escape": return (Byte)Keys.Escape;
                case "F1": return (Byte)Keys.F1;
                case "F2": return (Byte)Keys.F2;
                case "F3": return (Byte)Keys.F3;
                case "F4": return (Byte)Keys.F4;
                case "F5": return (Byte)Keys.F5;
                case "F6": return (Byte)Keys.F6;
                case "F7": return (Byte)Keys.F7;
                case "F8": return (Byte)Keys.F8;
                case "F9": return (Byte)Keys.F9;
                case "F10": return (Byte)Keys.F10;
                case "F11": return (Byte)Keys.F11;
                case "F12": return (Byte)Keys.F12;
                case "Help": return (Byte)Keys.Help;
                case "Home": return (Byte)Keys.Home;
                case "Insert": return (Byte)Keys.Insert;
                case "LButton": return (Byte)Keys.LButton;
                case "LControl": return (Byte)Keys.LControlKey;
                case "Left": return (Byte)Keys.Left;
                case "LMenu": return (Byte)Keys.LMenu;
                case "LShift": return (Byte)Keys.LShiftKey;
                case "LWin": return (Byte)Keys.LWin;
                case "MButton": return (Byte)Keys.MButton;
                case "Menu": return (Byte)Keys.Menu;
                case "Multiply": return (Byte)Keys.Multiply;
                case "Next": return (Byte)Keys.Next;
                case "NumLock": return (Byte)Keys.NumLock;
                case "NumPad0": return (Byte)Keys.NumPad0;
                case "NumPad1": return (Byte)Keys.NumPad1;
                case "NumPad2": return (Byte)Keys.NumPad2;
                case "NumPad3": return (Byte)Keys.NumPad3;
                case "NumPad4": return (Byte)Keys.NumPad4;
                case "NumPad5": return (Byte)Keys.NumPad5;
                case "NumPad6": return (Byte)Keys.NumPad6;
                case "NumPad7": return (Byte)Keys.NumPad7;
                case "NumPad8": return (Byte)Keys.NumPad8;
                case "NumPad9": return (Byte)Keys.NumPad9;
                case "PageDown": return (Byte)Keys.PageDown;
                case "PageUp": return (Byte)Keys.PageUp;
                case "Process": return (Byte)Keys.ProcessKey;
                case "RButton": return (Byte)Keys.RButton;
                case "Right": return (Byte)Keys.Right;
                case "RControl": return (Byte)Keys.RControlKey;
                case "RMenu": return (Byte)Keys.RMenu;
                case "RShift": return (Byte)Keys.RShiftKey;
                case "Scroll": return (Byte)Keys.Scroll;
                case "Space": return (Byte)Keys.Space;
                case "Tab": return (Byte)Keys.Tab;
                case "Up": return (Byte)Keys.Up;
            }
            return 0;
        }
    }
    
}

2.用法
Io_Api ia = new Io_Api();

//單鍵
//this.ia.keybd(this.ia.getKeys("A"));

//組合鍵
//QQ的截圖快捷鍵 Ctrl+Alt+A
//Byte[] s = { this.ia.getKeys("LControl"), this.ia.getKeys("LMenu"), this.ia.getKeys("A") };
//this.ia.keybd(s);

//滑鼠相對移動 
//this.ia.mouse(MouseEventFlag.Move,5,0);

//滑鼠移動到指定位置
//this.ia.mouse_move(100, 100);

//滑鼠 右鍵
//this.ia.mouse_click("R");

//滑鼠 左鍵 雙擊
this.ia.mouse_click("L",true);