From a3cca30e1f90ef0640b3a797efef9a1202a7082d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 10 Mar 2017 22:58:13 +0100 Subject: [PATCH] Implement mouse wheel support With these changes, the vertical scrolling of the mouse wheel is now properly supported. It has no acceleration, because the wheel doesn't have one either. It has a delay, however, which I tried to tune to a reasonable speed. Signed-off-by: Gergely Nagy --- src/Kaleidoscope-MouseKeys.cpp | 26 ++++++++++++++++++++++++-- src/Kaleidoscope-MouseKeys.h | 4 ++++ src/MouseKeyDefs.h | 5 +++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Kaleidoscope-MouseKeys.cpp b/src/Kaleidoscope-MouseKeys.cpp index 50b4203f..d330c051 100644 --- a/src/Kaleidoscope-MouseKeys.cpp +++ b/src/Kaleidoscope-MouseKeys.cpp @@ -12,8 +12,24 @@ uint16_t MouseKeys_::speedDelay = 0; uint8_t MouseKeys_::accelSpeed = 1; uint16_t MouseKeys_::accelDelay = 50; +uint8_t MouseKeys_::wheelSpeed = 1; +uint16_t MouseKeys_::wheelDelay = 50; + uint32_t MouseKeys_::accelEndTime; uint32_t MouseKeys_::endTime; +uint32_t MouseKeys_::wheelEndTime; + +void MouseKeys_::scrollWheel(uint8_t keyCode) { + if (millis() < wheelEndTime) + return; + + wheelEndTime = millis() + wheelDelay; + + if (keyCode & KEY_MOUSE_UP) + Mouse.move(0, 0, wheelSpeed); + else if (keyCode & KEY_MOUSE_DOWN) + Mouse.move(0, 0, -wheelSpeed); +} void MouseKeys_::loopHook(bool postClear) { if (postClear) { @@ -69,9 +85,15 @@ Key MouseKeys_::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyS if (key_toggled_on(keyState)) { endTime = millis() + speedDelay; accelEndTime = millis() + accelDelay; + wheelEndTime = 0; + } + if (key_is_pressed(keyState)) { + if (mappedKey.keyCode & KEY_MOUSE_WHEEL) { + scrollWheel (mappedKey.keyCode); + } + else + mouseMoveIntent |= mappedKey.keyCode; } - if (key_is_pressed(keyState)) - mouseMoveIntent |= mappedKey.keyCode; } else if (key_toggled_on(keyState)) { if (mappedKey.keyCode & KEY_MOUSE_WARP && mappedKey.flags & IS_MOUSE_KEY) { // we don't pass in the left and up values because those are the diff --git a/src/Kaleidoscope-MouseKeys.h b/src/Kaleidoscope-MouseKeys.h index e94d5ea9..4b309cd7 100644 --- a/src/Kaleidoscope-MouseKeys.h +++ b/src/Kaleidoscope-MouseKeys.h @@ -13,12 +13,16 @@ class MouseKeys_ : public KaleidoscopePlugin { static uint16_t speedDelay; static uint8_t accelSpeed; static uint16_t accelDelay; + static uint8_t wheelSpeed; + static uint16_t wheelDelay; private: static uint8_t mouseMoveIntent; static uint32_t endTime; static uint32_t accelEndTime; + static uint32_t wheelEndTime; + static void scrollWheel(uint8_t keyCode); static void loopHook(bool postClear); static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); }; diff --git a/src/MouseKeyDefs.h b/src/MouseKeyDefs.h index de4d82c0..4b998dc0 100644 --- a/src/MouseKeyDefs.h +++ b/src/MouseKeyDefs.h @@ -15,6 +15,7 @@ #define KEY_MOUSE_BUTTON B0010000 #define KEY_MOUSE_WARP B0100000 #define KEY_MOUSE_WARP_END B0010000 +#define KEY_MOUSE_WHEEL B1000000 #define Key_mouseWarpNW (Key) { KEY_MOUSE_WARP| KEY_MOUSE_UP | KEY_MOUSE_LEFT, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } @@ -32,8 +33,8 @@ #define Key_mouseDnL (Key) { KEY_MOUSE_DOWN | KEY_MOUSE_LEFT, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } #define Key_mouseDn (Key) { KEY_MOUSE_DOWN, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } #define Key_mouseDnR (Key) { KEY_MOUSE_DOWN | KEY_MOUSE_RIGHT, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } -#define Key_mouseScrollUp -#define Key_mouseScrollDn +#define Key_mouseScrollUp (Key) { KEY_MOUSE_WHEEL | KEY_MOUSE_UP, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } +#define Key_mouseScrollDn (Key) { KEY_MOUSE_WHEEL | KEY_MOUSE_DOWN, KEY_FLAGS|SYNTHETIC|IS_MOUSE_KEY } #define Key_mouseScrollL #define Key_mouseScrollR #define Key_mouseBtnL (Key) { KEY_MOUSE_BUTTON | KEY_MOUSE_BTN_L, KEY_FLAGS | SYNTHETIC | IS_MOUSE_KEY }