Skip to content

Instantly share code, notes, and snippets.

@jonathanedgecombe
Created November 22, 2017 19:23
Show Gist options
  • Save jonathanedgecombe/31d640ec985e4b0ed194d075214fdb97 to your computer and use it in GitHub Desktop.
Save jonathanedgecombe/31d640ec985e4b0ed194d075214fdb97 to your computer and use it in GitHub Desktop.
Arduino micro keypad
#include <Mouse.h>
#include <Keyboard.h>
#define KEYS 12
#define LEFT_CLICK 0
#define RIGHT_CLICK -1
#define MIDDLE_CLICK -2
int clicks[] = {
MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE
};
int pins[] = {
23, 5, 6, 22,
20, 19, 21, 18,
3, 11, 8, 9
};
int state[] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
int bindings[] = {
KEY_ESC, '-', KEY_UP_ARROW, '=',
KEY_TAB, KEY_LEFT_ARROW, KEY_DOWN_ARROW, KEY_RIGHT_ARROW,
'z', 'x', RIGHT_CLICK, LEFT_CLICK
};
void setup() {
for (int k = 0; k < KEYS; k++) {
pinMode(pins[k], INPUT_PULLUP);
}
Keyboard.begin();
Mouse.begin();
}
void loop() {
for (int k = 0; k < KEYS; k++) {
int pressed = digitalRead(pins[k]);
if (pressed == 0) {
if (state[k] == 0) {
if (bindings[k] > 0) {
Keyboard.press(bindings[k]);
} else {
Mouse.press(clicks[-bindings[k]]);
}
}
state[k] = 1;
} else {
if (state[k] > 0) {
state[k]++;
if (state[k] > 128) {
state[k] = 0;
if (bindings[k] > 0) {
Keyboard.release(bindings[k]);
} else {
Mouse.release(clicks[-bindings[k]]);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment