Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Arduino keyboard logs off


May 15, 2021 Arduino


Table of contents


This example uses a keyboard library to log off your user session on your computer, at which point pin 2 on ARDOINO UNO is pulled to ground. /b10> Sketches simulate keys in two or three keys at the same time and release them after a short delay.

Warning - Arduino takes over your computer keyboard when you use the Keyboard.print() command. /b10> To ensure that you do not lose control of your computer when you run sketches with this feature, set up a reliable control system before calling Keyboard.print(). /b11> This sketch is intended to send keyboard commands only after the pin is pulled to ground.

The required component

You will need the following components:

  • 1 × breadboard breadboard
  • 1 × Arduino Leonardo, Micro, or Due board
  • 1 × button
  • 1 × jumper

Program

Connect the components on the breadboard according to the circuit diagram, as shown in the following image.

The Arduino keyboard logs off

Sketch

Turn on the Arduino IDE software on your computer. U se arduino to encode and control your circuitry. /b11> Open a new sketch file by clicking New.

For this example, you need to use Arduino IDE 1.6.7

The Arduino keyboard logs off

Note - You must include the keyboard library in the Arduino library file. /b10> Copy and paste the keyboard library file into a file named libraries (highlighted below), as shown in the following screenshot.

The Arduino keyboard logs off

Arduino code

/*
   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);
}

Code description

Before you upload a program to your board, make sure that you assign the correct operating system you are using to the platform variables.

While the sketch is running, the pin 2 is grounded at the push of a button, and the board sends a logout sequence to the USB-connected PC.

Results

When pin 2 is grounded, it performs a logout operation.

It logs off using the following keyboard combination:

  • On Windows, press CTRL-ALT-DEL, and then ALT-l

  • At Ubuntu, CTRL-ALT-DEL and ENTER

  • On OSX, CMD-SHIFT-q