课程表

Arduino 基础

Arduino 函数库

Arduino 进阶

Arduino 项目

Arduino 传感器

Arduino 电机控制

Arduino 声音

工具箱
速查手册

Arduino 键盘注销

当前位置:免费教程 » 程序设计 » Arduino

本示例使用键盘库在您的计算机上注销您的用户会话,当ARDUINO UNO上的引脚2被拉到地面时。 草图在两个或三个键的同时按顺序模拟按键,并且在短暂的延迟之后,它释放它们。

警告 - 当您使用 Keyboard.print()命令时,Arduino接管您的计算机键盘。 为确保在使用此功能运行草图时不会失去对计算机的控制,请在调用Keyboard.print()之前设置可靠的控制系统。 此草图旨在仅在引脚拉到地后发送键盘命令。

 

必需的组件

您将需要以下组件 -

  • 1 × Breadboard 面包板
  • 1 × Arduino Leonardo, Micro, 或Due board
  • 1 × pushbutton 按钮
  • 1 × Jumper 跳线
 

程序

按照电路图并挂接面包板上的组件,如下图所示。

 

面包板

 

草图

在计算机上打开Arduino IDE软件。 在Arduino语言编码将控制你的电路。 通过单击新建打开一个新的草图文件。

对于本例,您需要使用Arduino IDE 1.6.7

 

Sketch

 

注意 - 您必须在Arduino库文件中包含键盘库。 使用名称库(突出显示)将键盘库文件复制并粘贴到文件中,如以下屏幕截图所示。

 

在Arduino库文件中包含键盘库

 

Arduino代码

  1. /*
  2. Keyboard logout
  3. This sketch demonstrates the Keyboard library.
  4. When you connect pin 2 to ground, it performs a logout.
  5. It uses keyboard combinations to do this, as follows:
  6. On Windows, CTRL-ALT-DEL followed by ALT-l
  7. On Ubuntu, CTRL-ALT-DEL, and ENTER
  8. On OSX, CMD-SHIFT-q
  9. To wake: Spacebar.
  10. Circuit:
  11. * Arduino Leonardo or Micro
  12. * wire to connect D2 to ground.
  13. */
  14.  
  15. #define OSX 0
  16. #define WINDOWS 1
  17. #define UBUNTU 2
  18.  
  19. #include "Keyboard.h"
  20.  
  21. // change this to match your platform:
  22. int platform = WINDOWS;
  23.  
  24. void setup() {
  25. // make pin 2 an input and turn on the
  26. // pullup resistor so it goes high unless
  27. // connected to ground:
  28. pinMode(2, INPUT_PULLUP);
  29. Keyboard.begin();
  30. }
  31.  
  32. void loop() {
  33. while (digitalRead(2) == HIGH) {
  34. // do nothing until pin 2 goes low
  35. delay(500);
  36. }
  37. delay(1000);
  38. switch (platform) {
  39. case OSX:
  40. Keyboard.press(KEY_LEFT_GUI);
  41. // Shift-Q logs out:
  42. Keyboard.press(KEY_LEFT_SHIFT);
  43. Keyboard.press('Q');
  44. delay(100);
  45. // enter:
  46. Keyboard.write(KEY_RETURN);
  47. break;
  48. case WINDOWS:
  49. // CTRL-ALT-DEL:
  50. Keyboard.press(KEY_LEFT_CTRL);
  51. Keyboard.press(KEY_LEFT_ALT);
  52. Keyboard.press(KEY_DELETE);
  53. delay(100);
  54. Keyboard.releaseAll();
  55. //ALT-l:
  56. delay(2000);
  57. Keyboard.press(KEY_LEFT_ALT);
  58. Keyboard.press('l');
  59. Keyboard.releaseAll();
  60. break;
  61. case UBUNTU:
  62. // CTRL-ALT-DEL:
  63. Keyboard.press(KEY_LEFT_CTRL);
  64. Keyboard.press(KEY_LEFT_ALT);
  65. Keyboard.press(KEY_DELETE);
  66. delay(1000);
  67. Keyboard.releaseAll();
  68. // Enter to confirm logout:
  69. Keyboard.write(KEY_RETURN);
  70. break;
  71. }
  72. // do nothing:
  73. while (true);
  74. }
  75.  
  76. Keyboard.releaseAll();
  77.  
  78. // enter:
  79. Keyboard.write(KEY_RETURN);
  80. break;
  81. case WINDOWS:
  82. // CTRL-ALT-DEL:
  83. Keyboard.press(KEY_LEFT_CTRL);
  84. Keyboard.press(KEY_LEFT_ALT);
  85. Keyboard.press(KEY_DELETE);
  86. delay(100);
  87. Keyboard.releaseAll();
  88. //ALT-l:
  89. delay(2000);
  90. Keyboard.press(KEY_LEFT_ALT);
  91. Keyboard.press('l');
  92. Keyboard.releaseAll();
  93. break;
  94. case UBUNTU:
  95. // CTRL-ALT-DEL:
  96. Keyboard.press(KEY_LEFT_CTRL);
  97. Keyboard.press(KEY_LEFT_ALT);
  98. Keyboard.press(KEY_DELETE);
  99. delay(1000);
  100. Keyboard.releaseAll();
  101. // Enter to confirm logout:
  102. Keyboard.write(KEY_RETURN);
  103. break;
  104. }
  105. // do nothing:
  106. while (true);
  107. }

 

代码说明

在将程序上传到开发板之前,请确保将正在使用的正确操作系统分配给平台变量。

在草图运行时,按下按钮将引脚2连接到地,板将发送注销序列到USB连接的PC。

 

结果

当将引脚2连接到地时,它执行注销操作。

它使用以下键盘组合注销 -

  • 在 Windows 上,按CTRL-ALT-DEL,然后按ALT-l

  • 在 Ubuntu ,CTRL-ALT-DEL和ENTER

  • 在 OSX 上,CMD-SHIFT-q

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号