diff --git a/examples/SpaceCadet/SpaceCadet.ino b/examples/SpaceCadet/SpaceCadet.ino index 7faace6e..de129903 100644 --- a/examples/SpaceCadet/SpaceCadet.ino +++ b/examples/SpaceCadet/SpaceCadet.ino @@ -47,15 +47,15 @@ void setup() { //Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS} static kaleidoscope::ModifierKeyMap spacecadetmap[] = { {Key_LeftShift, Key_LeftParen, 250} - ,{Key_RightShift, Key_RightParen, 250} - ,{Key_LeftGui, Key_LeftCurlyBracket, 250} - ,{Key_RightAlt, Key_RightCurlyBracket, 250} - ,{Key_LeftAlt, Key_RightCurlyBracket, 250} - ,{Key_LeftControl, Key_LeftBracket, 250} - ,{Key_RightControl, Key_RightBracket, 250} + , {Key_RightShift, Key_RightParen, 250} + , {Key_LeftGui, Key_LeftCurlyBracket, 250} + , {Key_RightAlt, Key_RightCurlyBracket, 250} + , {Key_LeftAlt, Key_RightCurlyBracket, 250} + , {Key_LeftControl, Key_LeftBracket, 250} + , {Key_RightControl, Key_RightBracket, 250} }; //Set the map and the number of entries - SpaceCadet.setMap(spacecadetmap, sizeof(spacecadetmap)/sizeof(spacecadetmap[0])); + SpaceCadet.setMap(spacecadetmap, sizeof(spacecadetmap) / sizeof(spacecadetmap[0])); Kaleidoscope.setup(); } diff --git a/src/Kaleidoscope/SpaceCadet.cpp b/src/Kaleidoscope/SpaceCadet.cpp index cc1c8018..c8b162da 100644 --- a/src/Kaleidoscope/SpaceCadet.cpp +++ b/src/Kaleidoscope/SpaceCadet.cpp @@ -21,158 +21,158 @@ namespace kaleidoscope { - //Default, empty constructor - ModifierKeyMap::ModifierKeyMap(){ - } - - //Constructor with input and output, and assume default timeout - ModifierKeyMap::ModifierKeyMap(Key input_, Key output_){ - input = input_; - output = output_; - } - - //Constructor with all three set - ModifierKeyMap::ModifierKeyMap(Key input_, Key output_, uint16_t timeout_) { - input = input_; - output = output_; - timeout = timeout_; - } - - //Space Cadet - uint8_t SpaceCadet::map_size_ = 0; - ModifierKeyMap * SpaceCadet::map_; - uint16_t SpaceCadet::time_out = 1000; - - //Empty constructor - SpaceCadet::SpaceCadet() { - //By default, we make one with left shift sending left paren, and right shift sending right paren - static ModifierKeyMap internalMap[] = { - //By default, respect the default timeout - {Key_LeftShift, Key_LeftParen, 0} - ,{Key_RightShift, Key_RightParen, 0} - //These may be uncommented, added, or set in the main sketch - /*,{Key_LeftGui,Key_LeftCurlyBracket,250} - ,{Key_RightAlt,Key_RightCurlyBracket,250} - ,{Key_LeftControl,Key_LeftBracket,250} - ,{Key_RightControl,Key_RightBracket,250}*/ - }; - - //Set the variables to our internal map - map_ = internalMap; - map_size_ = sizeof(internalMap)/sizeof(internalMap[0]); - } - - //Void function to reset the modifier map if desired. - void SpaceCadet::setMap(ModifierKeyMap * map, uint8_t map_size){ - //Set the map - map_ = map; - //set the map size to be the length of the array - map_size_ = map_size; - } - - void SpaceCadet::begin() { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); - } - - Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { - - // If nothing happened, bail out fast. - if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) { - return mapped_key; - } - - // If a key has been just toggled on... - if (keyToggledOn(key_state)) { - - if(map_size_ > 0) { - //This will only set one key, and if it isn't in our map it clears everything - //for the non-pressed key - for (uint8_t i = 0; i < map_size_; ++i) { - if(mapped_key.raw == map_[i].input.raw) { - //The keypress was valid and a match. - map_[i].flagged = true; - map_[i].start_time = millis(); - } else { - //The keypress wasn't a match. - map_[i].flagged = false; - map_[i].start_time = 0; - } - } - } - - // this is all we need to do on keypress, let the next handler do its thing too. - return mapped_key; - } - - // if the state is empty, that means that either the shifts weren't pressed, - // or we used another key in the interim. in both cases, nothing special to do. - bool valid_key = false; - bool pressed_key_was_valid = false; - uint8_t index = 0; - - if(map_size_ > 0) { - //Look to see if any keys in our map are flagged - for (uint8_t i = 0; i < map_size_; ++i) { - - if (map_[i].flagged) { - valid_key = true; - index = i; - } - if (map_[i].input.raw == mapped_key.raw) { - pressed_key_was_valid = true; - } - } - } +//Default, empty constructor +ModifierKeyMap::ModifierKeyMap() { +} - //If no valid mapped keys were pressed, simply return the keycode - if (!valid_key) { - return mapped_key; - } +//Constructor with input and output, and assume default timeout +ModifierKeyMap::ModifierKeyMap(Key input_, Key output_) { + input = input_; + output = output_; +} - //use the map index to find the local timeout for this key - uint16_t current_timeout = map_[index].timeout; - //If that isn't set, use the global timeout setting. - if(current_timeout == 0){ - current_timeout = time_out; - } +//Constructor with all three set +ModifierKeyMap::ModifierKeyMap(Key input_, Key output_, uint16_t timeout_) { + input = input_; + output = output_; + timeout = timeout_; +} - if ((millis() - map_[index].start_time) >= current_timeout) { - // if we timed out, that means we need to keep pressing the mapped - // key, but we won't need to send the alternative key in the end - map_[index].flagged = false; - map_[index].start_time = 0; - return mapped_key; - } +//Space Cadet +uint8_t SpaceCadet::map_size_ = 0; +ModifierKeyMap * SpaceCadet::map_; +uint16_t SpaceCadet::time_out = 1000; + +//Empty constructor +SpaceCadet::SpaceCadet() { + //By default, we make one with left shift sending left paren, and right shift sending right paren + static ModifierKeyMap internalMap[] = { + //By default, respect the default timeout + {Key_LeftShift, Key_LeftParen, 0} + , {Key_RightShift, Key_RightParen, 0} + //These may be uncommented, added, or set in the main sketch + /*,{Key_LeftGui,Key_LeftCurlyBracket,250} + ,{Key_RightAlt,Key_RightCurlyBracket,250} + ,{Key_LeftControl,Key_LeftBracket,250} + ,{Key_RightControl,Key_RightBracket,250}*/ + }; + + //Set the variables to our internal map + map_ = internalMap; + map_size_ = sizeof(internalMap) / sizeof(internalMap[0]); +} - // If the key that was pressed isn't one of our mapped keys, just - // return. This can happen when another key is released, and that should not - // interrupt us. +//Void function to reset the modifier map if desired. +void SpaceCadet::setMap(ModifierKeyMap * map, uint8_t map_size) { + //Set the map + map_ = map; + //set the map size to be the length of the array + map_size_ = map_size; +} - if (!pressed_key_was_valid) { - return mapped_key; - } +void SpaceCadet::begin() { + Kaleidoscope.useEventHandlerHook(eventHandlerHook); +} - // if a key toggled off (and that must be one of the mapped keys at this point), - // send the alternative key instead (if we were interrupted, we bailed out earlier). - if (keyToggledOff(key_state)) { - Key pressed_key = map_[index].input; - Key alternate_key = map_[index].output; - - //Since we are sending the actual key (no need for shift, etc), - //only need to send that key and not the original key. In fact, we - //may want to even UNSET the originally pressed key (future - //enhanacement?). This might also mean we don't need to return the - //key that was pressed, though I haven't confirmed that. - handleKeyswitchEvent(alternate_key, row, col, IS_PRESSED | INJECTED); - hid::sendKeyboardReport(); - - //Unflag the key so we don't try this again. - map_[index].flagged = false; - map_[index].start_time = 0; +Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { + + // If nothing happened, bail out fast. + if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) { + return mapped_key; + } + + // If a key has been just toggled on... + if (keyToggledOn(key_state)) { + + if (map_size_ > 0) { + //This will only set one key, and if it isn't in our map it clears everything + //for the non-pressed key + for (uint8_t i = 0; i < map_size_; ++i) { + if (mapped_key.raw == map_[i].input.raw) { + //The keypress was valid and a match. + map_[i].flagged = true; + map_[i].start_time = millis(); + } else { + //The keypress wasn't a match. + map_[i].flagged = false; + map_[i].start_time = 0; } + } + } - return mapped_key; + // this is all we need to do on keypress, let the next handler do its thing too. + return mapped_key; + } + + // if the state is empty, that means that either the shifts weren't pressed, + // or we used another key in the interim. in both cases, nothing special to do. + bool valid_key = false; + bool pressed_key_was_valid = false; + uint8_t index = 0; + + if (map_size_ > 0) { + //Look to see if any keys in our map are flagged + for (uint8_t i = 0; i < map_size_; ++i) { + + if (map_[i].flagged) { + valid_key = true; + index = i; + } + if (map_[i].input.raw == mapped_key.raw) { + pressed_key_was_valid = true; + } } + } + + //If no valid mapped keys were pressed, simply return the keycode + if (!valid_key) { + return mapped_key; + } + + //use the map index to find the local timeout for this key + uint16_t current_timeout = map_[index].timeout; + //If that isn't set, use the global timeout setting. + if (current_timeout == 0) { + current_timeout = time_out; + } + + if ((millis() - map_[index].start_time) >= current_timeout) { + // if we timed out, that means we need to keep pressing the mapped + // key, but we won't need to send the alternative key in the end + map_[index].flagged = false; + map_[index].start_time = 0; + return mapped_key; + } + + // If the key that was pressed isn't one of our mapped keys, just + // return. This can happen when another key is released, and that should not + // interrupt us. + + if (!pressed_key_was_valid) { + return mapped_key; + } + + // if a key toggled off (and that must be one of the mapped keys at this point), + // send the alternative key instead (if we were interrupted, we bailed out earlier). + if (keyToggledOff(key_state)) { + Key pressed_key = map_[index].input; + Key alternate_key = map_[index].output; + + //Since we are sending the actual key (no need for shift, etc), + //only need to send that key and not the original key. In fact, we + //may want to even UNSET the originally pressed key (future + //enhanacement?). This might also mean we don't need to return the + //key that was pressed, though I haven't confirmed that. + handleKeyswitchEvent(alternate_key, row, col, IS_PRESSED | INJECTED); + hid::sendKeyboardReport(); + + //Unflag the key so we don't try this again. + map_[index].flagged = false; + map_[index].start_time = 0; + } + + return mapped_key; +} } diff --git a/src/Kaleidoscope/SpaceCadet.h b/src/Kaleidoscope/SpaceCadet.h index 89146f21..56c384e7 100644 --- a/src/Kaleidoscope/SpaceCadet.h +++ b/src/Kaleidoscope/SpaceCadet.h @@ -21,44 +21,44 @@ #include namespace kaleidoscope { - //Declarations for the modifier key mapping - class ModifierKeyMap { - public: - //Empty constructor; set the vars separately - ModifierKeyMap(void); - //Constructor with input and output - ModifierKeyMap(Key input_, Key output_); - //Constructor with all three set - ModifierKeyMap(Key input_, Key output_, uint16_t timeout_); - //The key that is pressed - Key input; - //the key that is sent - Key output; - //The timeout (default to global timeout) - uint16_t timeout = 0; - //The flag (set to 0) - bool flagged = false; - //the start time for this key press - uint32_t start_time = 0; - }; +//Declarations for the modifier key mapping +class ModifierKeyMap { + public: + //Empty constructor; set the vars separately + ModifierKeyMap(void); + //Constructor with input and output + ModifierKeyMap(Key input_, Key output_); + //Constructor with all three set + ModifierKeyMap(Key input_, Key output_, uint16_t timeout_); + //The key that is pressed + Key input; + //the key that is sent + Key output; + //The timeout (default to global timeout) + uint16_t timeout = 0; + //The flag (set to 0) + bool flagged = false; + //the start time for this key press + uint32_t start_time = 0; +}; - //Declaration for the method (implementing KaleidoscopePlugin) - class SpaceCadet : public KaleidoscopePlugin { - public: - //Empty constructor - SpaceCadet(void); +//Declaration for the method (implementing KaleidoscopePlugin) +class SpaceCadet : public KaleidoscopePlugin { + public: + //Empty constructor + SpaceCadet(void); - //Methods - void setMap(ModifierKeyMap * map, uint8_t map_size); - void begin(void) final; + //Methods + void setMap(ModifierKeyMap * map, uint8_t map_size); + void begin(void) final; - static uint16_t time_out; + static uint16_t time_out; - private: - static uint8_t map_size_; - static ModifierKeyMap * map_; - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); - }; + private: + static uint8_t map_size_; + static ModifierKeyMap * map_; + static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); +}; }; extern kaleidoscope::SpaceCadet SpaceCadet;