diff --git a/src/lib/key-functions.c b/src/lib/key-functions.c index 53c6011..48517b6 100644 --- a/src/lib/key-functions.c +++ b/src/lib/key-functions.c @@ -18,78 +18,102 @@ #include "key-functions.h" +// ---------------------------------------------------------------------------- -void kbfun_press( KBFUN_FUNCTION_ARGS ) { +/* + * Generate a normal keypress or keyrelease + * + * Arguments + * - keycode: the keycode to use + * - pressed: whether to generate a keypress (true) or keyrelease (false) + * + * Note + * - Because of the way USB does things, what this actually does is either add + * or remove 'keycode' from the list of currently pressed keys + */ +static void _press_release(uint8_t keycode, bool pressed) { // no-op - if (keycode_ == 0) + if (keycode == 0) return; // modifier keys - switch (keycode_) { - case KEY_LeftControl: keyboard_modifier_keys |= (1<<0); + switch (keycode) { + case KEY_LeftControl: (pressed) + ? (keyboard_modifier_keys |= (1<<0)) + : (keyboard_modifier_keys &= ~(1<<0)); return; - case KEY_LeftShift: keyboard_modifier_keys |= (1<<1); + case KEY_LeftShift: (pressed) + ? (keyboard_modifier_keys |= (1<<1)) + : (keyboard_modifier_keys &= ~(1<<1)); return; - case KEY_LeftAlt: keyboard_modifier_keys |= (1<<2); + case KEY_LeftAlt: (pressed) + ? (keyboard_modifier_keys |= (1<<2)) + : (keyboard_modifier_keys &= ~(1<<2)); return; - case KEY_LeftGUI: keyboard_modifier_keys |= (1<<3); + case KEY_LeftGUI: (pressed) + ? (keyboard_modifier_keys |= (1<<3)) + : (keyboard_modifier_keys &= ~(1<<3)); return; - case KEY_RightControl: keyboard_modifier_keys |= (1<<4); + case KEY_RightControl: (pressed) + ? (keyboard_modifier_keys |= (1<<4)) + : (keyboard_modifier_keys &= ~(1<<4)); return; - case KEY_RightShift: keyboard_modifier_keys |= (1<<5); + case KEY_RightShift: (pressed) + ? (keyboard_modifier_keys |= (1<<5)) + : (keyboard_modifier_keys &= ~(1<<5)); return; - case KEY_RightAlt: keyboard_modifier_keys |= (1<<6); + case KEY_RightAlt: (pressed) + ? (keyboard_modifier_keys |= (1<<6)) + : (keyboard_modifier_keys &= ~(1<<6)); return; - case KEY_RightGUI: keyboard_modifier_keys |= (1<<7); + case KEY_RightGUI: (pressed) + ? (keyboard_modifier_keys |= (1<<7)) + : (keyboard_modifier_keys &= ~(1<<7)); return; } // all others - for (uint8_t i=0; i<6; i++) - if (keyboard_keys[i] == 0) { - keyboard_keys[i] = keycode_; - return; + for (uint8_t i=0; i<6; i++) { + if (pressed) { + if (keyboard_keys[i] == 0) { + keyboard_keys[i] = keycode; + return; + } + } else { + if (keyboard_keys[i] == keycode) { + keyboard_keys[i] = 0; + return; + } } + } } +// ---------------------------------------------------------------------------- + +/* + * - press + * - generate a normal keypress + */ +void kbfun_press( KBFUN_FUNCTION_ARGS ) { + _press_release(keycode_, true); +} + +/* + * - release + * - generate a normal keyrelease + */ void kbfun_release( KBFUN_FUNCTION_ARGS ) { - // no-op - if (keycode_ == 0) - return; - - // modifier keys - switch (keycode_) { - case KEY_LeftControl: keyboard_modifier_keys &= ~(1<<0); - return; - case KEY_LeftShift: keyboard_modifier_keys &= ~(1<<1); - return; - case KEY_LeftAlt: keyboard_modifier_keys &= ~(1<<2); - return; - case KEY_LeftGUI: keyboard_modifier_keys &= ~(1<<3); - return; - case KEY_RightControl: keyboard_modifier_keys &= ~(1<<4); - return; - case KEY_RightShift: keyboard_modifier_keys &= ~(1<<5); - return; - case KEY_RightAlt: keyboard_modifier_keys &= ~(1<<6); - return; - case KEY_RightGUI: keyboard_modifier_keys &= ~(1<<7); - return; - } - - // all others - for (uint8_t i=0; i<6; i++) - if (keyboard_keys[i] == keycode_) { - keyboard_keys[i] = 0; - return; - } + _press_release(keycode_, false); } // TODO: -// - implement two shifts => capslock // - implement layer lock key combos (make a function to switch to a specific // layer) +/* + * - next layer + * - layer increment (for all keys) + */ void kbfun_layer_inc( KBFUN_FUNCTION_ARGS ) { for (uint8_t row=0; row capslock + * - when assigned to two keys (e.g. the physical left and right shift keys), + * pressing and holding down one of them makes the second toggle capslock + */ +void kbfun_2_keys_capslock_press_release( KBFUN_FUNCTION_ARGS ) { + static uint8_t keys_pressed = 0; + + if (!pressed_) keys_pressed--; + + _press_release(keycode_, pressed_); + if (keys_pressed == 1) + _press_release(KEY_CapsLock, pressed_); + + if (pressed_) keys_pressed++; +} + diff --git a/src/lib/key-functions.h b/src/lib/key-functions.h index b709474..7b80874 100644 --- a/src/lib/key-functions.h +++ b/src/lib/key-functions.h @@ -35,7 +35,7 @@ #define KBFUN_FUNCTION_ARGS \ - uint8_t keycode_, \ + uint8_t keycode_, bool pressed_, \ uint8_t (*current_layers_)[KB_ROWS][KB_COLUMNS], \ uint8_t (*pressed_layers_)[KB_ROWS][KB_COLUMNS], \ uint8_t * row_, uint8_t * col_ diff --git a/src/main.c b/src/main.c index 340873a..56ea6b5 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,7 @@ int main(void) { if (press_function) { (*press_function)( kb_layout_get(current_layer, row, col), + true, ¤t_layers, &pressed_layers, &row, &col ); } @@ -75,6 +76,7 @@ int main(void) { if (release_function) { (*release_function)( kb_layout_get(pressed_layer, row, col), + false, ¤t_layers, &pressed_layers, &row, &col ); }