Skip to content

Instantly share code, notes, and snippets.

@stringsn88keys
Last active April 28, 2022 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stringsn88keys/f1d8b1d2cb90c604f9d8601a83f4d62f to your computer and use it in GitHub Desktop.
Save stringsn88keys/f1d8b1d2cb90c604f9d8601a83f4d62f to your computer and use it in GitHub Desktop.
Arduino LCD Keypad hotkey example which moves windows
#include <Keyboard.h>
#include <Mouse.h>
#include <LiquidCrystal.h>
/*
Arduino 2x16 LCD - Detect Buttons
modified on 18 Feb 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/
*/
//LCD pin to Arduino
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;
LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
// All keys are connected to the A0 pin so they will read with analogRead(0)
// https://create.arduino.cc/projecthub/electropeak/using-1602-lcd-keypad-shield-w-arduino-w-examples-e02d95
// Keyboard Scan Codes from usb_hid_keys.h
// https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
#define KEY_LEFT_GUI 0x83 // 131
#define KEY_UP_ARROW 0xDA // 218
#define KEY_DOWN_ARROW 0xD9 // 217
#define KEY_LEFT_ARROW 0xD8 // 216
#define KEY_RIGHT_ARROW 0xD7 // 215
// realistically, you could cascade x < 60, x < 200, x < 400, x < 600
// but this allows specifically checking for a direction without
// assuming that directions that had lower analog values were checked
#define IS_RIGHT(x) ((x)>=0 && (x)<60)
#define IS_UP(x) ((x)>=60 && (x)<200)
#define IS_DOWN(x) ((x)>=200 && (x)<400)
#define IS_LEFT(x) ((x)>=400 && (x)<600)
#define IS_SELECT(x) ((x)>=600 && (x)<800)
void blinkIfYoureRebooted() {
// 200, 400, 600, 800, 1000ms blinks
for(int i=1; i<=5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200 * i);
digitalWrite(LED_BUILTIN, LOW);
delay(200 * i);
}
}
void setup() {
// this runs once
pinMode(LED_BUILTIN, OUTPUT);
blinkIfYoureRebooted();
Mouse.begin();
}
char translateToOutputKey(int analogRead) {
if(IS_RIGHT(analogRead)) { return KEY_RIGHT_ARROW; }
if(IS_UP(analogRead)) { return KEY_UP_ARROW; }
if(IS_DOWN(analogRead)) { return KEY_DOWN_ARROW; }
if(IS_LEFT(analogRead)) { return KEY_LEFT_ARROW; }
return 0;
}
void loop() {
char key;
key=translateToOutputKey(analogRead(0));
if(key) {
Keyboard.begin();
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(key);
Keyboard.releaseAll();
delay(200); // debounce
} else {
// 10090 is a completely arbitrary number but
// it seems to move a barely perceptible amount
// a few times per second at most.
if(random(0,10090)==0) {
Mouse.move(random(0,5)-2, random(0,5)-2,0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment