From 477dcb5aed232fb439097587e275f8eb33df93b2 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 15 Nov 2016 08:52:57 +0100 Subject: [PATCH] A way to restart the event handler with a different code There are scenarios where one would want to inject a keycode into the event handler chain, restart the processing from scratch, but with a keycode different than what the lookup would normally yield. For example, with one-shot modifiers, a feature one may wish is to be able to turn the one-shotness off, and have them act as normal modifiers. This is easily done, if we can remove the one-shot markup, and let the event handler process the resulting code as-is. This makes that possible: in a custom event handler, just call handle_key_event() with the first argument set to the desired code, and the rest left unchanged. This makes it possible to inject events: not just register keycodes with the HID, but inject synthetic events, something much more powerful. Signed-off-by: Gergely Nagy --- src/Model01.cpp | 4 ++-- src/TestMode.cpp | 2 +- src/TestMode.h | 2 +- src/hooks.h | 3 ++- src/key_events.cpp | 10 ++++++---- src/key_events.h | 4 ++-- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Model01.cpp b/src/Model01.cpp index f8fda144..7290b70f 100644 --- a/src/Model01.cpp +++ b/src/Model01.cpp @@ -107,12 +107,12 @@ void Model01::act_on_matrix_scan() { uint8_t keynum = (row*8)+(col); - handle_key_event(row, 7-col, + handle_key_event(Key_NoKey, row, 7-col, bitRead(leftHandState.all, keynum), bitRead(previousLeftHandState.all, keynum) ); - handle_key_event(row, (15- col), + handle_key_event(Key_NoKey, row, (15- col), bitRead(rightHandState.all, keynum), bitRead(previousRightHandState.all, keynum) ); diff --git a/src/TestMode.cpp b/src/TestMode.cpp index 5d10d4a6..4edc0239 100644 --- a/src/TestMode.cpp +++ b/src/TestMode.cpp @@ -61,7 +61,7 @@ void TestMode_::TestMatrix () { void TestMode_::setup() { eventHandlers[0] = handle_key_event_test; } -bool handle_key_event_test(byte row, byte col, uint8_t currentState, uint8_t previousState) { +bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState) { Serial.write(row); } diff --git a/src/TestMode.h b/src/TestMode.h index 3d99d465..4dcf8cf8 100644 --- a/src/TestMode.h +++ b/src/TestMode.h @@ -15,4 +15,4 @@ class TestMode_ { extern TestMode_ TestMode; -bool handle_key_event_test(byte row, byte col, uint8_t currentState, uint8_t previousState); +bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); diff --git a/src/hooks.h b/src/hooks.h index 93b4057d..cec4da50 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -1,10 +1,11 @@ #pragma once #include +#include "key_defs.h" #define HOOK_MAX 64 -typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState); +typedef bool (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); extern custom_handler_t eventHandlers[HOOK_MAX]; void event_handler_hook_add (custom_handler_t hook); diff --git a/src/key_events.cpp b/src/key_events.cpp index d1753ba5..09c24aa6 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -49,19 +49,21 @@ Key lookup_key(byte keymap, byte row, byte col) { return mappedKey; } -void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { +void handle_key_event(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState) { for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) { custom_handler_t handler = eventHandlers[i]; - if ((*handler)(row, col, currentState, previousState)) + if ((*handler)(mappedKey, row, col, currentState, previousState)) return; } } -bool handle_key_event_default(byte row, byte col, uint8_t currentState, uint8_t previousState) { +bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState) { //for every newly pressed button, figure out what logical key it is and send a key down event // for every newly released button, figure out what logical key it is and send a key up event - Key mappedKey = lookup_key(temporary_keymap, row, col); + if (mappedKey.raw == Key_NoKey.raw) { + mappedKey = lookup_key(temporary_keymap, row, col); + } Key baseKey = lookup_key(primary_keymap, row, col); if ((baseKey.flags & SWITCH_TO_KEYMAP diff --git a/src/key_events.h b/src/key_events.h index 633f9f0a..3b71e073 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -16,10 +16,10 @@ extern const Key keymaps[][ROWS][COLS]; // sending events to the computer void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState); -void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState); +void handle_key_event(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); void press_key(Key mappedKey); void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState); void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState); -bool handle_key_event_default(byte row, byte col, uint8_t currentState, uint8_t previousState); +bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); Key lookup_key(byte keymap, byte row, byte col);