zoukankan      html  css  js  c++  java
  • 模拟操作(键盘、鼠标)三

    刚入此行的时候,小弟写过点类似模拟的操作:

    1、模拟操作(键盘、鼠标)一

    2、模拟操作(键盘、鼠标)二

    本次,换了一家新公司之后,遇到同样的问题,所以呢,小弟封装了一个类。

    希望大神不要喷,谢谢

    public class ApiHelper
        {
            //键盘事件常量
            public enum KeyEventFlag : int
            {
                Down = 0x0000,
                Up = 0x0002,
            }
    
            public class Io_Api
            {
                
               
                //键盘事件函数
                [DllImport("user32.dll", EntryPoint = "keybd_event")]
                public static extern void keybd_event(Byte bVk, Byte bScan, KeyEventFlag dwFlags, Int32 dwExtraInfo);
    
                [System.Runtime.InteropServices.DllImport("user32")]
                public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int 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;
                }
    
    
                //键盘操作
                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;
                }
    
                //键盘操作 带自动释放 dwFlags_time 单位:毫秒
                /// <summary>
                /// 按键
                /// </summary>
                /// <param name="__bVk"></param>
                /// <param name="up">是否自动释放</param>
                public void keybd(Byte __bVk, bool up)
                {
                    if (up)
                    {
                        this.vbk = __bVk;
                        //设置定时器间隔时间
                        // this.atimer.Interval = 0;
                        keybd(this.vbk, KeyEventFlag.Down);
                        keybd(this.vbk, KeyEventFlag.Up);
    
                        //this.atimer.Enabled = true;
                    }
                    else
                    {
                        this.vbk = __bVk;
                        //设置定时器间隔时间
                        this.atimer.Interval = 100;
                        keybd(this.vbk, KeyEventFlag.Down);
                        //this.atimer.Enabled = true;
                    }
                }
    
                //键盘操作 组合键 带释放 Ctrl+V
                public void keybd(Byte[] _bVk)
                {
                    //if (_bVk.Length >= 2)
                    //{
                    //    //按下所有键
                    //    foreach (Byte __bVk in _bVk)
                    //    {
                    //        keybd(__bVk, KeyEventFlag.Down);
                    //    }
                    //    //反转按键排序
                    //    //_bVk = (Byte[])_bVk.Reverse().ToArray();
                    //    Array.Reverse(_bVk);
                    //    //松开所有键
                    //    foreach (Byte __bVk in _bVk)
                    //    {
                    //        keybd(__bVk, KeyEventFlag.Up);
                    //    }
    
    
                    //}
    
                    keybd_event((byte)Keys.ControlKey, 0, 0, 0);//按下
                    keybd_event((byte)Keys.V, 0, 0, 0);
                    keybd_event((byte)Keys.ControlKey, 0, 0x2, 0);//松开
                    keybd_event((byte)Keys.V, 0, 0x2, 0);
    
                }
    
                //键盘操作 组合键 带释放 Alt+U
                public void AltU(Byte[] _bVk)
                {
                    
    
                    keybd_event((byte)Keys.LMenu, 0, 0, 0);//按下
                    keybd_event((byte)Keys.U, 0, 0, 0);
                    keybd_event((byte)Keys.LMenu, 0, 0x2, 0);//松开
                    keybd_event((byte)Keys.U, 0, 0x2, 0);
    
                }
    
    
    
                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 "0": return (Byte)Keys.D0;
                        case "1": return (Byte)Keys.D1;
                        case "2": return (Byte)Keys.D2;
                        case "3": return (Byte)Keys.D3;
                        case "4": return (Byte)Keys.D4;
                        case "5": return (Byte)Keys.D5;
                        case "6": return (Byte)Keys.D6;
                        case "7": return (Byte)Keys.D7;
                        case "8": return (Byte)Keys.D8;
                        case "9": 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;
                }
            }
        }
    

      希望各位大神能及时提出建议,谢谢。

      昨天看了“野生程序员”,今天又来了一篇,呵呵,我电子信息工程的,不晓得算什么,呵呵,不管他们,做好自己。

      WE GO!!!

  • 相关阅读:
    深入浅出:了解前后端分离优势、前后端接口联调以及优化问题
    深入浅出:了解JavaScript中的call,apply,bind的差别
    Vue2.0 搭建Vue脚手架(vue-cli)
    深入浅出:promise的各种用法
    深入浅出:了解常见的设计模式(闭包、垃圾回收机制)
    sql server xml 功能
    sqlite 用法
    PowerDesigner使用
    asp.net 开发注意的几点
    vue template
  • 原文地址:https://www.cnblogs.com/JeffController/p/4798155.html
Copyright © 2011-2022 走看看