Created
October 7, 2023 18:47
-
-
Save xinhaoyuan/d16131e0edd46cf24e5c3d5554369ca8 to your computer and use it in GitHub Desktop.
QMK mouse key: separate x/y accelerations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/quantum/mousekey.c b/quantum/mousekey.c | |
index df8aa613be..7e451c517a 100644 | |
--- a/quantum/mousekey.c | |
+++ b/quantum/mousekey.c | |
@@ -35,7 +35,8 @@ static inline int8_t times_inv_sqrt2(int8_t x) { | |
static report_mouse_t mouse_report = {0}; | |
static void mousekey_debug(void); | |
static uint8_t mousekey_accel = 0; | |
-static uint8_t mousekey_repeat = 0; | |
+static uint8_t mousekey_repeat_x = 0; | |
+static uint8_t mousekey_repeat_y = 0; | |
static uint8_t mousekey_wheel_repeat = 0; | |
#ifdef MOUSEKEY_INERTIA | |
static uint8_t mousekey_frame = 0; // track whether gesture is inactive, first frame, or repeating | |
@@ -87,7 +88,7 @@ uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX; | |
/* Default accelerated mode */ | |
-static uint8_t move_unit(void) { | |
+static uint8_t move_unit(uint8_t repeat) { | |
uint16_t unit; | |
if (mousekey_accel & (1 << 0)) { | |
unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4; | |
@@ -95,12 +96,12 @@ static uint8_t move_unit(void) { | |
unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2; | |
} else if (mousekey_accel & (1 << 2)) { | |
unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed); | |
- } else if (mousekey_repeat == 0) { | |
+ } else if (repeat == 0) { | |
unit = MOUSEKEY_MOVE_DELTA; | |
- } else if (mousekey_repeat >= mk_time_to_max) { | |
+ } else if (repeat >= mk_time_to_max) { | |
unit = MOUSEKEY_MOVE_DELTA * mk_max_speed; | |
} else { | |
- unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max; | |
+ unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * repeat) / mk_time_to_max; | |
} | |
return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit)); | |
} | |
@@ -325,10 +326,12 @@ void mousekey_task(void) { | |
# else // default acceleration | |
- if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) { | |
- if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; | |
- if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1); | |
- if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1); | |
+ if ((tmpmr.x && timer_elapsed(last_timer_c) > (mousekey_repeat_x ? mk_interval : mk_delay * 10)) || | |
+ (tmpmr.y && timer_elapsed(last_timer_c) > (mousekey_repeat_y ? mk_interval : mk_delay * 10))) { | |
+ if (tmpmr.x != 0) mouse_report.x = move_unit(mousekey_repeat_x) * ((tmpmr.x > 0) ? 1 : -1); | |
+ if (tmpmr.y != 0) mouse_report.y = move_unit(mousekey_repeat_y) * ((tmpmr.y > 0) ? 1 : -1); | |
+ if (tmpmr.x && mousekey_repeat_x != UINT8_MAX) mousekey_repeat_x++; | |
+ if (tmpmr.y && mousekey_repeat_y != UINT8_MAX) mousekey_repeat_y++; | |
/* diagonal move [1/sqrt(2)] */ | |
if (mouse_report.x && mouse_report.y) { | |
@@ -346,9 +349,9 @@ void mousekey_task(void) { | |
# endif // MOUSEKEY_INERTIA or not | |
if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) { | |
- if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++; | |
if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1); | |
if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1); | |
+ if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++; | |
/* diagonal move [1/sqrt(2)] */ | |
if (mouse_report.v && mouse_report.h) { | |
@@ -390,14 +393,19 @@ void mousekey_on(uint8_t code) { | |
# else // no inertia | |
- if (code == KC_MS_UP) | |
- mouse_report.y = move_unit() * -1; | |
- else if (code == KC_MS_DOWN) | |
- mouse_report.y = move_unit(); | |
- else if (code == KC_MS_LEFT) | |
- mouse_report.x = move_unit() * -1; | |
- else if (code == KC_MS_RIGHT) | |
- mouse_report.x = move_unit(); | |
+ if (code == KC_MS_UP) { | |
+ if (mouse_report.y >= 0) mousekey_repeat_y = 0; | |
+ mouse_report.y = move_unit(mousekey_repeat_y) * -1; | |
+ } else if (code == KC_MS_DOWN) { | |
+ if (mouse_report.y <= 0) mousekey_repeat_y = 0; | |
+ mouse_report.y = move_unit(mousekey_repeat_y); | |
+ } else if (code == KC_MS_LEFT) { | |
+ if (mouse_report.x >= 0) mousekey_repeat_x = 0; | |
+ mouse_report.x = move_unit(mousekey_repeat_x) * -1; | |
+ } else if (code == KC_MS_RIGHT) { | |
+ if (mouse_report.x <= 0) mousekey_repeat_x = 0; | |
+ mouse_report.x = move_unit(mousekey_repeat_x); | |
+ } | |
# endif // inertia or not | |
@@ -461,12 +469,12 @@ void mousekey_off(uint8_t code) { | |
mousekey_accel &= ~(1 << 1); | |
else if (code == KC_MS_ACCEL2) | |
mousekey_accel &= ~(1 << 2); | |
- if (mouse_report.x == 0 && mouse_report.y == 0) { | |
- mousekey_repeat = 0; | |
+ if (mouse_report.x == 0) mousekey_repeat_x = 0; | |
+ if (mouse_report.y == 0) mousekey_repeat_y = 0; | |
# ifdef MK_KINETIC_SPEED | |
+ if (mouse_report.x == 0 && mouse_report.y == 0) | |
mouse_timer = 0; | |
# endif /* #ifdef MK_KINETIC_SPEED */ | |
- } | |
if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0; | |
} | |
@@ -613,7 +621,8 @@ void mousekey_send(void) { | |
void mousekey_clear(void) { | |
mouse_report = (report_mouse_t){}; | |
- mousekey_repeat = 0; | |
+ mousekey_repeat_x = 0; | |
+ mousekey_repeat_y = 0; | |
mousekey_wheel_repeat = 0; | |
mousekey_accel = 0; | |
#ifdef MOUSEKEY_INERTIA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment