C# 模拟 鼠标 键盘操作

https://m.oschina.net/blog/198175


一.封装一个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);


WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值