Skip to content

Instantly share code, notes, and snippets.

@zvakanaka
Last active April 27, 2021 05:04
Show Gist options
  • Select an option

  • Save zvakanaka/f76fe0c0383aa64446b19d5cc62a236a to your computer and use it in GitHub Desktop.

Select an option

Save zvakanaka/f76fe0c0383aa64446b19d5cc62a236a to your computer and use it in GitHub Desktop.
#include <Keyboard.h>
using namespace std;
// you MUST install ArduinoSTL library by Mike Matera
#define KEY_DELAY 200
// pins 2, 3, 4, 5, 6, 7, 8, 9, 10, 16
#define KEY0 2
#define KEY2 3
#define KEY3 4
#define KEY4 5
#define KEY5 6
#define KEY6 7
#define KEY7 8
#define KEY8 9
#define KEY9 10
#define KEY1 16
#define COMBO true
#define SEQ false
// https://www.arduino.cc/en/Reference/KeyboardModifiers
//const char KEY_0_KEYS[] = { '0' };
//const char KEY_1_KEYS[] = { '1' };
//const char KEY_2_KEYS[] = { '2' };
//const char KEY_3_KEYS[] = { '3' };
//const char KEY_4_KEYS[] = { '4' };
//const char KEY_5_KEYS[] = { '5' };
//const char KEY_6_KEYS[] = { '6' };
//const char KEY_7_KEYS[] = { '7' };
//const char KEY_8_KEYS[] = { '8' };
//const char KEY_9_KEYS[] = { '9' };
const char KEY_0_KEYS[] = { 'u', 'c', 's', KEY_RETURN, 'o', 'b', KEY_RETURN };
const char KEY_1_KEYS[] = { 'u', 'c', 's', KEY_RETURN, 'w', KEY_RETURN };
const char KEY_2_KEYS[] = { 'u', 'c', 's', KEY_RETURN, 'x', KEY_RETURN, '9', '0', KEY_RETURN };
const char KEY_3_KEYS[] = { 'u', 'c', 's', KEY_RETURN, 'y', KEY_RETURN, '9', '0', KEY_RETURN };
const char KEY_4_KEYS[] = { 'p', 's', KEY_RETURN }; // PSPACE / Switches from a model space viewport to paper space
const char KEY_5_KEYS[] = { 'm', 's', KEY_RETURN }; // MSPACE / Switches from paper space to a model space viewport
const char KEY_6_KEYS[] = { 'd', 'i', KEY_RETURN }; // DIST / Measures the distance and angle between two points
const char KEY_7_KEYS[] = { 'p', 'r', 'i', 'n', 't', KEY_RETURN }; // PLOT / Plots a drawing to a plotter, printer, or file
const char KEY_8_KEYS[] = { 's', 'p', 'o', 'o', 'l', 'd', 'i', 'm', KEY_RETURN }; // add Spool Dimension
const char KEY_9_KEYS[] = { 's', 't', 'r', KEY_RETURN }; // STRETCH / Stretches objects crossed by a selection window or polygon
template<typename T, size_t S>
void checkKey(unsigned int key, T (&keysToPress)[S], bool isCombo);
void setupKeys();
template<typename T, size_t S>
void pressKeys(T (&keysToPress)[S], bool isCombo);
void setup() {
setupKeys();
// Serial.begin(9600);
Keyboard.begin();
delay(KEY_DELAY);
// Serial.println("Serial started");
// disable LEDs: https://arduino.stackexchange.com/a/63448/72376
pinMode(LED_BUILTIN_TX, INPUT);
pinMode(LED_BUILTIN_RX, INPUT);
}
void loop() {
checkKey(KEY0, KEY_0_KEYS, SEQ);
checkKey(KEY1, KEY_1_KEYS, SEQ);
checkKey(KEY2, KEY_2_KEYS, SEQ);
checkKey(KEY3, KEY_3_KEYS, SEQ);
checkKey(KEY4, KEY_4_KEYS, SEQ);
checkKey(KEY5, KEY_5_KEYS, SEQ);
checkKey(KEY6, KEY_6_KEYS, SEQ);
checkKey(KEY7, KEY_7_KEYS, SEQ);
checkKey(KEY8, KEY_8_KEYS, SEQ);
checkKey(KEY9, KEY_9_KEYS, SEQ);
}
template<typename T, size_t S>
void pressKeys(T (&keysToPress)[S], bool isCombo) {
for (char keyToPress : keysToPress) {
if (isCombo) {
Keyboard.press(keyToPress);
} else {
Keyboard.print(keyToPress);
}
}
}
template<typename T, size_t S>
void checkKey(unsigned int key, T (&keysToPress)[S], bool isCombo) {
if (digitalRead(key) == LOW) {
// Serial.print(key);
// Serial.println(" pressed");
pressKeys(keysToPress, isCombo);
delay(KEY_DELAY); // prevent double-press
}
if (digitalRead(key) == HIGH) {
Keyboard.releaseAll();
}
}
void setupKeys() {
pinMode(KEY0, INPUT_PULLUP);
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
pinMode(KEY3, INPUT_PULLUP);
pinMode(KEY4, INPUT_PULLUP);
pinMode(KEY5, INPUT_PULLUP);
pinMode(KEY6, INPUT_PULLUP);
pinMode(KEY7, INPUT_PULLUP);
pinMode(KEY8, INPUT_PULLUP);
pinMode(KEY9, INPUT_PULLUP);
}
@zvakanaka
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment