Keyboard Logout
Logs out the current user with key commands.
This example uses the Keyboard library to log you out of your user session on your computer when pin 2 on your Leonardo, Micro or Due is pulled to ground. The sketch simulates the keypress in sequence of two or three keys at the same time and after a short delay it releases them.
NB: When you use the Keyboard.print() command, the Arduino takes over your computer's keyboard! To insure you don't lose control of your computer while running a sketch with this function, make sure to set up a reliable control system before you call Keyboard.print(). This sketch is designed to only send a Keyboard command after a pin has been pulled to ground.
Hardware Required
Arduino Leonardo, Micro, or Due board
pushbutton
hook-up wires
breadboard
Circuit
Schematic
Code
Before you upload the program to your board, make sure to assign the correct OS you are currently using to the platform variable.
While the sketch is running, pressing the button will connect pin 2 to ground and the board will send the logout sequence to the USB connected pc.
1/*2
3 Keyboard logout4
5 This sketch demonstrates the Keyboard library.6
7 When you connect pin 2 to ground, it performs a logout.8
9 It uses keyboard combinations to do this, as follows:10
11 On Windows, CTRL-ALT-DEL followed by ALT-l12
13 On Ubuntu, CTRL-ALT-DEL, and ENTER14
15 On OSX, CMD-SHIFT-q16
17 To wake: Spacebar.18
19 Circuit:20
21 - Arduino Leonardo or Micro22
23 - wire to connect D2 to ground24
25 created 6 Mar 201226
27 modified 27 Mar 201228
29 by Tom Igoe30
31 This example is in the public domain.32
33 https://www.arduino.cc/en/Tutorial/KeyboardLogout34
35*/36
37#define OSX 038#define WINDOWS 139#define UBUNTU 240
41#include "Keyboard.h"42
43// change this to match your platform:44int platform = OSX;45
46void setup() {47
48 // make pin 2 an input and turn on the pull-up resistor so it goes high unless49
50 // connected to ground:51
52 pinMode(2, INPUT_PULLUP);53
54 Keyboard.begin();55}56
57void loop() {58
59 while (digitalRead(2) == HIGH) {60
61 // do nothing until pin 2 goes low62
63 delay(500);64
65 }66
67 delay(1000);68
69 switch (platform) {70
71 case OSX:72
73 Keyboard.press(KEY_LEFT_GUI);74
75 // Shift-Q logs out:76
77 Keyboard.press(KEY_LEFT_SHIFT);78
79 Keyboard.press('Q');80
81 delay(100);82
83 Keyboard.releaseAll();84
85 // enter:86
87 Keyboard.write(KEY_RETURN);88
89 break;90
91 case WINDOWS:92
93 // CTRL-ALT-DEL:94
95 Keyboard.press(KEY_LEFT_CTRL);96
97 Keyboard.press(KEY_LEFT_ALT);98
99 Keyboard.press(KEY_DELETE);100
101 delay(100);102
103 Keyboard.releaseAll();104
105 // ALT-l:106
107 delay(2000);108
109 Keyboard.press(KEY_LEFT_ALT);110
111 Keyboard.press('l');112
113 Keyboard.releaseAll();114
115 break;116
117 case UBUNTU:118
119 // CTRL-ALT-DEL:120
121 Keyboard.press(KEY_LEFT_CTRL);122
123 Keyboard.press(KEY_LEFT_ALT);124
125 Keyboard.press(KEY_DELETE);126
127 delay(1000);128
129 Keyboard.releaseAll();130
131 // Enter to confirm logout:132
133 Keyboard.write(KEY_RETURN);134
135 break;136
137 }138
139 // do nothing:140
141 while (true);142}
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/07/29 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.