From ad7f645e0177b623cbc817849e270b67f653fef7 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 23 Nov 2016 12:56:42 +0100 Subject: [PATCH] Add an INJECTED flag for keyswitch states The INJECTED flag can be used by handlers to determine where the event originated from: as a result of a direct keypress, or if it was injected into the event loop some other way. Signed-off-by: Gergely Nagy --- src/key_events.h | 4 +++- src/keyswitch_state.cpp | 8 ++++---- src/keyswitch_state.h | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/key_events.h b/src/key_events.h index 9efe45af..c59b9afa 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -39,7 +39,9 @@ extern const Key keymaps[][ROWS][COLS]; * For this reason, the handle_key_event receives four arguments: the mapped key * (or Key_NoKey if we do not want to override what is in the keymap), the row * and column of the key, so we can look up the code for it, and the current and - * previous state of the key, so we can determine what the event is. + * previous state of the key, so we can determine what the event is. The + * currentState may be flagged INJECTED, which signals that the event was + * injected, and is not a direct result of a keypress, coming from the scanner. */ void handle_key_event(Key mappedKey, byte row, byte col, uint8_t currentState, uint8_t previousState); diff --git a/src/keyswitch_state.cpp b/src/keyswitch_state.cpp index 90ea9cbb..d35a4569 100644 --- a/src/keyswitch_state.cpp +++ b/src/keyswitch_state.cpp @@ -11,7 +11,7 @@ boolean key_was_pressed (byte keyState) { } boolean key_was_pressed (uint8_t currentState, uint8_t previousState) { - if (previousState) { + if (previousState & B00000001) { return true; } else { return false; @@ -27,7 +27,7 @@ boolean key_is_pressed (byte keyState) { } boolean key_is_pressed (uint8_t currentState, uint8_t previousState) { - if (currentState) { + if (currentState & B00000001) { return true; } else { return false; @@ -43,7 +43,7 @@ boolean key_toggled_on(byte keyState) { } boolean key_toggled_on (uint8_t currentState, uint8_t previousState) { - if (currentState && ! previousState ) { + if ((currentState & B00000001) && ! (previousState & B00000001) ) { return true; } else { return false; @@ -60,7 +60,7 @@ boolean key_toggled_off(byte keyState) { } boolean key_toggled_off (uint8_t currentState, uint8_t previousState) { - if (previousState && ! currentState ) { + if ((previousState & B00000001) && ! (currentState & B00000001) ) { return true; } else { return false; diff --git a/src/keyswitch_state.h b/src/keyswitch_state.h index 13aa82d9..810b4e68 100644 --- a/src/keyswitch_state.h +++ b/src/keyswitch_state.h @@ -3,6 +3,7 @@ #include +#define INJECTED B10000000 boolean key_was_pressed (byte keyState); boolean key_is_pressed (byte keyState);