// Some more meaningful names for referencing applicable I/O pins #define FIRE_PIN 2 #define UP_PIN 3 #define DOWN_PIN 16 #define RIGHT_PIN 15 #define LEFT_PIN 14 // Arduino-friendly (value+136) keyboard values of direction keys of numeric keypad #define KEY_PAD_UP 232 #define KEY_PAD_DOWN 226 #define KEY_PAD_RIGHT 230 #define KEY_PAD_LEFT 228 // Map a keyboard value to the input pin. First Pins[] value is first Keys[] value, etc byte Pins[] = {FIRE_PIN, UP_PIN, DOWN_PIN, RIGHT_PIN, LEFT_PIN }; byte Keys[] = {KEY_RIGHT_CTRL, KEY_PAD_UP, KEY_PAD_DOWN, KEY_PAD_RIGHT, KEY_PAD_LEFT}; void setup() { // pullup all button input pins to high for (int i = 0; i <= sizeof(Pins); i++) { pinMode(Pins[i], INPUT_PULLUP); } Keyboard.begin(); } void loop() { // Press any keys that need to be pressed { int keyStates = 0; for (int i = 0; i <= sizeof(Pins); i++) { if (digitalRead(Pins[i]) == LOW) { Keyboard.press(Keys[i]); keyStates = 1; } } if (keyStates == 1) { // pause a moment to give any pressed keys a chance to report, and possibly ignore some joystick bounce as well delay(10); } } // Release any keys that need to be released for (int i = 0; i <= sizeof(Pins); i++) { if (digitalRead(Pins[i]) == HIGH) { Keyboard.release(Keys[i]); } } // If no buttons are being pressed, releaseAll() to ensure no keys are stuck down { int pinStates = 1; for (int i = 0; i <= sizeof(Pins); i++) { pinStates = pinStates & digitalRead(Pins[i]); } if (pinStates == HIGH) { Keyboard.releaseAll(); } } } // End of Loop();