From 7eabe867a4834cd2fa49257104c5b40c608e9b0d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 29 Jul 2018 21:07:18 +0200 Subject: [PATCH] Stop moving into a given direction when releasing a mouse key While we do clear the report every cycle, similar to how key release events are explicitly removed from the report, mouse movements should get removed too. This makes it possible to use them in macros reliably, without surprising results (an extra report sent at the end of the macro). Signed-off-by: Gergely Nagy --- src/Kaleidoscope-MouseKeys.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Kaleidoscope-MouseKeys.cpp b/src/Kaleidoscope-MouseKeys.cpp index 7e643201..cafd7ede 100644 --- a/src/Kaleidoscope-MouseKeys.cpp +++ b/src/Kaleidoscope-MouseKeys.cpp @@ -114,6 +114,34 @@ kaleidoscope::EventHandlerResult MouseKeys_::onKeyswitchEvent(Key &mappedKey, by } else { mouseMoveIntent |= mappedKey.keyCode; } + } else if (keyToggledOff(keyState)) { + /* If a mouse key toggles off, we want to explicitly stop moving (or + * scrolling) in that direction. We want to do this to support use-cases + * where we send multiple reports per cycle (such as macros), and can't + * rely on the main loop clearing the report for us. We do not want to + * clear the whole report either, because we want any other mouse keys + * to still have their desired effect. Therefore, we selectively stop + * movement or scrolling. */ + mouseMoveIntent &= ~mappedKey.keyCode; + bool x = false, y = false, vWheel = false, hWheel = false; + + if (mappedKey.keyCode & KEY_MOUSE_UP || + mappedKey.keyCode & KEY_MOUSE_DOWN) { + if (mappedKey.keyCode & KEY_MOUSE_WHEEL) { + vWheel = true; + } else { + y = true; + } + } else if (mappedKey.keyCode & KEY_MOUSE_LEFT || + mappedKey.keyCode & KEY_MOUSE_RIGHT) { + if (mappedKey.keyCode & KEY_MOUSE_WHEEL) { + hWheel = true; + } else { + x = true; + } + } + + kaleidoscope::hid::stopMouse(x, y, vWheel, hWheel); } } else if (keyToggledOn(keyState)) { if (mappedKey.keyCode & KEY_MOUSE_WARP && mappedKey.flags & IS_MOUSE_KEY) {