#pragma once #include #include #include "Mode.h" class ModeKeyboard : public Mode { protected: byte code; bool stroke2; bool special; bool control_hold; bool super_hold; bool alt_hold; bool shift_hold; bool nextReset; public: ModeKeyboard() { reset(); } void reset() { stroke2 = false; special = false; nextReset = false; digitalWrite(LED, 1); } Mode * update(unsigned int keys) { digitalWrite(LED, 1); if (keys & KEY_STAR) return Mode::modes[0]; if (keys & KEY_HASH) { if (nextReset) { /* hash was pressed twice, reset */ reset(); return NULL; } nextReset = true; if (!special) { special = true; } return NULL; } nextReset = false; if (special) { stroke2 = false; special = false; handleSpecial(keys); return NULL; } if (!stroke2) { digitalWrite(LED, 0); code = 10 * keyToNumber(keys); stroke2 = true; } else { stroke2 = false; code += keyToNumber(keys); transmitCode(code); if (control_hold) { Keyboard.release(KEY_LEFT_CTRL); control_hold = false; } if (alt_hold) { Keyboard.release(KEY_LEFT_ALT); alt_hold = false; } if (super_hold) { Keyboard.release(KEY_LEFT_GUI); super_hold = false; } if (shift_hold) { Keyboard.release(KEY_LEFT_SHIFT); shift_hold = false; } } return NULL; } byte keyToNumber(unsigned int keys) { if (keys & KEY_0) return 0; if (keys & KEY_1) return 1; if (keys & KEY_2) return 2; if (keys & KEY_3) return 3; if (keys & KEY_4) return 4; if (keys & KEY_5) return 5; if (keys & KEY_6) return 6; if (keys & KEY_7) return 7; if (keys & KEY_8) return 8; if (keys & KEY_9) return 9; return 0; } void transmitCode(byte code) { if (code == 99) Keyboard.write(KEY_BACKSPACE); else if (code == 98) Keyboard.write(KEY_TAB); else if (code == 97) Keyboard.write(KEY_ESC); else if (code == 96) Keyboard.write(KEY_RETURN); else if (code == 95) Keyboard.write(KEY_TAB); else Keyboard.write(code + 32); } void handleSpecial(unsigned int keys) { if (keys & KEY_4) { Keyboard.press(KEY_LEFT_SHIFT); shift_hold = true; } if (keys & KEY_7) { Keyboard.press(KEY_LEFT_CTRL); control_hold = true; } if (keys & KEY_8) { Keyboard.press(KEY_LEFT_GUI); super_hold = true; } if (keys & KEY_9) { Keyboard.press(KEY_LEFT_ALT); alt_hold = true; } } };