Code provided below can be compiled using Arduino IDE provided by DigiStump. The firmware contains a neat trick allowing to cancel a press on the button. In fact you'll have a 2 second countdown before shortcut is sent. If emergency button is released within this period, action is canceled.
#include "DigiKeyboard.h"
#define KEY_DELETE 76
bool state = false;
int countdown = -1;
void setup() {
pinMode(2, INPUT);
digitalWrite(2, HIGH); // Pull-up
}
void loop() {
// Kept from sample
// this is generally not necessary but with some older systems it seems to
// prevent missing the first character after a delay:
DigiKeyboard.sendKeyStroke(0);
int sensorValue = digitalRead(2);
if (sensorValue == 1) {
// Push button is pressed
if (countdown == 0) {
// It's the final count down ! Send shortcut Ctrl-Alt-L
if (state == false) {
DigiKeyboard.sendKeyStroke(KEY_L, MOD_ALT_LEFT | MOD_CONTROL_LEFT);
state = true;
}
} else if (countdown == -1) {
// We were waiting, start countdown
countdown = 20; // countdown * 100 = <time in milliseconds>
state = false;
} else {
// Still pressed, continue the count down
countdown--;
}
} else {
// Push button is released (Short circuit -> GND)
// Reset
countdown = -1;
}
// Kept from sample
// It's better to use DigiKeyboard.delay() over the regular Arduino delay()
// if doing keyboard stuff because it keeps talking to the computer to make
// sure the computer knows the keyboard is alive and connected
DigiKeyboard.delay(100);
}