当前位置:   article > 正文

C#分享辅助类:键盘模拟(KeybordHandler)_c# 模拟键盘输入

c# 模拟键盘输入

名称

方法

字符串转键盘编码

StrToKeybord

键盘输入

KeyboardInput

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Wsfly
  6. {
  7. /// <summary>
  8. /// 键盘操作
  9. /// </summary>
  10. public class KeybordHandler
  11. {
  12. /// <summary>
  13. /// 默认按键配置
  14. /// </summary>
  15. static Dictionary<string, int[]> _DEFAULTKEYS = null;
  16. /// <summary>
  17. /// 字符串转键盘编码
  18. /// </summary>
  19. /// <param name="text"></param>
  20. public static int[] StrToKeybord(string text)
  21. {
  22. if (string.IsNullOrEmpty(text)) return null;
  23. if (_DEFAULTKEYS == null) new KeybordHandler();
  24. List<int> keys = new List<int>();
  25. foreach (char c in text)
  26. {
  27. int[] key = _DEFAULTKEYS["{" + c + "}"];
  28. foreach (int k in key)
  29. {
  30. keys.Add(k);
  31. }
  32. }
  33. return keys.ToArray();
  34. }
  35. /// <summary>
  36. /// 键盘输入
  37. /// </summary>
  38. /// <param name="text"></param>
  39. public static void KeyboardInput(string text)
  40. {
  41. int[] keys = StrToKeybord(text);
  42. KeyboardDo(keys);
  43. }
  44. const uint KEYEVENTF_EXTENDEDKEY = 0x1;
  45. const uint KEYEVENTF_KEYUP = 0x2;
  46. /// <summary>
  47. /// 键盘事件
  48. /// </summary>
  49. /// <param name="bVk"></param>
  50. /// <param name="bScan"></param>
  51. /// <param name="dwFlags"></param>
  52. /// <param name="dwExtraInfo"></param>
  53. [System.Runtime.InteropServices.DllImport("user32")]
  54. static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
  55. /// <summary>
  56. /// 按钮状态
  57. /// </summary>
  58. /// <param name="nVirtKey"></param>
  59. /// <returns></returns>
  60. [System.Runtime.InteropServices.DllImport("user32.dll")]
  61. static extern short GetKeyState(int nVirtKey);
  62. /// <summary>
  63. /// 模拟键盘按下
  64. /// </summary>
  65. /// <param name="keys"></param>
  66. public static void KeyboardDo(int[] keys)
  67. {
  68. for (int i = 0; i < keys.Length; i++)
  69. {
  70. int key = keys[i];
  71. if (key == 16 || key == 17 || key == 18)
  72. {
  73. //Shift/Ctrl/Alt键时 组合键
  74. keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
  75. keybd_event((byte)keys[i + 1], 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
  76. keybd_event((byte)keys[i + 1], 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  77. keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  78. //跳到下一个
  79. i++;
  80. }
  81. else
  82. {
  83. //非组合键
  84. keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
  85. keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// 构造
  91. /// </summary>
  92. private KeybordHandler()
  93. {
  94. _DEFAULTKEYS = new Dictionary<string, int[]>();
  95. _DEFAULTKEYS.Add("{VK_BACK}", new int[] { 8 });//退格键
  96. _DEFAULTKEYS.Add("{VK_TAB}", new int[] { 9 });//Tab键
  97. _DEFAULTKEYS.Add("{VK_RETURN}", new int[] { 13 });//回车键
  98. _DEFAULTKEYS.Add("{VK_SHIFT}", new int[] { 16 });//Shift键
  99. _DEFAULTKEYS.Add("{VK_CONTROL}", new int[] { 17 });//Ctrl键
  100. _DEFAULTKEYS.Add("{VK_MENU}", new int[] { 18 });//Alt键
  101. _DEFAULTKEYS.Add("{VK_PAUSE}", new int[] { 19 });//Pause Break键
  102. _DEFAULTKEYS.Add("{VK_CAPITAL}", new int[] { 20 });//Caps Lock键
  103. _DEFAULTKEYS.Add("{VK_ESC}", new int[] { 27 });//ESC键
  104. _DEFAULTKEYS.Add("{VK_SPACE}", new int[] { 32 });//空格键
  105. _DEFAULTKEYS.Add("{VK_PRIOR}", new int[] { 33 });//Page Up
  106. _DEFAULTKEYS.Add("{VK_NEXT}", new int[] { 34 });//Page Down
  107. _DEFAULTKEYS.Add("{VK_END}", new int[] { 35 });//End键
  108. _DEFAULTKEYS.Add("{VK_HOME}", new int[] { 36 });//Home键
  109. _DEFAULTKEYS.Add("{VK_LEFT}", new int[] { 37 });//方向键:←
  110. _DEFAULTKEYS.Add("{VK_UP}", new int[] { 38 });//方向键:↑
  111. _DEFAULTKEYS.Add("{VK_RIGHT}", new int[] { 39 });//方向键:→
  112. _DEFAULTKEYS.Add("{VK_DOWN}", new int[] { 40 });//方向键:↓
  113. _DEFAULTKEYS.Add("{VK_INSERT}", new int[] { 45 });//Insert键
  114. _DEFAULTKEYS.Add("{VK_DELETE}", new int[] { 46 });//Delete键
  115. _DEFAULTKEYS.Add("{VK_LWIN}", new int[] { 91 });//左徽标键
  116. _DEFAULTKEYS.Add("{VK_RWIN}", new int[] { 92 });//右徽标键
  117. _DEFAULTKEYS.Add("{VK_APPS}", new int[] { 93 });//鼠标右键快捷键
  118. //小键盘
  119. _DEFAULTKEYS.Add("{0}", new int[] { 96 });//小键盘0
  120. _DEFAULTKEYS.Add("{1}", new int[] { 97 });//小键盘1
  121. _DEFAULTKEYS.Add("{2}", new int[] { 98 });//小键盘2
  122. _DEFAULTKEYS.Add("{3}", new int[] { 99 });//小键盘3
  123. _DEFAULTKEYS.Add("{4}", new int[] { 100 });//小键盘4
  124. _DEFAULTKEYS.Add("{5}", new int[] { 101 });//小键盘5
  125. _DEFAULTKEYS.Add("{6}", new int[] { 102 });//小键盘6
  126. _DEFAULTKEYS.Add("{7}", new int[] { 103 });//小键盘7
  127. _DEFAULTKEYS.Add("{8}", new int[] { 104 });//小键盘8
  128. _DEFAULTKEYS.Add("{9}", new int[] { 105 });//小键盘9
  129. _DEFAULTKEYS.Add("{.}", new int[] { 110 });//小键盘.
  130. _DEFAULTKEYS.Add("{*}", new int[] { 106 });//小键盘*
  131. _DEFAULTKEYS.Add("{+}", new int[] { 107 });//小键盘+
  132. _DEFAULTKEYS.Add("{-}", new int[] { 109 });//小键盘-
  133. _DEFAULTKEYS.Add("{/}", new int[] { 111 });//小键盘/
  134. //功能键 F1-F12
  135. _DEFAULTKEYS.Add("{F1}", new int[] { 112 });//F1
  136. _DEFAULTKEYS.Add("{F2}", new int[] { 113 });//F2
  137. _DEFAULTKEYS.Add("{F3}", new int[] { 114 });//F3
  138. _DEFAULTKEYS.Add("{F4}", new int[] { 115 });//F4
  139. _DEFAULTKEYS.Add("{F5}", new int[] { 116 });//F5
  140. _DEFAULTKEYS.Add("{F6}", new int[] { 117 });//F6
  141. _DEFAULTKEYS.Add("{F7}", new int[] { 118 });//F7
  142. _DEFAULTKEYS.Add("{F8}", new int[] { 119 });//F8
  143. _DEFAULTKEYS.Add("{F9}", new int[] { 120 });//F9
  144. _DEFAULTKEYS.Add("{F10}", new int[] { 121 });//F10
  145. _DEFAULTKEYS.Add("{F11}", new int[] { 122 });//F11
  146. _DEFAULTKEYS.Add("{F12}", new int[] { 123 });//F12
  147. _DEFAULTKEYS.Add("{VK_NUMLOCK}", new int[] { 144 });//Num Lock键
  148. _DEFAULTKEYS.Add("{VK_SCROLL}", new int[] { 145 });//Scroll Lock键
  149. //字母
  150. _DEFAULTKEYS.Add("{a}", new int[] { 65 });
  151. _DEFAULTKEYS.Add("{b}", new int[] { 66 });
  152. _DEFAULTKEYS.Add("{c}", new int[] { 67 });
  153. _DEFAULTKEYS.Add("{d}", new int[] { 68 });
  154. _DEFAULTKEYS.Add("{e}", new int[] { 69 });
  155. _DEFAULTKEYS.Add("{f}", new int[] { 70 });
  156. _DEFAULTKEYS.Add("{g}", new int[] { 71 });
  157. _DEFAULTKEYS.Add("{h}", new int[] { 72 });
  158. _DEFAULTKEYS.Add("{i}", new int[] { 73 });
  159. _DEFAULTKEYS.Add("{j}", new int[] { 74 });
  160. _DEFAULTKEYS.Add("{k}", new int[] { 75 });
  161. _DEFAULTKEYS.Add("{l}", new int[] { 76 });
  162. _DEFAULTKEYS.Add("{m}", new int[] { 77 });
  163. _DEFAULTKEYS.Add("{n}", new int[] { 78 });
  164. _DEFAULTKEYS.Add("{o}", new int[] { 79 });
  165. _DEFAULTKEYS.Add("{p}", new int[] { 80 });
  166. _DEFAULTKEYS.Add("{q}", new int[] { 81 });
  167. _DEFAULTKEYS.Add("{r}", new int[] { 82 });
  168. _DEFAULTKEYS.Add("{s}", new int[] { 83 });
  169. _DEFAULTKEYS.Add("{t}", new int[] { 84 });
  170. _DEFAULTKEYS.Add("{u}", new int[] { 85 });
  171. _DEFAULTKEYS.Add("{v}", new int[] { 86 });
  172. _DEFAULTKEYS.Add("{w}", new int[] { 87 });
  173. _DEFAULTKEYS.Add("{x}", new int[] { 88 });
  174. _DEFAULTKEYS.Add("{y}", new int[] { 89 });
  175. _DEFAULTKEYS.Add("{z}", new int[] { 90 });
  176. _DEFAULTKEYS.Add("{A}", new int[] { 16, 65 });
  177. _DEFAULTKEYS.Add("{B}", new int[] { 16, 66 });
  178. _DEFAULTKEYS.Add("{C}", new int[] { 16, 67 });
  179. _DEFAULTKEYS.Add("{D}", new int[] { 16, 68 });
  180. _DEFAULTKEYS.Add("{E}", new int[] { 16, 69 });
  181. _DEFAULTKEYS.Add("{F}", new int[] { 16, 70 });
  182. _DEFAULTKEYS.Add("{G}", new int[] { 16, 71 });
  183. _DEFAULTKEYS.Add("{H}", new int[] { 16, 72 });
  184. _DEFAULTKEYS.Add("{I}", new int[] { 16, 73 });
  185. _DEFAULTKEYS.Add("{J}", new int[] { 16, 74 });
  186. _DEFAULTKEYS.Add("{K}", new int[] { 16, 75 });
  187. _DEFAULTKEYS.Add("{L}", new int[] { 16, 76 });
  188. _DEFAULTKEYS.Add("{M}", new int[] { 16, 77 });
  189. _DEFAULTKEYS.Add("{N}", new int[] { 16, 78 });
  190. _DEFAULTKEYS.Add("{O}", new int[] { 16, 79 });
  191. _DEFAULTKEYS.Add("{P}", new int[] { 16, 80 });
  192. _DEFAULTKEYS.Add("{Q}", new int[] { 16, 81 });
  193. _DEFAULTKEYS.Add("{R}", new int[] { 16, 82 });
  194. _DEFAULTKEYS.Add("{S}", new int[] { 16, 83 });
  195. _DEFAULTKEYS.Add("{T}", new int[] { 16, 84 });
  196. _DEFAULTKEYS.Add("{U}", new int[] { 16, 85 });
  197. _DEFAULTKEYS.Add("{V}", new int[] { 16, 86 });
  198. _DEFAULTKEYS.Add("{W}", new int[] { 16, 87 });
  199. _DEFAULTKEYS.Add("{X}", new int[] { 16, 88 });
  200. _DEFAULTKEYS.Add("{Y}", new int[] { 16, 89 });
  201. _DEFAULTKEYS.Add("{Z}", new int[] { 16, 90 });
  202. //符号
  203. _DEFAULTKEYS.Add("{;}", new int[] { 186 });
  204. _DEFAULTKEYS.Add("{=}", new int[] { 187 });
  205. _DEFAULTKEYS.Add("{,}", new int[] { 188 });
  206. //_DEFAULTKEYS.Add("{-}", new int[] { 189 });
  207. //_DEFAULTKEYS.Add("{.}", new int[] { 190 });
  208. //_DEFAULTKEYS.Add("{/}", new int[] { 191 });
  209. _DEFAULTKEYS.Add("{`}", new int[] { 192 });
  210. _DEFAULTKEYS.Add("{[}", new int[] { 219 });
  211. //_DEFAULTKEYS.Add("{/}", new int[] { 220 });
  212. _DEFAULTKEYS.Add("{]}", new int[] { 221 });
  213. _DEFAULTKEYS.Add("{'}", new int[] { 222 });
  214. _DEFAULTKEYS.Add("{:}", new int[] { 16, 186 });
  215. //_DEFAULTKEYS.Add("{+}", new int[] { 16, 187 });
  216. _DEFAULTKEYS.Add("{<}", new int[] { 16, 188 });
  217. _DEFAULTKEYS.Add("{_}", new int[] { 16, 189 });
  218. _DEFAULTKEYS.Add("{>}", new int[] { 16, 190 });
  219. _DEFAULTKEYS.Add("{?}", new int[] { 16, 191 });
  220. _DEFAULTKEYS.Add("{~}", new int[] { 16, 192 });
  221. _DEFAULTKEYS.Add("{{}", new int[] { 16, 219 });
  222. _DEFAULTKEYS.Add("{|}", new int[] { 16, 220 });
  223. _DEFAULTKEYS.Add("{}}", new int[] { 16, 221 });
  224. _DEFAULTKEYS.Add("{\"}", new int[] { 16, 222 });
  225. //数字上面的符号
  226. _DEFAULTKEYS.Add("{!}", new int[] { 16, 49 });
  227. _DEFAULTKEYS.Add("{@}", new int[] { 16, 50 });
  228. _DEFAULTKEYS.Add("{#}", new int[] { 16, 51 });
  229. _DEFAULTKEYS.Add("{$}", new int[] { 16, 52 });
  230. _DEFAULTKEYS.Add("{%}", new int[] { 16, 53 });
  231. _DEFAULTKEYS.Add("{^}", new int[] { 16, 54 });
  232. _DEFAULTKEYS.Add("{&}", new int[] { 16, 55 });
  233. //_DEFAULTKEYS.Add("{*}", new int[] { 16, 56 });
  234. _DEFAULTKEYS.Add("{(}", new int[] { 16, 57 });
  235. _DEFAULTKEYS.Add("{)}", new int[] { 16, 48 });
  236. }
  237. }
  238. }
  239. /*
  240. VK_BACK: 8, //退格键
  241. VK_TAB: 9, //TAB键
  242. VK_RETURN: 13, //回车键
  243. VK_SHIFT: 16, //Shift键
  244. VK_CONTROL: 17, //Ctrl键
  245. VK_MENU: 18, //Alt键
  246. VK_PAUSE: 19, //Pause Break键
  247. VK_CAPITAL: 20, //Caps Lock键
  248. VK_SPACE: 32, //空格键
  249. VK_PRIOR: 33, //Page Up
  250. VK_NEXT: 34, //Page Down
  251. VK_END: 35, //End键
  252. VK_HOME: 36, //Home键
  253. VK_LEFT: 37, //方向键:←
  254. VK_UP: 38, //方向键:↑
  255. VK_RIGHT: 39, //方向键:→
  256. VK_DOWN: 40, //方向键:↓
  257. VK_INSERT: 45, //Insert键
  258. VK_DELETE: 46, //Delete键
  259. //字母表
  260. VK_A: 65,
  261. VK_B: 66,
  262. VK_C: 67,
  263. VK_D: 68,
  264. VK_E: 69,
  265. VK_F: 70,
  266. VK_G: 71,
  267. VK_H: 72,
  268. VK_I: 73,
  269. VK_J: 74,
  270. VK_K: 75,
  271. VK_L: 76,
  272. VK_M: 77,
  273. VK_N: 78,
  274. VK_O: 79,
  275. VK_P: 80,
  276. VK_Q: 81,
  277. VK_R: 82,
  278. VK_S: 83,
  279. VK_T: 84,
  280. VK_U: 85,
  281. VK_V: 86,
  282. VK_W: 87,
  283. VK_X: 88,
  284. VK_Y: 89,
  285. VK_Z: 90,
  286. VK_LWIN: 91, //左徽标键
  287. VK_RWIN: 92, //右徽标键
  288. VK_APPS: 93, //鼠标右键快捷键
  289. VK_NUMPAD0: 96, //小键盘0
  290. VK_NUMPAD0: 97, //小键盘1
  291. VK_NUMPAD0: 98, //小键盘2
  292. VK_NUMPAD0: 99, //小键盘3
  293. VK_NUMPAD0: 100, //小键盘4
  294. VK_NUMPAD0: 101, //小键盘5
  295. VK_NUMPAD0: 102, //小键盘6
  296. VK_NUMPAD0: 103, //小键盘7
  297. VK_NUMPAD0: 104, //小键盘8
  298. VK_NUMPAD0: 105, //小键盘9
  299. VK_DECIMAL: 110, //小键盘.
  300. VK_MULTIPLY: 106, //小键盘*
  301. VK_MULTIPLY: 107, //小键盘+
  302. VK_SUBTRACT: 109, //小键盘-
  303. VK_DIVIDE: 111, //小键盘/
  304. VK_F1: 112, //F1键
  305. VK_F2: 113, //F2键
  306. VK_F3: 114, //F3键
  307. VK_F4: 115, //F4键
  308. VK_F5: 116, //F5键
  309. VK_F6: 117, //F6键
  310. VK_F7: 118, //F7键
  311. VK_F8: 119, //F8键
  312. VK_F9: 120, //F9键
  313. VK_F10: 121, //F10键
  314. VK_F11: 122, //F11键
  315. VK_F12: 123, //F12键
  316. VK_NUMLOCK: 144, //Num Lock键
  317. VK_SCROLL: 145, //Scroll Lock键
  318. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/272516
推荐阅读
相关标签
  

闽ICP备14008679号