Google Chrome-like middle mouse button scrolling

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Codenirus
Posts: 1
Joined: 30 Mar 2023, 19:28

Google Chrome-like middle mouse button scrolling

Post by Codenirus » 30 Mar 2023, 20:11

I've recently been using middle mouse button scrolling a lot in Google Chrome and in Jetbrains IDEs (via a plugin), and I'm really finding it helpful instead of viscously scrolling my mouse wheel. I don't actually use the middle click, but I assign my mouse 4 button (side button) to the middle click action.

I've been using a program called ScrollNavigator which introduces this type of scrolling to almost any windows app, and it works pretty well. The only difference is that it behaves more like a drag scroll.


I started looking into these forums to find if there is an AutoHotKey script that I can use to emulate this type of Chrome-like middle mouse button scrolling. I came across this script here which I modified to support horizontal scrolling:

Code: Select all

; Description: Scroll Explorer on middle mouse button drag
; Permalink: https://autohotkey.com/boards/viewtopic.php?t=43715
; Author: aph
; Version: 0.2
$*MButton::
    MouseGetPos, CursorX, CursorY, Window, ClassNN
    WinGetTitle, Title, ahk_id %Window%
    WinGetClass, ahk_class, ahk_id %Window%
    WinGet ahk_exe, ProcessName, ahk_id %Window%
    WinGet ahk_PID, PID, ahk_id %Window%
    WinGetText, VisibleText, ahk_id %Window%
    MouseGetPos, CursorX_ended, CursorY_ended, Window_ended, ClassNN_ended
    WinGetClass, ahk_class_ended, ahk_id %Window_ended%
    WinGet ahk_exe_ended, ProcessName, ahk_id %Window_ended%
    AllowedApp := ahk_exe = "explorer.exe" or ahk_exe = "mmc.exe" or ahk_exe = "systempropertiesadvanced.exe" or ahk_exe = "filezilla.exe" or ahk_exe = "7zFM.exe"
    AllowedText := InStr(VisibleText, "Tree View") or InStr(VisibleText, "FolderView")
    LimitedApp := ahk_exe = "cmd.exe"
    DisabledApp := ahk_class_ended = "Shell_TrayWnd" or ahk_class_ended = "WorkerW"
    if (AllowedText >= 1)
        AllowedText = 1
    If (DisabledApp) {
        SendInput, {MButton Down}
        Return
    }
    Else If (!AllowedApp and !LimitedApp and !AllowedText) {
        SendInput, {MButton Down}
        Return
    }
    Else {
        If (AllowedApp) {
            SendInput, {MButton}
        }
        MiddleScroll := 1
        SetSystemCursor("SIZEALL")
        Sensitivity = 10 ; How far the middle mouse wheel has to be dragged before scrolling is triggered
        MouseGetPos, X1, Y1, , c, 2
        OrigTimer := 40 ; How quickly the file list scrolls
        SetTimer, MBScroll, %OrigTimer%
        MBScroll:
            MouseGetPos, X2, Y2
            Distance := Abs(Y2-Y1)
			DistanceX := Abs(X2-X1)
			
			; Check if we should start to scroll horizontally
			If (DistanceX >= Sensitivity) {
				RoundedX := % Round((DistanceX / 200)**1.25+1)
			    DllCall("SystemParametersInfo", UInt, 0x69, UInt, Round(Ln(RoundedX)+1), UInt, 0, UInt, 0) ; Vary lines scrolled by distance of drag 
                Timer := Round(OrigTimer - (OrigTimer/2*Percent/100))
                SetTimer, MBScroll, %Timer%
				Percent := (A_ScreenWidth - (Max(Y1, Abs(X1-A_ScreenWidth)) - DistanceX)) / A_ScreenWidth * 100
				SendInput, % "{Blind}{Wheel" (X2 > X1 ? "Right" : "Left") " " RoundedX "}"
            }
			
			; Check if we should start to scroll vertically
            If (Distance >= Sensitivity) {
                Rounded := % Round((Distance / 200)**1.25+1)
                DllCall("SystemParametersInfo", UInt, 0x69, UInt, Round(Ln(Rounded)+1), UInt, 0, UInt, 0) ; Vary lines scrolled by distance of drag 
                Timer := Round(OrigTimer - (OrigTimer/2*Percent/100))
                SetTimer, MBScroll, %Timer%
                Percent := (A_ScreenHeight - (Max(Y1, Abs(Y1-A_ScreenHeight)) - Distance)) / A_ScreenHeight * 100
                SendInput, % "{Blind}{Wheel" (Y2 > Y1 ? "Down" : "Up") " " Rounded "}"
            }
        Return
        $*MButton Up::
            DllCall("SystemParametersInfo", UInt, 0x69, UInt, 3, UInt, 0, UInt, 0) ; Set back to 3 lines scrolled
            SetTimer, MBScroll, off
            SetSystemCursor()
            MiddleScroll := 0
            SendInput {MButton Up}
            SetSystemCursor(Cursor="") {
                SystemCursors := "32512IDC_ARROW|32513IDC_IBEAM|32514IDC_WAIT|32515IDC_CROSS|32516IDC_UPARROW|32642IDC_SIZENWSE|32643IDC_SIZENESW|32644IDC_SIZEWE|32645IDC_SIZENS|32646IDC_SIZEALL|32648IDC_NO|32649IDC_HAND|32650IDC_APPSTARTING|32651IDC_HELP"
                If (Cursor = "")
                    Return DllCall("SystemParametersInfo", "UInt", 0x57, "UInt", 0, "UInt", 0, "UInt", 0) 
                If (StrLen(SystemCursors) = 221)
                    Loop, Parse, SystemCursors, |
                        StringReplace, SystemCursors, SystemCursors, %A_LoopField%, % DllCall("LoadCursor", "UInt", 0, "Int", SubStr(A_LoopField, 1, 5)) A_LoopField
                If !(Cursor := SubStr(SystemCursors, InStr(SystemCursors "|", "IDC_" Cursor "|") - 5 - p := (StrLen(SystemCursors) - 221) / 14, 5))
                    MsgBox, 262160, %A_ScriptName% - %A_ThisFunc%(): Error, Invalid cursor name!
                Else
                    Loop, Parse, SystemCursors, |
                        DllCall("SetSystemCursor", "UInt", DllCall("CopyIcon", "UInt", Cursor), "Int", SubStr(A_LoopField, 6, p))
                }
        Return
    }
Return
The post that I pulled this from can be found here: https://autohotkey.com/boards/viewtopic.php?t=43715

This script behaves similarly to Chrome-like middle mouse scrolling, but it's way too sensitive in certain applications, for example in OneNote. I tried changing the "OrigTimer" as suggested in the original post to a higher value to reduce the sensitivity but it just makes the scrolling very choppy. I can tell the script is performing a mouse wheel scroll. I feel like if this script was able to affect the scroll bar position directly, it could be smoother, but I don't know AHK scripting.



Anyways, all this to say that I'm wondering if anyone knows of a script that can do what I'm looking for (chrome-like middle mouse button scrolling), or if someone could suggest a modification to the script I have to make it less sensitive and yet feel smooth. I know there are drag-n-scroll scripts out there but that's the not the scrolling behavior I'm looking for.

I know very little of AHK scripting, I can read it and make small tweaks but I can't write it. In the end, if I can't find a script, I'll just be sticking with the ScrollNavigator app that I found.

Return to “Ask for Help (v1)”