From 7d024cabc0c20d1487c4a2fe4d3827930280b6a5 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 25 Jan 2017 11:31:12 +0100 Subject: [PATCH] Add an .isModifierActive method Useful when writing an event handler that needs to check if a modifier is active, but may run before OneShot has a chance to re-register the given modifier. Signed-off-by: Gergely Nagy --- README.md | 6 ++++++ src/Akela/OneShot.cpp | 10 +++++++++- src/Akela/OneShot.h | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fab8601..6ff09727 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,12 @@ modifiers and one-shot layer keys. It has the following methods: > useful for macros that need to fiddle with either modifier or layer state: if > one-shots are not active, they need not restore the original state. +### `.isModifierActive(key)` + +> Returns if the modifier `key` has a one-shot state active. Use this together +> with `Keyboard.isModifierActive` to catch cases where a one-shot modifier is +> active, but not registered yet. + ### `.cancel([withStickies])` > The `cancel()` method can be used to cancel any pending one-shot effects, diff --git a/src/Akela/OneShot.cpp b/src/Akela/OneShot.cpp index eccf9bf2..e6865497 100644 --- a/src/Akela/OneShot.cpp +++ b/src/Akela/OneShot.cpp @@ -38,7 +38,7 @@ namespace Akela { #define isModifier(key) (key.raw >= Key_LCtrl.raw && key.raw <= Key_RGUI.raw) #define isLayerKey(key) (key.flags == (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP) && key.keyCode >= MOMENTARY_OFFSET && key.keyCode <= MOMENTARY_OFFSET + 23) -#define isOneShot(idx) (bitRead (State, idx)) +#define isOneShot(idx) (bitRead (State, (idx))) #define setOneShot(idx) (bitWrite (State, idx, 1)) #define clearOneShot(idx) (bitWrite (State, idx, 0)) @@ -258,6 +258,14 @@ namespace Akela { return (State && !hasTimedOut () && !shouldCancel); } + bool + OneShot::isModifierActive (Key key) { + if (key.raw < Key_LCtrl.raw || key.raw > Key_RGUI.raw) + return false; + + return isOneShot (key.keyCode - Key_LCtrl.keyCode); + } + void OneShot::cancel (bool withStickies) { shouldCancel = true; diff --git a/src/Akela/OneShot.h b/src/Akela/OneShot.h index c35881b7..2db14f48 100644 --- a/src/Akela/OneShot.h +++ b/src/Akela/OneShot.h @@ -37,6 +37,8 @@ namespace Akela { static void cancel (bool withStickies); static void cancel (void) { cancel (false); }; + static bool isModifierActive (Key key); + void inject (Key key, uint8_t keyState); private: