diff --git a/src/key_events.cpp b/src/key_events.cpp index b614e2df..ca1b69e3 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -45,13 +45,18 @@ void handleKeyswitchEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { * In particular, doing them for keypresses with out-of-bounds (row,col) * would cause out-of-bounds array accesses in Layer.lookup(), * Layer.updateLiveCompositeKeymap(), etc. - * Perhaps this check should instead be for INJECTED - I'm not clear on - * whether keypresses with out-of-bounds (row,col) are the same as - * keypresses with INJECTED set, or are a superset or a subset of - * INJECTED keypresses. In any case, the (row,col) test is *safe* in that - * it avoids out-of-bounds accesses, at least in core. (Can't promise - * anything about event handlers - they may still receive out-of-bounds - * (row,col), and handling that properly is on them.) + * Note that many INJECTED keypresses use the UNKNOWN_KEYSWITCH_LOCATION macro + * which gives us row==255, col==255 here. Therefore, it's legitimate that + * we may have keypresses with out-of-bounds (row, col). + * However, some INJECTED keypresses do have valid (row, col) if they are + * injecting an event tied to a physical keyswitch - and we want them to go + * through this lookup. + * So we can't just test for INJECTED here, we need to test the row and col + * directly. + * Note also that this (row, col) test avoids out-of-bounds accesses in *core*, + * but doesn't guarantee anything about event handlers - event handlers may + * still receive out-of-bounds (row, col), and handling that properly is on + * them. */ if (row < ROWS && col < COLS) { @@ -75,7 +80,11 @@ void handleKeyswitchEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { } } - if (!(keyState & INJECTED)) { + /* Convert (row, col) to the correct mappedKey + * The condition here means that if mappedKey and (row, col) are both valid, + * the mappedKey wins - we don't re-look-up the mappedKey + */ + if (mappedKey.raw == Key_NoKey.raw) { mappedKey = Layer.lookup(row, col); } diff --git a/src/key_events.h b/src/key_events.h index a6af8b29..6bfa62cf 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -11,6 +11,9 @@ extern const Key keymaps[][ROWS][COLS]; // Code can use this macro on injected key events to signal that // the event isn't tied to a specific physical keyswitch #define UNKNOWN_KEYSWITCH_LOCATION 255,255 +// Conversely, if an injected event *is* tied to a physical keyswitch and should +// be resolved by the current keymap, code can use Key_NoKey on the injected event +// with a real (row, col) location // sending events to the computer /* The event handling starts with the Scanner calling handleKeyswitchEvent() for