For an index to all my stories click this text
This story is about building a so-called boss button for your computer.
The story uses Arduino language (C++) on any Raspberry Pi Pico.
You'll need-
- Raspberry Pi Pico (any version)
- A push button
What's a boss-button.
A boss button was popular some years ago.
Suppose you are at work and playing a game, browsing the internet, looking at forums or anything else you are not supposed to do. Then your boss suddenly walks in......
Or you are programming something which is in a preliminary stage and you do not want anyone to see.
Or You're designing or buying a present for your wife/girlfriend and she suddenly enters your mancave.
And let's be frank: you do not want to get caught watching porn, do you ???
You get the intention.
This is where the boss button gets in.
When you press that button the window you are working in closes immediately.
Boss button or ALT-F4
You might press ALT-F4 at your keyboard (just try it now).
That closes the window you are working in immediately.
There is only one problem with this.
All the work you've done is lost.
Or the program will not close and asks if it should save your work first.
And that leaves the window open for anyone to see...........
Our boss button.
Our boss button therefore needs to do more.
it needs to save your work and the close the window.
This involves two keystrokes:
CTRL S
ALT F4
To achieve this you need to remember these keystrokes and use them fast.
Just try that. I bet you can not do that fast enough.
So we need a button that when pressed activates those two keystrokes automatically.
And that's what our boss button is going to do.
USB Hid
To built a boss button the microcontroller of our choice needs to be able to act as an USB-hid. This means a USN Human Interface Device.
Normally we can communicate with our microcontroller over USB in the Arduino IDE or in Thonny (for micropython). However that is serial communication. The Arduino IDE or Thonny is at that moment acting as a serial inteface and not as a real USB device.
To send commands or even text to an editor (like word or office) or any other program your need a real keyboard. Or your microcontroller needs to act as one.
And that is what the USB-hid library does.
Unfortunately not all microcontrollers are capable of acting as an USB hid. The controller must be equipped with an able USB chip. The ESP8266 and ESP32 are not. Some Arduino's will work. The Raspberry Pi Pico series do the trick flawless.
Arduino IDE
In the Arduino boards manager make sure you have installed the Raspberry Pi Pico/RP2040/RP2350 entry by Earle F. Philhower III
If the board definitions are installed there will be a keyboard entry in the list of library's. That's what we are going to use.
Breadboard setup.
The breadboard setup is really simple.
The push button is connected to GND and to GPIO-15. That's all that is to it.
Normally I use pull-up resistors for connecting a button. But as I want to keep this as simple as possible I am going to use the internal PULL-UP resistor.
The program.
#include <Keyboard.h> const int buttonPin = 15; void setup() { pinMode(buttonPin, INPUT_PULLUP); Keyboard.begin(); delay(5000); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == LOW) { Keyboard.press(KEY_LEFT_CTRL); // Hold down the Control key Keyboard.press('s'); // Press s delay(200); Keyboard.releaseAll(); Keyboard.press(KEY_LEFT_ALT); // Hold down the Control key Keyboard.press(KEY_F4); // Press F1 delay(200); // Wait a bit to ensure the key is recognized Keyboard.releaseAll(); Keyboard.press(KEY_LEFT_CTRL); // Hold down the Control key Keyboard.press(KEY_F1); // Press F1 delay(100); Keyboard.releaseAll(); Keyboard.end(); } }
That's all.
I am going to break the code up in several parts and give a short explanation.
#include <Keyboard.h>
Obviously the keyboard library needs to be loaded.
const int buttonPin = 15;
The button is attached to pin GPIO-15
void setup() { pinMode(buttonPin, INPUT_PULLUP); Keyboard.begin(); delay(5000); }
The setup makes sure that the button is declared as an INPUT with the internal PULL_UP resistor activated.
And the keyboard library is activated.
The exciting things happen in de loop.
int buttonState = digitalRead(buttonPin); if (buttonState == LOW)
The state of the buttonpin is read. If it is LOW then the button is pressed.
Keyboard.press(KEY_LEFT_CTRL); // Hold down the Control key Keyboard.press('s'); // Press s delay(200); Keyboard.releaseAll();
When the button is pressed we first send the keycode CTRL s which means save the current work.
Keyboard.press(KEY_LEFT_ALT); // Hold down the Control key Keyboard.press(KEY_F4); // Press F1 delay(200); // Wait a bit to ensure the key is recognized Keyboard.releaseAll();
Then we send the code ALT F4 This is the code to close the current screen.
And for Linux users we have something extra:
Keyboard.press(KEY_LEFT_CTRL); // Hold down the Control key Keyboard.press(KEY_F1); // Press F1 delay(100); Keyboard.releaseAll();;
Linux can work in multiple virtual windows.
Normally you will work in Virtual window 1. The things you do in secret will mostly be done in Virtual window 2 (or any other if you have more).
The code CTRL F1 makes Linux move to Virtual window 1.
So besides saving your work and closing the window you were working in the code also moves your screen to Virtual window 1 where suposedly your normal work is done and not the things you are hiding.
Leave these last lines out if you are using Windows.
Keyboard.end();
And this stops the HID function.
It is very important to use this command:
Keyboard.releaseAll();
after each key combination. If you forget this then you might encounter strange effects.
That's it.
Here is my test setup. And yes indeed that's a hell of a large button. The Raspberry Pi Pico would easily fit within the button.
I am going to replace it though with a foot pedal. A foot pedal can easily be hidden beneath my desk..........
You can make large buttons and foot pedals very easy. But that is another story, one that will be covered also on this weblog.
I have some nice other plans for using the Raspberry Pi Pico as a HID device.
So keep on visiting this weblog.
Till next time
have fun
Luc Volders