当前位置:   article > 正文

C#鼠标键盘操作用于桌面脚本_c# 后台鼠键

c# 后台鼠键

项目中添加这两个类KeyBord ,MoseKeyboard(文末)

对于有些软件获取不到具体控件的句柄,那就只能用鼠标键盘操作了

比如想执行一个粘贴操作,可以这样写,通常需要在新起的线程中写,不然复制到粘贴板那会报错

  1. Clipboard.SetDataObject("123456", true);//复制到粘贴板
  2. MoseKeyboard.ExcuteCmd("1000,Mouse,1597,442,LeftButtonClick"); //点击文本框
  3. MoseKeyboard.ExcuteCmd("1000,KeyBord,Control+V");//粘贴

起线程传入数组参数示例

  1. string[] array = new string[]
  2. {
  3. "100,Mouse,1017,242,LeftButtonClick",
  4. "1000,Mouse,891,514,RightButtonClick"
  5. };
  6. Thread ThreadPause = new Thread(new ParameterizedThreadStart(RunTest));
  7. ThreadPause.Start(array);
  8. private void RunTest(object obj)
  9. {
  10. string[] strArray = (string[])obj;
  11. }

传入的string参数如为分别为,时间间隔,操作类型,屏幕坐标xy,操作,

这里用按键精灵显示(我的资源中有https://download.csdn.net/download/m0_37137902/14900615)

通常脚本需要截取字符串,比如截取 ABC中A和C之间的B,可以用下面这个方法

string str = GetStringOfStartAndEnd()("A", "B", "ABC");
  1. public Func<string, string, string, string> GetStringOfStartAndEnd()
  2. {
  3. return (x, y, z) =>
  4. {
  5. string str = string.Empty;
  6. int start = z.IndexOf(x);
  7. int end = z.IndexOf(y);
  8. if (end > start)
  9. {
  10. str = z.Substring(start + x.Length, end - start - x.Length);
  11. }
  12. return str;
  13. };
  14. }

直接取到末尾用这个方法,比如想得到abcd中,b以后的字段(当然这两个方法没啥难度,经常用就写到这方便后面复制)

string str = GetStringOfStartAndEndHttp()("abcd", "b");
  1. public Func<string, string, string> GetStringOfStartAndEndHttp()
  2. {
  3. return (x, y) =>
  4. {
  5. string str = string.Empty;
  6. int start = x.IndexOf(y) + y.Length;
  7. int length = x.Length - start;
  8. str = x.Substring(start, length);
  9. return str;
  10. };
  11. }

脚本通常也需要获取url返回的值

  1. public string HttpGet(string url)
  2. {
  3. Encoding encoding = Encoding.UTF8;
  4. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  5. request.Method = "GET";
  6. request.Accept = "text/html, application/xhtml+xml, */*";
  7. request.ContentType = "application/json";
  8. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  9. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  10. {
  11. return reader.ReadToEnd();
  12. }
  13. }

经常会把执行命令放在txt里,可以用数组的形式读取出来

string[] array=File.ReadAllLines(Application.StartupPath + @"\mytxt.txt", Encoding.Default);

按键枚举类

  1. public enum KeyBord : int
  2. {
  3. // 摘要:
  4. // 从键值提取修饰符的位屏蔽。
  5. Modifiers = -65536,
  6. //
  7. // 摘要:
  8. // Backspace 键。
  9. Backspace = 8,
  10. //
  11. // 摘要:
  12. // Tab 键。
  13. Tab = 9,
  14. //
  15. // 摘要:
  16. // LINEFEED 键。
  17. LineFeed = 10,
  18. //
  19. // 摘要:
  20. // Clear 键。
  21. Clear = 12,
  22. //
  23. // 摘要:
  24. // Enter 键。
  25. Enter = 13,
  26. //
  27. // 摘要:
  28. // Return 键。
  29. Return = 13,
  30. //
  31. // 摘要:
  32. // Shift 键。
  33. ShiftKey = 16,
  34. //
  35. // 摘要:
  36. // Ctrl 键。
  37. ControlKey = 17,
  38. //
  39. // 摘要:
  40. // Alt 键。
  41. Menu = 18,
  42. //
  43. // 摘要:
  44. // Pause 键。
  45. Pause = 19,
  46. //
  47. // 摘要:
  48. // Caps Lock 键。
  49. CapsLock = 20,
  50. //
  51. // 摘要:
  52. // Caps Lock 键。
  53. Capital = 20,
  54. //
  55. // 摘要:
  56. // IME Kana 模式键。
  57. KanaMode = 21,
  58. //
  59. // 摘要:
  60. // IME Hanguel 模式键。(为了保持兼容性而设置;使用 HangulMode)
  61. HanguelMode = 21,
  62. //
  63. // 摘要:
  64. // IME Hangul 模式键。
  65. HangulMode = 21,
  66. //
  67. // 摘要:
  68. // IME Junja 模式键。
  69. JunjaMode = 23,
  70. //
  71. // 摘要:
  72. // IME 最终模式键。
  73. FinalMode = 24,
  74. //
  75. // 摘要:
  76. // IME Kanji 模式键。
  77. KanjiMode = 25,
  78. //
  79. // 摘要:
  80. // IME Hanja 模式键。
  81. HanjaMode = 25,
  82. //
  83. // 摘要:
  84. // Esc 键。
  85. Escape = 27,
  86. //
  87. // 摘要:
  88. // IME 转换键。
  89. IMEConvert = 28,
  90. //
  91. // 摘要:
  92. // IME 非转换键。
  93. IMENonconvert = 29,
  94. //
  95. // 摘要:
  96. // IME 接受键。已过时,请改用 System.Windows.Forms.Keys.IMEAccept。
  97. IMEAceept = 30,
  98. //
  99. // 摘要:
  100. // IME 接受键,替换 System.Windows.Forms.Keys.IMEAceept。
  101. IMEAccept = 30,
  102. //
  103. // 摘要:
  104. // IME 模式更改键。
  105. IMEModeChange = 31,
  106. //
  107. // 摘要:
  108. // 空格键。
  109. Space = 32,
  110. //
  111. // 摘要:
  112. // Page Up 键。
  113. Prior = 33,
  114. //
  115. // 摘要:
  116. // Page Up 键。
  117. PageUp = 33,
  118. //
  119. // 摘要:
  120. // Page Down 键。
  121. Next = 34,
  122. //
  123. // 摘要:
  124. // Page Down 键。
  125. PageDown = 34,
  126. //
  127. // 摘要:
  128. // End 键。
  129. End = 35,
  130. //
  131. // 摘要:
  132. // Home 键。
  133. Home = 36,
  134. //
  135. // 摘要:
  136. // 向左键。
  137. Left = 37,
  138. //
  139. // 摘要:
  140. // 向上键。
  141. Up = 38,
  142. //
  143. // 摘要:
  144. // 向右键。
  145. Right = 39,
  146. //
  147. // 摘要:
  148. // 向下键。
  149. Down = 40,
  150. //
  151. // 摘要:
  152. // Select 键。
  153. Select = 41,
  154. //
  155. // 摘要:
  156. // Print 键。
  157. Print = 42,
  158. //
  159. // 摘要:
  160. // EXECUTE 键。
  161. Execute = 43,
  162. //
  163. // 摘要:
  164. // Print Screen 键。
  165. PrintScreen = 44,
  166. //
  167. // 摘要:
  168. // Print Screen 键。
  169. Snapshot = 44,
  170. //
  171. // 摘要:
  172. // Ins 键。
  173. Insert = 45,
  174. //
  175. // 摘要:
  176. // DeL 键。
  177. Delete = 46,
  178. //
  179. // 摘要:
  180. // Help 键。
  181. Help = 47,
  182. //
  183. // 摘要:
  184. // 0 键。
  185. D0 = 48,
  186. //
  187. // 摘要:
  188. // 1 键。
  189. D1 = 49,
  190. //
  191. // 摘要:
  192. // 2 键。
  193. D2 = 50,
  194. //
  195. // 摘要:
  196. // 3 键。
  197. D3 = 51,
  198. //
  199. // 摘要:
  200. // 4 键。
  201. D4 = 52,
  202. //
  203. // 摘要:
  204. // 5 键。
  205. D5 = 53,
  206. //
  207. // 摘要:
  208. // 6 键。
  209. D6 = 54,
  210. //
  211. // 摘要:
  212. // 7 键。
  213. D7 = 55,
  214. //
  215. // 摘要:
  216. // 8 键。
  217. D8 = 56,
  218. //
  219. // 摘要:
  220. // 9 键。
  221. D9 = 57,
  222. //
  223. // 摘要:
  224. // A 键。
  225. A = 65,
  226. //
  227. // 摘要:
  228. // B 键。
  229. B = 66,
  230. //
  231. // 摘要:
  232. // C 键。
  233. C = 67,
  234. //
  235. // 摘要:
  236. // D 键。
  237. D = 68,
  238. //
  239. // 摘要:
  240. // E 键。
  241. E = 69,
  242. //
  243. // 摘要:
  244. // F 键。
  245. F = 70,
  246. //
  247. // 摘要:
  248. // G 键。
  249. G = 71,
  250. //
  251. // 摘要:
  252. // H 键。
  253. H = 72,
  254. //
  255. // 摘要:
  256. // I 键。
  257. I = 73,
  258. //
  259. // 摘要:
  260. // J 键。
  261. J = 74,
  262. //
  263. // 摘要:
  264. // K 键。
  265. K = 75,
  266. //
  267. // 摘要:
  268. // L 键。
  269. L = 76,
  270. //
  271. // 摘要:
  272. // M 键。
  273. M = 77,
  274. //
  275. // 摘要:
  276. // N 键。
  277. N = 78,
  278. //
  279. // 摘要:
  280. // O 键。
  281. O = 79,
  282. //
  283. // 摘要:
  284. // P 键。
  285. P = 80,
  286. //
  287. // 摘要:
  288. // Q 键。
  289. Q = 81,
  290. //
  291. // 摘要:
  292. // R 键。
  293. R = 82,
  294. //
  295. // 摘要:
  296. // S 键。
  297. S = 83,
  298. //
  299. // 摘要:
  300. // T 键。
  301. T = 84,
  302. //
  303. // 摘要:
  304. // U 键。
  305. U = 85,
  306. //
  307. // 摘要:
  308. // V 键。
  309. V = 86,
  310. //
  311. // 摘要:
  312. // W 键。
  313. W = 87,
  314. //
  315. // 摘要:
  316. // X 键。
  317. X = 88,
  318. //
  319. // 摘要:
  320. // Y 键。
  321. Y = 89,
  322. //
  323. // 摘要:
  324. // Z 键。
  325. Z = 90,
  326. //
  327. // 摘要:
  328. // 左 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。
  329. LWin = 91,
  330. //
  331. // 摘要:
  332. // 右 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。
  333. RWin = 92,
  334. //
  335. // 摘要:
  336. // 应用程序键(Microsoft Natural Keyboard,人体工程学键盘)。
  337. Apps = 93,
  338. //
  339. // 摘要:
  340. // 计算机睡眠键。
  341. Sleep = 95,
  342. //
  343. // 摘要:
  344. // 数字键盘上的 0 键。
  345. NumPad0 = 96,
  346. //
  347. // 摘要:
  348. // 数字键盘上的 1 键。
  349. NumPad1 = 97,
  350. //
  351. // 摘要:
  352. // 数字键盘上的 2 键。
  353. NumPad2 = 98,
  354. //
  355. // 摘要:
  356. // 数字键盘上的 3 键。
  357. NumPad3 = 99,
  358. //
  359. // 摘要:
  360. // 数字键盘上的 4 键。
  361. NumPad4 = 100,
  362. //
  363. // 摘要:
  364. // 数字键盘上的 5 键。
  365. NumPad5 = 101,
  366. //
  367. // 摘要:
  368. // 数字键盘上的 6 键。
  369. NumPad6 = 102,
  370. //
  371. // 摘要:
  372. // 数字键盘上的 7 键。
  373. NumPad7 = 103,
  374. //
  375. // 摘要:
  376. // 数字键盘上的 8 键。
  377. NumPad8 = 104,
  378. //
  379. // 摘要:
  380. // 数字键盘上的 9 键。
  381. NumPad9 = 105,
  382. //
  383. // 摘要:
  384. // 乘号键。
  385. Multiply = 106,
  386. //
  387. // 摘要:
  388. // 加号键。
  389. Add = 107,
  390. //
  391. // 摘要:
  392. // 分隔符键。
  393. Separator = 108,
  394. //
  395. // 摘要:
  396. // 减号键。
  397. Subtract = 109,
  398. //
  399. // 摘要:
  400. // 句点键。
  401. Decimal = 110,
  402. //
  403. // 摘要:
  404. // 除号键。
  405. Divide = 111,
  406. //
  407. // 摘要:
  408. // F1 键。
  409. F1 = 112,
  410. //
  411. // 摘要:
  412. // F2 键。
  413. F2 = 113,
  414. //
  415. // 摘要:
  416. // F3 键。
  417. F3 = 114,
  418. //
  419. // 摘要:
  420. // F4 键。
  421. F4 = 115,
  422. //
  423. // 摘要:
  424. // F5 键。
  425. F5 = 116,
  426. //
  427. // 摘要:
  428. // F6 键。
  429. F6 = 117,
  430. //
  431. // 摘要:
  432. // F7 键。
  433. F7 = 118,
  434. //
  435. // 摘要:
  436. // F8 键。
  437. F8 = 119,
  438. //
  439. // 摘要:
  440. // F9 键。
  441. F9 = 120,
  442. //
  443. // 摘要:
  444. // F10 键。
  445. F10 = 121,
  446. //
  447. // 摘要:
  448. // F11 键。
  449. F11 = 122,
  450. //
  451. // 摘要:
  452. // F12 键。
  453. F12 = 123,
  454. //
  455. // 摘要:
  456. // F13 键。
  457. F13 = 124,
  458. //
  459. // 摘要:
  460. // F14 键。
  461. F14 = 125,
  462. //
  463. // 摘要:
  464. // F15 键。
  465. F15 = 126,
  466. //
  467. // 摘要:
  468. // F16 键。
  469. F16 = 127,
  470. //
  471. // 摘要:
  472. // F17 键。
  473. F17 = 128,
  474. //
  475. // 摘要:
  476. // F18 键。
  477. F18 = 129,
  478. //
  479. // 摘要:
  480. // F19 键。
  481. F19 = 130,
  482. //
  483. // 摘要:
  484. // F20 键。
  485. F20 = 131,
  486. //
  487. // 摘要:
  488. // F21 键。
  489. F21 = 132,
  490. //
  491. // 摘要:
  492. // F22 键。
  493. F22 = 133,
  494. //
  495. // 摘要:
  496. // F23 键。
  497. F23 = 134,
  498. //
  499. // 摘要:
  500. // F24 键。
  501. F24 = 135,
  502. //
  503. // 摘要:
  504. // Num Lock 键。
  505. NumLock = 144,
  506. //
  507. // 摘要:
  508. // Scroll Lock 键。
  509. Scroll = 145,
  510. //
  511. // 摘要:
  512. // 左 Shift 键。
  513. LShiftKey = 160,
  514. //
  515. // 摘要:
  516. // 右 Shift 键。
  517. RShiftKey = 161,
  518. //
  519. // 摘要:
  520. // 左 Ctrl 键。
  521. LControlKey = 162,
  522. //
  523. // 摘要:
  524. // 右 Ctrl 键。
  525. RControlKey = 163,
  526. //
  527. // 摘要:
  528. // 左 Alt 键。
  529. LMenu = 164,
  530. //
  531. // 摘要:
  532. // 右 Alt 键。
  533. RMenu = 165,
  534. //
  535. // 摘要:
  536. // 浏览器后退键(Windows 2000 或更高版本)。
  537. BrowserBack = 166,
  538. //
  539. // 摘要:
  540. // 浏览器前进键(Windows 2000 或更高版本)。
  541. BrowserForward = 167,
  542. //
  543. // 摘要:
  544. // 浏览器刷新键(Windows 2000 或更高版本)。
  545. BrowserRefresh = 168,
  546. //
  547. // 摘要:
  548. // 浏览器停止键(Windows 2000 或更高版本)。
  549. BrowserStop = 169,
  550. //
  551. // 摘要:
  552. // 浏览器搜索键(Windows 2000 或更高版本)。
  553. BrowserSearch = 170,
  554. //
  555. // 摘要:
  556. // 浏览器收藏夹键(Windows 2000 或更高版本)。
  557. BrowserFavorites = 171,
  558. //
  559. // 摘要:
  560. // 浏览器主页键(Windows 2000 或更高版本)。
  561. BrowserHome = 172,
  562. //
  563. // 摘要:
  564. // 静音键(Windows 2000 或更高版本)。
  565. VolumeMute = 173,
  566. //
  567. // 摘要:
  568. // 减小音量键(Windows 2000 或更高版本)。
  569. VolumeDown = 174,
  570. //
  571. // 摘要:
  572. // 增大音量键(Windows 2000 或更高版本)。
  573. VolumeUp = 175,
  574. //
  575. // 摘要:
  576. // 媒体下一曲目键(Windows 2000 或更高版本)。
  577. MediaNextTrack = 176,
  578. //
  579. // 摘要:
  580. // 媒体上一曲目键(Windows 2000 或更高版本)。
  581. MediaPreviousTrack = 177,
  582. //
  583. // 摘要:
  584. // 媒体停止键(Windows 2000 或更高版本)。
  585. MediaStop = 178,
  586. //
  587. // 摘要:
  588. // 媒体播放暂停键(Windows 2000 或更高版本)。
  589. MediaPlayPause = 179,
  590. //
  591. // 摘要:
  592. // 启动邮件键(Windows 2000 或更高版本)。
  593. LaunchMail = 180,
  594. //
  595. // 摘要:
  596. // 选择媒体键(Windows 2000 或更高版本)。
  597. SelectMedia = 181,
  598. //
  599. // 摘要:
  600. // 启动应用程序一键(Windows 2000 或更高版本)。
  601. LaunchApplication1 = 182,
  602. //
  603. // 摘要:
  604. // 启动应用程序二键(Windows 2000 或更高版本)。
  605. LaunchApplication2 = 183,
  606. //
  607. // 摘要:
  608. // OEM 1 键。
  609. Oem1 = 186,
  610. //
  611. // 摘要:
  612. // 美式标准键盘上的 OEM 分号键(Windows 2000 或更高版本)。
  613. OemSemicolon = 186,
  614. //
  615. // 摘要:
  616. // 任何国家/地区键盘上的 OEM 加号键(Windows 2000 或更高版本)。
  617. Oemplus = 187,
  618. //
  619. // 摘要:
  620. // 任何国家/地区键盘上的 OEM 逗号键(Windows 2000 或更高版本)。
  621. Oemcomma = 188,
  622. //
  623. // 摘要:
  624. // 任何国家/地区键盘上的 OEM 减号键(Windows 2000 或更高版本)。
  625. OemMinus = 189,
  626. //
  627. // 摘要:
  628. // 任何国家/地区键盘上的 OEM 句点键(Windows 2000 或更高版本)。
  629. OemPeriod = 190,
  630. //
  631. // 摘要:
  632. // 美式标准键盘上的 OEM 问号键(Windows 2000 或更高版本)。
  633. OemQuestion = 191,
  634. //
  635. // 摘要:
  636. // OEM 2 键。
  637. Oem2 = 191,
  638. //
  639. // 摘要:
  640. // 美式标准键盘上的 OEM 波形符键(Windows 2000 或更高版本)。
  641. Oemtilde = 192,
  642. //
  643. // 摘要:
  644. // OEM 3 键。
  645. Oem3 = 192,
  646. //
  647. // 摘要:
  648. // OEM 4 键。
  649. Oem4 = 219,
  650. //
  651. // 摘要:
  652. // 美式标准键盘上的 OEM 左括号键(Windows 2000 或更高版本)。
  653. OemOpenBrackets = 219,
  654. //
  655. // 摘要:
  656. // 美式标准键盘上的 OEM 管道键(Windows 2000 或更高版本)。
  657. OemPipe = 220,
  658. //
  659. // 摘要:
  660. // OEM 5 键。
  661. Oem5 = 220,
  662. //
  663. // 摘要:
  664. // OEM 6 键。
  665. Oem6 = 221,
  666. //
  667. // 摘要:
  668. // 美式标准键盘上的 OEM 右括号键(Windows 2000 或更高版本)。
  669. OemCloseBrackets = 221,
  670. //
  671. // 摘要:
  672. // OEM 7 键。
  673. Oem7 = 222,
  674. //
  675. // 摘要:
  676. // 美式标准键盘上的 OEM 单/双引号键(Windows 2000 或更高版本)。
  677. OemQuotes = 222,
  678. //
  679. // 摘要:
  680. // OEM 8 键。
  681. Oem8 = 223,
  682. //
  683. // 摘要:
  684. // OEM 102 键。
  685. Oem102 = 226,
  686. //
  687. // 摘要:
  688. // RT 102 键的键盘上的 OEM 尖括号或反斜杠键(Windows 2000 或更高版本)。
  689. OemBackslash = 226,
  690. //
  691. // 摘要:
  692. // Process Key 键。
  693. ProcessKey = 229,
  694. //
  695. // 摘要:
  696. // 用于将 Unicode 字符当作键击传递。Packet 键值是用于非键盘输入法的 32 位虚拟键值的低位字。
  697. Packet = 231,
  698. //
  699. // 摘要:
  700. // Attn 键。
  701. Attn = 246,
  702. //
  703. // 摘要:
  704. // Crsel 键。
  705. Crsel = 247,
  706. //
  707. // 摘要:
  708. // Exsel 键。
  709. Exsel = 248,
  710. //
  711. // 摘要:
  712. // ERASE EOF 键。
  713. EraseEof = 249,
  714. //
  715. // 摘要:
  716. // Play 键。
  717. Play = 250,
  718. //
  719. // 摘要:
  720. // Zoom 键。
  721. Zoom = 251,
  722. //
  723. // 摘要:
  724. // 保留以备将来使用的常数。
  725. NoName = 252,
  726. //
  727. // 摘要:
  728. // PA1 键。
  729. Pa1 = 253,
  730. //
  731. // 摘要:
  732. // Clear 键。
  733. OemClear = 254,
  734. //
  735. // 摘要:
  736. // 从键值提取键代码的位屏蔽。
  737. KeyCode = 65535,
  738. //
  739. // 摘要:
  740. // Shift 修改键。
  741. Shift = 65536,
  742. //
  743. // 摘要:
  744. // Ctrl 修改键。
  745. Control = 131072,
  746. //
  747. // 摘要:
  748. // Alt 修改键。
  749. Alt = 262144,
  750. }

键盘操作MoseKeyboard类

  1. class MoseKeyboard
  2. {
  3. [DllImport("user32")]
  4. public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
  5. [DllImport("user32.dll")]
  6. static extern bool SetCursorPos(int X, int Y);
  7. const uint KEYEVENTF_EXTENDEDKEY = 0x1;
  8. const uint KEYEVENTF_KEYUP = 0x2;
  9. [DllImport("user32.dll")]
  10. static extern short GetKeyState(int nVirtKey);
  11. [DllImport("user32.dll")]
  12. static extern void keybd_event(
  13. byte bVk,
  14. byte bScan,
  15. uint dwFlags,
  16. uint dwExtraInfo
  17. );
  18. public static int x = 0;
  19. public static int y = 0;
  20. public enum VirtualKeys : byte
  21. {
  22. VK_NUMLOCK = 0x90, //数字锁定键
  23. VK_SCROLL = 0x91, //滚动锁定
  24. VK_CAPITAL = 0x14, //大小写锁定
  25. VK_A = 62,
  26. VK_LEFT = 37, //左箭头
  27. VK_UP = 38,//上箭头
  28. VK_RIGHT = 39,// 右箭头
  29. VK_DOWN = 40,//  下箭头
  30. VK_RETURN = 13, //回车
  31. VK_DELETE = Keys.Delete
  32. }
  33. private enum MouseOper : byte
  34. {
  35. MOVE = 0x0001, /* mouse move */
  36. LEFTDOWN = 0x0002, /* left button down */
  37. LEFTUP = 0x0004, /* left button up */
  38. RIGHTDOWN = 0x0008, /* right button down */
  39. RIGHTUP = 0x0010,
  40. }
  41. public static bool GetState(KeyBord Key)
  42. {
  43. return (GetKeyState((int)Key) == 1);
  44. }
  45. private static void SetState(KeyBord Key, bool State)
  46. {
  47. if (State != GetState(Key))
  48. {
  49. keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
  50. keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  51. }
  52. }
  53. public static void LeftButtonClick(int x, int y)//左键单击
  54. {
  55. SetCurPos(x, y);
  56. mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
  57. }
  58. public static void LeftButtonDoubleClick(int x, int y)//左键双击
  59. {
  60. SetCurPos(x, y);
  61. mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
  62. mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
  63. }
  64. public static void RightButtonClick(int x, int y)//右键单击
  65. {
  66. SetCurPos(x, y);
  67. mouse_event((int)(MouseOper.RIGHTDOWN | MouseOper.RIGHTUP), 0, 0, 0, 0);
  68. }
  69. public static void SetCurPos(int x, int y)
  70. {
  71. SetCursorPos(x, y);
  72. }
  73. public static void PressKey(KeyBord key)//模拟按键按下和弹起
  74. {
  75. keybd_event((byte)key, 0x45, 0, 0);//按键按下
  76. // Simulate a key release
  77. keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);//按键弹起
  78. }
  79. public static void Compositekey(KeyBord key1, KeyBord key2, KeyBord key3)
  80. {
  81. keybd_event((byte)key1, (byte)0, 0, 0);//key1按下
  82. keybd_event((byte)key1, (byte)0, 0, 0);//key2按下
  83. MoseKeyboard.PressKey(key3);//key3按下弹起
  84. keybd_event((byte)key2, (byte)0, KEYEVENTF_KEYUP, 0);//key2弹起
  85. keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起
  86. }
  87. public static void Compositekey(KeyBord key1, KeyBord key2)
  88. {
  89. keybd_event((byte)key1, (byte)0, 0, 0);//key1按下
  90. MoseKeyboard.PressKey(key2);//key3按下弹起
  91. keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起
  92. }
  93. public static bool ExcuteCmd(string cmd)//执行单行命令
  94. {
  95. if (cmd == "")
  96. return false;
  97. //cmd格式是:延时,类型,(鼠标X,Y),命令
  98. string[] str = cmd.Split(',');//分解命令
  99. if (str.Length < 3 || str.Length > 5)//命令是否正确
  100. return false;
  101. try
  102. {
  103. Thread.Sleep(int.Parse(str[0]));//延时操作
  104. if (str[1] == "KeyBord")//键盘操作
  105. {
  106. string[] keys = str[2].Split('+');//分解按键
  107. if (keys.Length == 1)//单个健按下
  108. {
  109. MoseKeyboard.PressKey(GetKeyByString(keys[0]));
  110. return true;
  111. }
  112. if (keys.Length == 2)//2个复合键按下
  113. {
  114. MoseKeyboard.Compositekey(GetKeyByString(keys[0]), GetKeyByString(keys[1]));
  115. return true;
  116. }
  117. if (keys.Length == 2)//3个复合键按下
  118. {
  119. MoseKeyboard.Compositekey(GetKeyByString(keys[0]), GetKeyByString(keys[1]), GetKeyByString(keys[2]));
  120. return true;
  121. }
  122. return false;
  123. }
  124. if (str[1] == "Mouse")//鼠标操作
  125. {
  126. if (str[4] == "LeftButtonClick")//左键单击
  127. {
  128. MoseKeyboard.LeftButtonClick(int.Parse(str[2]), int.Parse(str[3]));
  129. return true;
  130. }
  131. if (str[4] == "LeftButtonDoubleClick")//左键双击
  132. {
  133. MoseKeyboard.LeftButtonDoubleClick(int.Parse(str[2]), int.Parse(str[3]));
  134. return true;
  135. }
  136. if (str[4] == "RightButtonClick")//右键单击
  137. {
  138. MoseKeyboard.RightButtonClick(int.Parse(str[2]), int.Parse(str[3]));
  139. return true;
  140. }
  141. return false;
  142. }
  143. return false;
  144. }
  145. catch (Exception ex)
  146. {
  147. return false;
  148. }
  149. }
  150. public static KeyBord GetKeyByString(string key)
  151. {
  152. if (key == "A")
  153. return KeyBord.A;
  154. if (key == "B")
  155. return KeyBord.B;
  156. if (key == "C")
  157. return KeyBord.C;
  158. if (key == "D")
  159. return KeyBord.D;
  160. if (key == "E")
  161. return KeyBord.E;
  162. if (key == "F")
  163. return KeyBord.F;
  164. if (key == "G")
  165. return KeyBord.G;
  166. if (key == "H")
  167. return KeyBord.H;
  168. if (key == "I")
  169. return KeyBord.I;
  170. if (key == "J")
  171. return KeyBord.J;
  172. if (key == "K")
  173. return KeyBord.K;
  174. if (key == "L")
  175. return KeyBord.L;
  176. if (key == "M")
  177. return KeyBord.M;
  178. if (key == "N")
  179. return KeyBord.N;
  180. if (key == "O")
  181. return KeyBord.O;
  182. if (key == "P")
  183. return KeyBord.P;
  184. if (key == "Q")
  185. return KeyBord.Q;
  186. if (key == "R")
  187. return KeyBord.R;
  188. if (key == "S")
  189. return KeyBord.S;
  190. if (key == "T")
  191. return KeyBord.T;
  192. if (key == "U")
  193. return KeyBord.U;
  194. if (key == "V")
  195. return KeyBord.V;
  196. if (key == "W")
  197. return KeyBord.W;
  198. if (key == "X")
  199. return KeyBord.X;
  200. if (key == "Y")
  201. return KeyBord.Y;
  202. if (key == "Z")
  203. return KeyBord.Z;//A-Z
  204. if (key == "F1")
  205. return KeyBord.F1;
  206. if (key == "F2")
  207. return KeyBord.F2;
  208. if (key == "F3")
  209. return KeyBord.F3;
  210. if (key == "F4")
  211. return KeyBord.F4;
  212. if (key == "F5")
  213. return KeyBord.F5;
  214. if (key == "F6")
  215. return KeyBord.F6;
  216. if (key == "F7")
  217. return KeyBord.F7;
  218. if (key == "F8")
  219. return KeyBord.F8;
  220. if (key == "F9")
  221. return KeyBord.F9;
  222. if (key == "F10")
  223. return KeyBord.F10;
  224. if (key == "F11")
  225. return KeyBord.F11;
  226. if (key == "F12")
  227. return KeyBord.F12;//F1-F12
  228. if (key == "Esc")
  229. return KeyBord.Escape;
  230. if (key == "0")
  231. return KeyBord.D0;
  232. if (key == "1")
  233. return KeyBord.D1;
  234. if (key == "2")
  235. return KeyBord.D2;
  236. if (key == "3")
  237. return KeyBord.D3;
  238. if (key == "4")
  239. return KeyBord.D4;
  240. if (key == "5")
  241. return KeyBord.D5;
  242. if (key == "6")
  243. return KeyBord.D6;
  244. if (key == "7")
  245. return KeyBord.D7;
  246. if (key == "8")
  247. return KeyBord.D8;
  248. if (key == "9")
  249. return KeyBord.D9;//0-9
  250. if (key == "-")
  251. return KeyBord.OemMinus;
  252. if (key == "=")
  253. return KeyBord.Oemplus;
  254. if (key == @"\")
  255. return KeyBord.OemBackslash;
  256. if (key == "BackSpace")
  257. return KeyBord.Backspace;
  258. if (key == "Tab")
  259. return KeyBord.Tab;
  260. if (key == "CapsLock")
  261. return KeyBord.CapsLock;
  262. if (key == "Windows")
  263. return KeyBord.LWin;
  264. if (key == "Alt")
  265. return KeyBord.Alt;
  266. if (key == "Shift")
  267. return KeyBord.ShiftKey;
  268. if (key == "Control")
  269. return KeyBord.ControlKey;
  270. if (key == "[")
  271. return KeyBord.OemOpenBrackets;
  272. if (key == "]")
  273. return KeyBord.Oem6;
  274. if (key == ";")
  275. return KeyBord.OemSemicolon;
  276. if (key == "'")
  277. return KeyBord.OemQuotes;
  278. if (key == ",")
  279. return KeyBord.Oemcomma;
  280. if (key == ".")
  281. return KeyBord.OemPeriod;
  282. if (key == "?")
  283. return KeyBord.OemQuestion;
  284. if (key == "Space")
  285. return KeyBord.Space;
  286. if (key == "<")
  287. return KeyBord.Oemcomma;
  288. if (key == ">")
  289. return KeyBord.OemPeriod;
  290. if (key == "/")
  291. return KeyBord.OemQuestion;
  292. if (key == "~")
  293. return KeyBord.Oem3;
  294. if (key == "Inst")/控制键盘
  295. return KeyBord.Insert;
  296. if (key == "Home")
  297. return KeyBord.Home;
  298. if (key == "End")
  299. return KeyBord.End;
  300. if (key == "Enter")
  301. return KeyBord.Enter;
  302. if (key == "PageDw")
  303. return KeyBord.PageDown;
  304. if (key == "PageUp")
  305. return KeyBord.PageUp;
  306. if (key == "Left")
  307. return KeyBord.Left;
  308. if (key == "Right")
  309. return KeyBord.Right;
  310. if (key == "Up")
  311. return KeyBord.Up;
  312. if (key == "Down")
  313. return KeyBord.Down;
  314. if (key == "PrintScreen")
  315. return KeyBord.PrintScreen;
  316. if (key == "Scroll")
  317. return KeyBord.Scroll;
  318. if (key == "Num0")//数字键盘
  319. return KeyBord.NumPad0;
  320. if (key == "Num1")
  321. return KeyBord.NumPad1;
  322. if (key == "Num2")
  323. return KeyBord.NumPad2;
  324. if (key == "Num3")
  325. return KeyBord.NumPad3;
  326. if (key == "Num4")
  327. return KeyBord.NumPad4;
  328. if (key == "Num5")
  329. return KeyBord.NumPad5;
  330. if (key == "Num6")
  331. return KeyBord.NumPad6;
  332. if (key == "Num7")
  333. return KeyBord.NumPad7;
  334. if (key == "Num8")
  335. return KeyBord.NumPad8;
  336. if (key == "Num9")
  337. return KeyBord.NumPad9;
  338. if (key == "NumAdd")
  339. return KeyBord.Add;
  340. if (key == "Num/")
  341. return KeyBord.Divide;
  342. if (key == "Num*")
  343. return KeyBord.Multiply;
  344. if (key == "Num-")
  345. return KeyBord.Subtract;
  346. if (key == "NumEnter")
  347. return KeyBord.Enter;
  348. if (key == "Num.")
  349. return KeyBord.OemPeriod;
  350. if (key == "NumLock")
  351. return KeyBord.NumLock;
  352. return KeyBord.T;
  353. }
  354. }

 

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

闽ICP备14008679号