架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22368|回复: 0

[资料] C# 模拟 鼠标 键盘操作【自测,确实可用】

[复制链接]
发表于 2016-2-5 14:34:57 | 显示全部楼层 |阅读模式
第一次写博文,也不知道有神马技巧  ,代码质量并不高,主要就是展示 如何模拟鼠标和键盘操作
一.封装了一个Io_Api 类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Timers;
  7. using System.Windows.Forms; //Keys枚举在这里面
  8. namespace io_apis
  9. {
  10.     //鼠标事件常量
  11.     public enum MouseEventFlag : uint
  12.     {
  13.         Move = 0x0001,
  14.         LeftDown = 0x0002,
  15.         LeftUp = 0x0004,
  16.         RightDown = 0x0008,
  17.         RightUp = 0x0010,
  18.         MiddleDown = 0x0020,
  19.         MiddleUp = 0x0040,
  20.         XDown = 0x0080,
  21.         XUp = 0x0100,
  22.         Wheel = 0x0800,
  23.         VirtualDesk = 0x4000,
  24.         Absolute = 0x8000
  25.     }

  26.     //键盘事件常量
  27.     public enum KeyEventFlag : int
  28.     {
  29.         Down = 0x0000,
  30.         Up = 0x0002,
  31.     }
  32.      
  33.     public class Io_Api
  34.     {
  35.         //鼠标事件函数
  36.         [DllImport("user32.dll", EntryPoint = "mouse_event")]
  37.         public static extern void mouse_event(MouseEventFlag dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

  38.         //鼠标移动函数
  39.         [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
  40.         private static extern int SetCursorPos(int x, int y);

  41.         //键盘事件函数
  42.         [DllImport("user32.dll", EntryPoint = "keybd_event")]
  43.         public static extern void keybd_event(Byte bVk, Byte bScan, KeyEventFlag dwFlags, Int32 dwExtraInfo);

  44.         //定时器
  45.         private System.Timers.Timer atimer = new System.Timers.Timer();

  46.         //自动释放键值
  47.         private Byte vbk;

  48.         //初始化
  49.         public Io_Api() {

  50.             //设置定时器事件
  51.             this.atimer.Elapsed += new ElapsedEventHandler(atimer_Elapsed);
  52.             this.atimer.AutoReset = true;
  53.         }


  54.         //鼠标操作 _dx,_dy 是鼠标距离当前位置的二维移动向量
  55.         public void mouse(MouseEventFlag _dwFlags,int _dx,int _dy)
  56.         {
  57.             mouse_event(_dwFlags, _dx, _dy, 0, 0);
  58.         }

  59.         //鼠标操作
  60.         public void mouse_click(string button="L",bool is_double=false)
  61.          
  62.         {
  63.             switch (button){
  64.                 case "L":
  65.                     mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
  66.                     mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
  67.                     if (is_double) {
  68.                         mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
  69.                         mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
  70.                     }
  71.                     break;
  72.                 case "R":
  73.                     mouse_event(MouseEventFlag.RightDown, 0, 0, 0, 0);
  74.                     mouse_event(MouseEventFlag.RightUp, 0, 0, 0, 0);
  75.                     if (is_double) {
  76.                         mouse_event(MouseEventFlag.RightDown, 0, 0, 0, 0);
  77.                         mouse_event(MouseEventFlag.RightUp, 0, 0, 0, 0);
  78.                     }
  79.                     break;
  80.                 case "M":
  81.                     mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, 0);
  82.                     mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, 0);
  83.                     if (is_double)
  84.                     {
  85.                         mouse_event(MouseEventFlag.MiddleDown, 0, 0, 0, 0);
  86.                         mouse_event(MouseEventFlag.MiddleUp, 0, 0, 0, 0);
  87.                     }
  88.                     break;
  89.             }
  90.         }

  91.         //鼠标移动到 指定位置(_dx,_dy)
  92.         public void mouse_move(int _dx, int _dy)
  93.         {
  94.             SetCursorPos(_dx, _dy);
  95.         }

  96.         //键盘操作
  97.         public void keybd(Byte _bVk, KeyEventFlag _dwFlags)
  98.         {
  99.             keybd_event(_bVk, 0, _dwFlags, 0);
  100.         }

  101.         //键盘操作 带自动释放 dwFlags_time 单位:毫秒
  102.         public void keybd(Byte __bVk, int dwFlags_time = 100)
  103.         {

  104.             this.vbk = __bVk;
  105.             //设置定时器间隔时间
  106.             this.atimer.Interval = dwFlags_time;
  107.             keybd(this.vbk, KeyEventFlag.Down);
  108.             this.atimer.Enabled = true;
  109.         }

  110.         //键盘操作 组合键 带释放
  111.         public void keybd(Byte[] _bVk)
  112.         {
  113.             if (_bVk.Length >= 2)
  114.             {
  115.                 //按下所有键
  116.                 foreach (Byte __bVk in _bVk){
  117.                     keybd(__bVk, KeyEventFlag.Down);
  118.                 }
  119.                 //反转按键排序
  120.                 _bVk=(Byte[])_bVk.Reverse().ToArray();

  121.                 //松开所有键
  122.                 foreach (Byte __bVk in _bVk)
  123.                 {
  124.                     keybd(__bVk, KeyEventFlag.Up);
  125.                 }
  126.             }
  127.         }

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

  131.             //释放按键
  132.             keybd(this.vbk, KeyEventFlag.Up);
  133.         }

  134.         //获取键码 这一部分 就是根据字符串 获取 键码 这里只列出了一部分 可以自己修改
  135.         public Byte getKeys(string key)
  136.         {
  137.             switch (key) {
  138.                 case "A": return (Byte)Keys.A;
  139.                 case "B": return (Byte)Keys.B;
  140.                 case "C": return (Byte)Keys.C;
  141.                 case "D": return (Byte)Keys.D;
  142.                 case "E": return (Byte)Keys.E;
  143.                 case "F": return (Byte)Keys.F;
  144.                 case "G": return (Byte)Keys.G;
  145.                 case "H": return (Byte)Keys.H;
  146.                 case "I": return (Byte)Keys.I;
  147.                 case "J": return (Byte)Keys.J;
  148.                 case "K": return (Byte)Keys.K;
  149.                 case "L": return (Byte)Keys.L;
  150.                 case "M": return (Byte)Keys.M;
  151.                 case "N": return (Byte)Keys.N;
  152.                 case "O": return (Byte)Keys.O;
  153.                 case "P": return (Byte)Keys.P;
  154.                 case "Q": return (Byte)Keys.Q;
  155.                 case "R": return (Byte)Keys.R;
  156.                 case "S": return (Byte)Keys.S;
  157.                 case "T": return (Byte)Keys.T;
  158.                 case "U": return (Byte)Keys.U;
  159.                 case "V": return (Byte)Keys.V;
  160.                 case "W": return (Byte)Keys.W;
  161.                 case "X": return (Byte)Keys.X;
  162.                 case "Y": return (Byte)Keys.Y;
  163.                 case "Z": return (Byte)Keys.Z;
  164.                 case "Add": return (Byte)Keys.Add;
  165.                 case "Back": return (Byte)Keys.Back;
  166.                 case "Cancel": return (Byte)Keys.Cancel;
  167.                 case "Capital": return (Byte)Keys.Capital;
  168.                 case "CapsLock": return (Byte)Keys.CapsLock;
  169.                 case "Clear": return (Byte)Keys.Clear;
  170.                 case "Crsel": return (Byte)Keys.Crsel;
  171.                 case "ControlKey": return (Byte)Keys.ControlKey;
  172.                 case "D0": return (Byte)Keys.D0;
  173.                 case "D1": return (Byte)Keys.D1;
  174.                 case "D2": return (Byte)Keys.D2;
  175.                 case "D3": return (Byte)Keys.D3;
  176.                 case "D4": return (Byte)Keys.D4;
  177.                 case "D5": return (Byte)Keys.D5;
  178.                 case "D6": return (Byte)Keys.D6;
  179.                 case "D7": return (Byte)Keys.D7;
  180.                 case "D8": return (Byte)Keys.D8;
  181.                 case "D9": return (Byte)Keys.D9;
  182.                 case "Decimal": return (Byte)Keys.Decimal;
  183.                 case "Delete": return (Byte)Keys.Delete;
  184.                 case "Divide": return (Byte)Keys.Divide;
  185.                 case "Down": return (Byte)Keys.Down;
  186.                 case "End": return (Byte)Keys.End;
  187.                 case "Enter": return (Byte)Keys.Enter;
  188.                 case "Escape": return (Byte)Keys.Escape;
  189.                 case "F1": return (Byte)Keys.F1;
  190.                 case "F2": return (Byte)Keys.F2;
  191.                 case "F3": return (Byte)Keys.F3;
  192.                 case "F4": return (Byte)Keys.F4;
  193.                 case "F5": return (Byte)Keys.F5;
  194.                 case "F6": return (Byte)Keys.F6;
  195.                 case "F7": return (Byte)Keys.F7;
  196.                 case "F8": return (Byte)Keys.F8;
  197.                 case "F9": return (Byte)Keys.F9;
  198.                 case "F10": return (Byte)Keys.F10;
  199.                 case "F11": return (Byte)Keys.F11;
  200.                 case "F12": return (Byte)Keys.F12;
  201.                 case "Help": return (Byte)Keys.Help;
  202.                 case "Home": return (Byte)Keys.Home;
  203.                 case "Insert": return (Byte)Keys.Insert;
  204.                 case "LButton": return (Byte)Keys.LButton;
  205.                 case "LControl": return (Byte)Keys.LControlKey;
  206.                 case "Left": return (Byte)Keys.Left;
  207.                 case "LMenu": return (Byte)Keys.LMenu;
  208.                 case "LShift": return (Byte)Keys.LShiftKey;
  209.                 case "LWin": return (Byte)Keys.LWin;
  210.                 case "MButton": return (Byte)Keys.MButton;
  211.                 case "Menu": return (Byte)Keys.Menu;
  212.                 case "Multiply": return (Byte)Keys.Multiply;
  213.                 case "Next": return (Byte)Keys.Next;
  214.                 case "NumLock": return (Byte)Keys.NumLock;
  215.                 case "NumPad0": return (Byte)Keys.NumPad0;
  216.                 case "NumPad1": return (Byte)Keys.NumPad1;
  217.                 case "NumPad2": return (Byte)Keys.NumPad2;
  218.                 case "NumPad3": return (Byte)Keys.NumPad3;
  219.                 case "NumPad4": return (Byte)Keys.NumPad4;
  220.                 case "NumPad5": return (Byte)Keys.NumPad5;
  221.                 case "NumPad6": return (Byte)Keys.NumPad6;
  222.                 case "NumPad7": return (Byte)Keys.NumPad7;
  223.                 case "NumPad8": return (Byte)Keys.NumPad8;
  224.                 case "NumPad9": return (Byte)Keys.NumPad9;
  225.                 case "PageDown": return (Byte)Keys.PageDown;
  226.                 case "PageUp": return (Byte)Keys.PageUp;
  227.                 case "Process": return (Byte)Keys.ProcessKey;
  228.                 case "RButton": return (Byte)Keys.RButton;
  229.                 case "Right": return (Byte)Keys.Right;
  230.                 case "RControl": return (Byte)Keys.RControlKey;
  231.                 case "RMenu": return (Byte)Keys.RMenu;
  232.                 case "RShift": return (Byte)Keys.RShiftKey;
  233.                 case "Scroll": return (Byte)Keys.Scroll;
  234.                 case "Space": return (Byte)Keys.Space;
  235.                 case "Tab": return (Byte)Keys.Tab;
  236.                 case "Up": return (Byte)Keys.Up;
  237.             }
  238.             return 0;
  239.         }
  240.     }

  241.      
  242. }
复制代码


2.用法

  1. Io_Api ia = new Io_Api();

  2. //单键
  3. //this.ia.keybd(this.ia.getKeys("A"));

  4. //组合键
  5. //QQ的截图快捷键 Ctrl+Alt+A
  6. //Byte[] s = { this.ia.getKeys("LControl"), this.ia.getKeys("LMenu"), this.ia.getKeys("A") };
  7. //this.ia.keybd(s);

  8. //鼠标相对移动
  9. //this.ia.mouse(MouseEventFlag.Move,5,0);

  10. //鼠标移动到指定位置
  11. //this.ia.mouse_move(100, 100);

  12. //鼠标 右键
  13. //this.ia.mouse_click("R");

  14. //鼠标 左键 双击
  15. this.ia.mouse_click("L",true);
复制代码






上一篇:往往会
下一篇:ASP.NET machineKey的作用和使用方法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-2-19 11:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表