Skip to content

Commit 37a4f96

Browse files
committed
Cache explicitly configured AutoShift key
Avoid searching for the mapping again if it is already known. Signed-off-by: Marco Herrn <[email protected]>
1 parent f298751 commit 37a4f96

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShift.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,16 @@ bool AutoShift::isExplicitlyMapped(Key key) {
104104
for (uint8_t i{0}; i < explicitmappings_count_; ++i) {
105105
LongPress mappedKey = cloneFromProgmem(explicitmappings_[i]);
106106
if (mappedKey.key == key) {
107+
// cache the mapped key to not have to search it again
108+
mapped_key_.key = mappedKey.key;
109+
mapped_key_.alternate_key = mappedKey.alternate_key;
107110
return true;
108111
}
109112
}
110113

111-
// If no matches were found, return false
114+
// If no matches were found, clear mapped_key_ and return false
115+
mapped_key_.key = Key_Transparent;
116+
mapped_key_.alternate_key = Key_Transparent;
112117
return false;
113118
}
114119

@@ -209,17 +214,10 @@ void AutoShift::flushEvent(bool is_long_press) {
209214
event.key = Runtime.lookupKey(event.addr);
210215

211216
// If we have an explicit mapping for that key, apply that.
212-
bool mapped= false;
213-
for (uint8_t i{0}; i < explicitmappings_count_; ++i) {
214-
LongPress mappedKey = cloneFromProgmem(explicitmappings_[i]);
215-
if (mappedKey.key == event.key) {
216-
event.key = mappedKey.alternate_key;
217-
mapped= true;
218-
}
219-
}
220-
221-
// If there was no explicit mapping, just add the shift modifier
222-
if (!mapped) {
217+
if (mapped_key_.key != Key_Transparent) {
218+
event.key = mapped_key_.alternate_key;
219+
} else {
220+
// If there was no explicit mapping, just add the shift modifier
223221
// event.key = longpresses[event.key]
224222
uint8_t flags = event.key.getFlags();
225223
flags ^= SHIFT_HELD;

plugins/Kaleidoscope-AutoShift/src/kaleidoscope/plugin/AutoShift.h

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ class AutoShift : public Plugin {
296296
// An array of LongPress objects in PROGMEM.
297297
LongPress const *explicitmappings_{nullptr};
298298
uint8_t explicitmappings_count_{0};
299+
300+
// A cache of the current explicit config key values, so we
301+
// don't have to keep looking them up from PROGMEM.
302+
LongPress mapped_key_ = { .key = Key_Transparent, .alternate_key = Key_Transparent };
299303
};
300304

301305
// =============================================================================

0 commit comments

Comments
 (0)