Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Created September 24, 2025 12:37
Show Gist options
  • Save kou1okada/95e09cb98673f6ff1e63cc3281583dff to your computer and use it in GitHub Desktop.
Save kou1okada/95e09cb98673f6ff1e63cc3281583dff to your computer and use it in GitHub Desktop.
MiddleButtonDragScroll.ahk - Enables scrolling by dragging mouse middle button

MiddleButtonDragScroll.ahk - Enables scrolling by dragging mouse middle button

This script enables scrolling by dragging the middle mouse button.

Requirement

Usage

To launch the script, double-click it in Explorer.exe.

License

  • The MIT license
; MiddleButtonDragScroll.ahk
; Copyright (c) 2025 Koichi OKADA. All rights reserved.
; This script is distributed under the MIT license.
#Requires AutoHotkey v2.0
global x0 := 0, y0 := 0
global ScrollFactor := RegRead("HKEY_CURRENT_USER\Control Panel\Desktop", "WheelScrollLines") / 300
global mode := +1 ; +1 = grab, -1 = scroll
global vx := 0.0, vy := 0.0
global rx := 0.0, ry := 0.0
global moved_flag := false
global InertiaActive := false
global InertiaAlpha := 0.999
MButton::
{
global x0, y0, rx, ry, moved_flag, vx, vy
moved_flag := false
rx := 0.0
ry := 0.0
vx := 0.0
vy := 0.0
CoordMode("Mouse", "Screen")
MouseGetPos(&x0, &y0)
SetTimer(Scroll, 10)
}
MButton Up::
{
SetTimer(Scroll, 0)
global moved_flag, InertiaActive
if !moved_flag
{
Hotkey("MButton", "Off")
Send("{MButton Down}")
Sleep(10)
Send("{MButton Up}")
Hotkey("MButton", "On")
return
}
InertiaActive := true
SetTimer(InertiaScroll, 10)
}
Scroll()
{
global x0, y0, ScrollFactor, mode
global vx, vy
static x1 := 0, y1 := 0
CoordMode("Mouse", "Screen")
MouseGetPos(&x1, &y1)
dx := mode * (x1 - x0)
dy := mode * (y1 - y0)
vx := dx * ScrollFactor
vy := dy * ScrollFactor
ApplyScroll(vx, vy)
x0 := x1
y0 := y1
}
InertiaScroll()
{
global vx, vy, InertiaActive, InertiaAlpha
vx *= InertiaAlpha
vy *= InertiaAlpha
ApplyScroll(vx, vy)
if Abs(vx) < 0.1 && Abs(vy) < 0.1
{
InertiaActive := false
SetTimer(InertiaScroll, 0)
}
}
ApplyScroll(dx, dy)
{
global rx, ry, moved_flag
totalX := dx + rx
totalY := dy + ry
ix := Round(totalX)
iy := Round(totalY)
loop Abs(ix)
{
Send(ix > 0 ? "{WheelLeft}" : "{WheelRight}")
moved_flag := true
}
loop Abs(iy)
{
Send(iy > 0 ? "{WheelUp}" : "{WheelDown}")
moved_flag := true
}
rx := totalX - ix
ry := totalY - iy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment