emacs-keyboard.ino 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // https://www.arduino.cc/en/Reference/KeyboardModifiers
  17. // { buttonPin, keyCode, wasPushed }
  18. keys[0] = { 2, KEY_LEFT_CTRL , false };
  19. keys[1] = { 3, KEY_LEFT_ALT, false};
  20. keys[2] = { 4, KEY_LEFT_SHIFT, false };
  21. keys[3] = { 5, KEY_BACKSPACE, false };
  22. // make pins inputs and turn on the
  23. // pullup resistor so they go high unless
  24. // connected to ground:
  25. for(int i = 0; i < NUM_OF_KEYS; i++) {
  26. pinMode(keys[i].pin, INPUT_PULLUP);
  27. }
  28. Keyboard.begin();
  29. }
  30. void loop() {
  31. for(int i = 0; i < NUM_OF_KEYS; i++) {
  32. // If key is now pushed, but wasn't before...
  33. if((digitalRead(keys[i].pin) == LOW) && (keys[i].wasPushed == false)) {
  34. Keyboard.press(keys[i].keyCode); // Send key down
  35. keys[i].wasPushed = true;
  36. }
  37. // If key is now unpushed, but was before...
  38. else if ((digitalRead(keys[i].pin) == HIGH) && (keys[i].wasPushed == true)) {
  39. Keyboard.release(keys[i].keyCode); // Send key up
  40. keys[i].wasPushed = false;
  41. }
  42. }