Use the triggering coordinates when injecting events

When we activate a OneShot key, use the coordinates of the (last) physical key
that triggered it. This is done by keeping an array of positions for each
possible OneShot key. While this costs us 16 bytes of RAM and a bit of code, the
benefit is that plugins ordered after OneShot will know where OneShot was
triggered from.

Fixes #13.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/389/head
Gergely Nagy 7 years ago
parent b685e8f8fe
commit a5ceb5833a

@ -33,6 +33,7 @@ Key OneShot::prev_key_;
bool OneShot::should_cancel_ = false; bool OneShot::should_cancel_ = false;
bool OneShot::should_cancel_stickies_ = false; bool OneShot::should_cancel_stickies_ = false;
bool OneShot::should_mask_on_interrupt_ = false; bool OneShot::should_mask_on_interrupt_ = false;
uint8_t OneShot::positions_[16];
// --- helper macros ------ // --- helper macros ------
@ -57,9 +58,15 @@ bool OneShot::should_mask_on_interrupt_ = false;
#define hasTimedOut() (millis () - start_time_ >= time_out) #define hasTimedOut() (millis () - start_time_ >= time_out)
void OneShot::positionToCoords(uint8_t pos, byte *row, byte *col) {
*col = pos % COLS;
*row = (pos - *col) / COLS;
}
// ---- OneShot stuff ---- // ---- OneShot stuff ----
void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) { void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) {
Key key; Key key;
byte row, col;
if (idx < 8) { if (idx < 8) {
key.flags = Key_LeftControl.flags; key.flags = Key_LeftControl.flags;
@ -69,7 +76,8 @@ void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) {
key.keyCode = LAYER_SHIFT_OFFSET + idx - 8; key.keyCode = LAYER_SHIFT_OFFSET + idx - 8;
} }
handleKeyswitchEvent(key, UNKNOWN_KEYSWITCH_LOCATION, key_state | INJECTED); positionToCoords(idx, &row, &col);
handleKeyswitchEvent(key, row, col, key_state | INJECTED);
} }
void OneShot::activateOneShot(uint8_t idx) { void OneShot::activateOneShot(uint8_t idx) {
@ -100,6 +108,7 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
} }
} else if (keyToggledOn(key_state)) { } else if (keyToggledOn(key_state)) {
start_time_ = millis(); start_time_ = millis();
positions_[idx] = row * COLS + col;
setPressed(idx); setPressed(idx);
setOneShot(idx); setOneShot(idx);
saveAsPrevious(mapped_key); saveAsPrevious(mapped_key);
@ -138,6 +147,7 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
} else { } else {
start_time_ = millis(); start_time_ = millis();
positions_[idx] = row * COLS + col;
setOneShot(idx); setOneShot(idx);
saveAsPrevious(mapped_key); saveAsPrevious(mapped_key);

@ -66,6 +66,9 @@ class OneShot : public KaleidoscopePlugin {
static bool should_cancel_; static bool should_cancel_;
static bool should_cancel_stickies_; static bool should_cancel_stickies_;
static bool should_mask_on_interrupt_; static bool should_mask_on_interrupt_;
static uint8_t positions_[16];
static void positionToCoords(uint8_t pos, byte *row, byte *col);
static void injectNormalKey(uint8_t idx, uint8_t key_state); static void injectNormalKey(uint8_t idx, uint8_t key_state);
static void activateOneShot(uint8_t idx); static void activateOneShot(uint8_t idx);

Loading…
Cancel
Save