Is there a way to trigger a script by activating the mouse scrollwheel X times in Y ms?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
-Garfle-
Posts: 19
Joined: 07 Feb 2024, 07:46

Is there a way to trigger a script by activating the mouse scrollwheel X times in Y ms?

Post by -Garfle- » 02 Apr 2024, 06:51

Short version - can AHK detect and use as a trigger something like 3 scroll wheel scrolls within 200 ms?

Longer version:

I love my Logitech MX Master 3s mouse, and one of its helpful features is the infinite scroll - it's a feature I catch myself trying to invoke on other mice. I was wondering if I could replicate it via an AHK script, but would really appreciate help.

First I want to see if the trigger is possible.

To trigger the function on the MX Master, you flick the scroll wheel. I think I could replicate this by seeing how many instances of "scroll wheel up and "scroll wheel down" a flick of my other mouse generates and having the AHK recognise that many instances, in such a short space of time, as the trigger to accelerate the scrolling effect.

The effect/action is a bit simpler - and "just" needs to be a few sends of the scroll keystroke.

A useful addition would be to check for a near-immediate follow-up of scrolling in the same direction, and triggering "scroll to top" or "scroll to bottom", or a near-immediate follow-up of scrolling in the other direction to cancel/counter-act the extra scrolling.

I found this V1 script that's very nice, but isn't triggered in the same way as the MX master, and doesn't offer quite the same effect.

User avatar
mikeyww
Posts: 27009
Joined: 09 Sep 2014, 18:38

Re: Is there a way to trigger a script by activating the mouse scrollwheel X times in Y ms?

Post by mikeyww » 02 Apr 2024, 09:55

Code: Select all

; This script triggers an action when activating the mouse wheel X times in Y ms
#Requires AutoHotkey v2.0
time      := []
threshold := 5    ; X times
period    := 1000 ;  in Y ms

WheelUp::
WheelDown:: {
 now := A_TickCount
 If !time.Length
  SetTimer purge, 15
 time.Push(now)         ; Add current tick to array
 If time.Length >= threshold
  stop(), action()
}

action() {              ; Action to be triggered
 MsgBox 'Done!`n`n', 'Status', 'Iconi'
}

purge() {               ; Remove old values from array
 now := A_TickCount
 For n, t in time
  If now - t > period
   time.RemoveAt(n)
 tip
 If !time.Length        ; When no values remain, stop the timer
  stop
}

tip() {                 ; For each array value, display time since the value
 now := A_TickCount, str := ''
 For n, t in time
  str .= now - t '`n'
 ToolTip Trim(str, '`n')
}

stop() {
 SetTimer purge, 0
 Global time := []
 ToolTip
 SoundBeep 1000
}
Last edited by mikeyww on 02 Apr 2024, 11:04, edited 1 time in total.

-Garfle-
Posts: 19
Joined: 07 Feb 2024, 07:46

Re: Is there a way to trigger a script by activating the mouse scrollwheel X times in Y ms?

Post by -Garfle- » 02 Apr 2024, 11:03

mikeyww wrote:
02 Apr 2024, 09:55

Code: Select all

#Requires AutoHotkey v2.0
time      := []
period    := 1000
threshold := 5

WheelUp::
WheelDown:: {
 now := A_TickCount
 If !time.Length
  SetTimer purge, 15
 time.Push(now)         ; Add current tick to array
 If time.Length >= threshold {
  stop
  MsgBox 'Done!`n`n', 'Status', 'Iconi'
 }
}

purge() {               ; Remove old values from array
 now := A_TickCount
 For n, t in time
  If now - t > period
   time.RemoveAt(n)
 tip
 If !time.Length        ; When no values remain, stop the timer
  stop
}

tip() {                 ; For each array value, display time since the value
 now := A_TickCount, str := ''
 For n, t in time
  str .= now - t '`n'
 ToolTip Trim(str, '`n')
}

stop() {
 SetTimer purge, 0
 Global time := []
 ToolTip
 SoundBeep 1000
}
Thanks Mikey - that's genius!

One thing I don't understand is (well, candidly, there's a lot I haven't figured out in that code but...) why this disables my scroll function.

User avatar
mikeyww
Posts: 27009
Joined: 09 Sep 2014, 18:38

Re: Is there a way to trigger a script by activating the mouse scrollwheel X times in Y ms?

Post by mikeyww » 02 Apr 2024, 11:06

Your wheel is disabled because hotkeys always disable their native functions-- unless you precede the hotkey with tilde, which you can also do here.

Post Reply

Return to “Ask for Help (v2)”