Skip to content

Instantly share code, notes, and snippets.

@di0ib di0ib/8key.ino Secret
Last active Dec 7, 2016

Embed
What would you like to do?
//install HID Project from Library Manager
#include "HID-Project.h"
//debounce milliseconds
const int debounce = 10;
//Switch Pins
const byte k[8] = { 21, 20, 19, 18, 2, 3, 4, 5 };
//Switch status
boolean s[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
//M for Media Key, K for Keyboard.
//The commands for consumer (media keys) is different than normal keyboard output.
//This indicates if the switch position is one or the other
const char codetype[8] = { 'K', 'M', 'M', 'M', 'M', 'M', 'M', 'M' };
//Keycodes for Media Keys
const ConsumerKeycode ccode[8] = { MEDIA_VOLUME_DOWN,
MEDIA_VOLUME_DOWN,
MEDIA_VOLUME_MUTE,
MEDIA_VOLUME_UP,
MEDIA_PREVIOUS,
MEDIA_PLAY_PAUSE,
MEDIA_STOP,
MEDIA_NEXT
};
//Keycodes for Regular Keys
const KeyboardKeycode kcode[8] = { KEY_ESC,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8
};
void setup() {
Keyboard.begin();
Consumer.begin();
//setup inputs, turn on pullups
for (int i = 0; i <= 7; i++) {
pinMode(k[i], INPUT);
digitalWrite(k[i], 1);
}
}
void loop() {
CheckKeys();
delay(debounce);
}
void CheckKeys() {
for (int i = 0; i <= 7; i++) {
if (codetype[i] == 'M') {
if (digitalRead(k[i]) == 0) {
if (s[i] == 0) {
Consumer.press((ccode[i]));
s[i] = 1;
}
}
else {
if (s[i] == 1) {
s[i] = 0;
Consumer.release((ccode[i]));
}
}
}
if (codetype[i] == 'K') {
if (digitalRead(k[i]) == 0) {
if (s[i] == 0) {
Keyboard.press((kcode[i]));
s[i] = 1;
}
}
else {
if (s[i] == 1) {
s[i] = 0;
Keyboard.release((kcode[i]));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.