emacs-keyboard.ino 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // #include "Keyboard.h"
  2. // #include "HID.h"
  3. // https://www.arduino.cc/en/Reference/KeyboardBegin
  4. // https://www.arduino.cc/en/Tutorial/KeyboardMessage
  5. // https://www.arduino.cc/en/Tutorial/KeyboardReprogram
  6. // http://www.cplusplus.com/doc/tutorial/structures/
  7. struct Key {
  8. int pin;
  9. char keyCode;
  10. boolean wasPushed;
  11. };
  12. const int NUM_OF_KEYS = 4;
  13. Key keys[NUM_OF_KEYS];
  14. void setup() {
  15. // Initialize key data
  16. // { buttonPin, keyCode }
  17. keys[0] = { 2, KEY_LEFT_CTRL };
  18. keys[1] = { 3, KEY_LEFT_ALT };
  19. keys[2] = { 4, KEY_LEFT_SHIFT };
  20. keys[3] = { 5, KEY_BACKSPACE };
  21. // make pins inputs and turn on the
  22. // pullup resistor so they go high unless
  23. // connected to ground:
  24. for(int i = 0; i < NUM_OF_KEYS; i++) {
  25. pinMode(keys[i].pin, INPUT_PULLUP);
  26. }
  27. Keyboard.begin();
  28. }
  29. void loop() {
  30. for(int i = 0; i < NUM_OF_KEYS; i++) {
  31. if(digitalRead(keys[i].pin) == LOW) { // If the key is pressed...
  32. Keyboard.press(keys[i].keyCode); // Send the key
  33. delay(100);
  34. Keyboard.releaseAll();
  35. }
  36. }
  37. }