本示例使用键盘库在您的计算机上注销您的用户会话,当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

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

Arduino代码
- /*
- Keyboard logout
- This sketch demonstrates the Keyboard library.
- When you connect pin 2 to ground, it performs a logout.
- It uses keyboard combinations to do this, as follows:
- On Windows, CTRL-ALT-DEL followed by ALT-l
- On Ubuntu, CTRL-ALT-DEL, and ENTER
- On OSX, CMD-SHIFT-q
- To wake: Spacebar.
- Circuit:
- * Arduino Leonardo or Micro
- * wire to connect D2 to ground.
- */
- #define OSX 0
- #define WINDOWS 1
- #define UBUNTU 2
- #include "Keyboard.h"
- // change this to match your platform:
- int platform = WINDOWS;
- void setup() {
- // make pin 2 an input and turn on the
- // pullup resistor so it goes high unless
- // connected to ground:
- pinMode(2, INPUT_PULLUP);
- Keyboard.begin();
- }
- void loop() {
- while (digitalRead(2) == HIGH) {
- // do nothing until pin 2 goes low
- delay(500);
- }
- delay(1000);
- switch (platform) {
- case OSX:
- Keyboard.press(KEY_LEFT_GUI);
- // Shift-Q logs out:
- Keyboard.press(KEY_LEFT_SHIFT);
- Keyboard.press('Q');
- delay(100);
- // enter:
- Keyboard.write(KEY_RETURN);
- break;
- case WINDOWS:
- // CTRL-ALT-DEL:
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press(KEY_DELETE);
- delay(100);
- Keyboard.releaseAll();
- //ALT-l:
- delay(2000);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press('l');
- Keyboard.releaseAll();
- break;
- case UBUNTU:
- // CTRL-ALT-DEL:
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press(KEY_DELETE);
- delay(1000);
- Keyboard.releaseAll();
- // Enter to confirm logout:
- Keyboard.write(KEY_RETURN);
- break;
- }
- // do nothing:
- while (true);
- }
- Keyboard.releaseAll();
- // enter:
- Keyboard.write(KEY_RETURN);
- break;
- case WINDOWS:
- // CTRL-ALT-DEL:
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press(KEY_DELETE);
- delay(100);
- Keyboard.releaseAll();
- //ALT-l:
- delay(2000);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press('l');
- Keyboard.releaseAll();
- break;
- case UBUNTU:
- // CTRL-ALT-DEL:
- Keyboard.press(KEY_LEFT_CTRL);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press(KEY_DELETE);
- delay(1000);
- Keyboard.releaseAll();
- // Enter to confirm logout:
- Keyboard.write(KEY_RETURN);
- break;
- }
- // do nothing:
- while (true);
- }
代码说明
在将程序上传到开发板之前,请确保将正在使用的正确操作系统分配给平台变量。
在草图运行时,按下按钮将引脚2连接到地,板将发送注销序列到USB连接的PC。
结果
当将引脚2连接到地时,它执行注销操作。
它使用以下键盘组合注销 -
-
在 Windows 上,按CTRL-ALT-DEL,然后按ALT-l
-
在 Ubuntu ,CTRL-ALT-DEL和ENTER
-
在 OSX 上,CMD-SHIFT-q
转载本站内容时,请务必注明来自W3xue,违者必究。