diff --git a/libraries/Keyboardio-Macros/src/MacroKeyDefs.h b/libraries/Keyboardio-Macros/src/MacroKeyDefs.h index 9efa9481..c96f5a4c 100644 --- a/libraries/Keyboardio-Macros/src/MacroKeyDefs.h +++ b/libraries/Keyboardio-Macros/src/MacroKeyDefs.h @@ -1,6 +1,6 @@ #pragma once -#define IS_MACRO B00000001 +#define IS_MACRO B00000100 #define M(n) (Key){ KEY_FLAGS|SYNTHETIC|IS_MACRO, n} #define Key_macroKey1 M(1) diff --git a/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h b/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h index b8479b27..3e0951d7 100644 --- a/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h +++ b/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h @@ -1,6 +1,6 @@ #pragma once -#define IS_MOUSE_KEY B00010000 +#define IS_MOUSE_KEY B00000010 // Synthetic, not internal #define KEY_MOUSE_BTN_L 0x01 // Synthetic key diff --git a/src/key_defs.h b/src/key_defs.h index 3d8ee3e4..f76a716f 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -19,7 +19,8 @@ typedef union { #define RALT_HELD B00000100 #define SHIFT_HELD B00001000 #define GUI_HELD B00010000 -#define SYNTHETIC B10000000 +#define SYNTHETIC B01000000 +#define RESERVED B10000000 #define LCTRL(k) ((Key) { k.flags | CTRL_HELD, k.rawKey }) #define LALT(k) ((Key) { k.flags | LALT_HELD, k.rawKey }) @@ -28,16 +29,15 @@ typedef union { #define LGUI(k) ((Key) { k.flags | GUI_HELD, k.rawKey }) // we assert that synthetic keys can never have keys held, so we reuse the _HELD bits -#define IS_SYSCTL B00000010 -#define IS_CONSUMER B00000100 -#define IS_INTERNAL B00001000 -#define SWITCH_TO_KEYMAP B00100000 -#define SWITCH_TO_KEYMAP_MOMENTARY B01000000 - +#define IS_SYSCTL B00000001 +#define IS_CONSUMER B00000010 +#define SWITCH_TO_KEYMAP B00000100 +#define SWITCH_TO_KEYMAP_MOMENTARY B00001000 +#define IS_INTERNAL B00010000 // IS_INTERNAL key table: -#define LED_TOGGLE 0x01 // Synthetic, internal +#define LED_TOGGLE B00000001 // Synthetic, internal #define KEYMAP_0 0 @@ -253,4 +253,4 @@ typedef union { -#define Key_LEDEffectNext (Key) { KEY_FLAGS | SYNTHETIC | IS_INTERNAL, LED_TOGGLE } +#define Key_LEDEffectNext (Key) { KEY_FLAGS | SYNTHETIC | IS_INTERNAL | LED_TOGGLE, 0 } diff --git a/src/key_events.cpp b/src/key_events.cpp index e2692f3d..dd0e3272 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -1,18 +1,53 @@ #include "key_events.h" -void handle_synthetic_key_event(Key mappedKey, uint8_t keyState) { +static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { + if (keymapEntry.flags & SWITCH_TO_KEYMAP_MOMENTARY ) { + if (key_toggled_on(keyState)) { + if ( keymapEntry.rawKey == KEYMAP_NEXT) { + temporary_keymap++; + } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { + temporary_keymap--; + } else { + temporary_keymap = keymapEntry.rawKey; + } + } + if (key_toggled_off(keyState)) { + temporary_keymap = primary_keymap; + } + + // switch keymap and stay there + } else if (key_toggled_on(keyState)) { + temporary_keymap = primary_keymap = keymapEntry.rawKey; + Storage.save_primary_keymap(primary_keymap); + } +} + +static bool handle_synthetic_key_event(Key mappedKey, uint8_t keyState) { + if (mappedKey.flags & RESERVED) + return false; + + if (!(mappedKey.flags & SYNTHETIC)) + return false; + if (!key_toggled_on(keyState)) - return; + return true; - if (mappedKey.flags & IS_CONSUMER) { - ConsumerControl.press(mappedKey.rawKey); - } else if (mappedKey.flags & IS_INTERNAL) { - if (mappedKey.rawKey == LED_TOGGLE) { + if (mappedKey.flags & IS_INTERNAL) { + if (mappedKey.flags & LED_TOGGLE) { LEDControl.next_mode(); + } else { + return false; } + } else if (mappedKey.flags & IS_CONSUMER) { + ConsumerControl.press(mappedKey.rawKey); } else if (mappedKey.flags & IS_SYSCTL) { SystemControl.press(mappedKey.rawKey); + } else if (mappedKey.flags & SWITCH_TO_KEYMAP || + mappedKey.flags & SWITCH_TO_KEYMAP_MOMENTARY) { + // Should not happen, handled elsewhere. } + + return true; } custom_handler_t eventHandlers[HOOK_MAX]; @@ -96,26 +131,3 @@ void release_key(Key mappedKey) { } Keyboard.release(mappedKey.rawKey); } - - -void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { - if (keymapEntry.flags & SWITCH_TO_KEYMAP_MOMENTARY ) { - if (key_toggled_on(keyState)) { - if ( keymapEntry.rawKey == KEYMAP_NEXT) { - temporary_keymap++; - } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { - temporary_keymap--; - } else { - temporary_keymap = keymapEntry.rawKey; - } - } - if (key_toggled_off(keyState)) { - temporary_keymap = primary_keymap; - } - - // switch keymap and stay there - } else if (key_toggled_on(keyState)) { - temporary_keymap = primary_keymap = keymapEntry.rawKey; - Storage.save_primary_keymap(primary_keymap); - } -} diff --git a/src/key_events.h b/src/key_events.h index 2bc6d799..cfa1faab 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -46,10 +46,8 @@ extern const Key keymaps[][ROWS][COLS]; void handle_key_event(Key mappedKey, byte row, byte col, uint8_t keyState); // Internal use -void handle_synthetic_key_event( Key mappedKey, uint8_t keyState); void press_key(Key mappedKey); void release_key(Key mappedKey); -void handle_keymap_key_event(Key keymapEntry, uint8_t keyState); bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t keyState); Key lookup_key(byte keymap, byte row, byte col);