Skip to content

Instantly share code, notes, and snippets.

@honboubao
Last active January 20, 2025 15:02
Show Gist options
  • Save honboubao/3a2f5cd23d145bbdc18451887db72d5e to your computer and use it in GitHub Desktop.
Save honboubao/3a2f5cd23d145bbdc18451887db72d5e to your computer and use it in GitHub Desktop.
AutoHotKey v2 script to activate mod keys and enable fast scrolling when mouse buttons are being held for more than 300ms.
#Persistent
SetTimer, CheckHoldCtrl, Off ; Timer for Ctrl (forward button)
SetTimer, CheckHoldShift, Off ; Timer for Shift (back button)
SetTimer, CheckHoldAlt, Off ; Timer for Alt (middle button)
HoldTime := 0.3 ; Hold time threshold in milliseconds
ScrollSpeed := 10 ; Multiplier for faster scrolling
; Back button (XButton1) -> Shift key
XButton1::
KeyWait, XButton1, T%HoldTime% ; Wait for the button to be released within the threshold
if (ErrorLevel) {
; If the button is held for more than the threshold
Send, {Shift Down} ; Press Shift
SetTimer, CheckHoldShift, On ; Start a timer to monitor for release
} else {
; Otherwise, it was a quick press
Send, {XButton1} ; Send the back button event
}
return
CheckHoldShift:
if !GetKeyState("XButton1", "P") {
; If the back button is released
if GetKeyState("Shift", "P") {
Send, {Shift Up} ; Release Shift
}
SetTimer, CheckHoldShift, Off ; Stop monitoring
}
return
; Forward button (XButton2) -> Ctrl key
XButton2::
KeyWait, XButton2, T%HoldTime% ; Wait for the button to be released within the threshold
if (ErrorLevel) {
; If the button is held for more than the threshold
Send, {Ctrl Down} ; Press Ctrl
SetTimer, CheckHoldCtrl, On ; Start a timer to monitor for release
} else {
; Otherwise, it was a quick press
Send, {XButton2} ; Send the forward button event
}
return
CheckHoldCtrl:
if !GetKeyState("XButton2", "P") {
; If the forward button is released
if GetKeyState("Ctrl", "P") {
Send, {Ctrl Up} ; Release Ctrl
}
SetTimer, CheckHoldCtrl, Off ; Stop monitoring
}
return
; Middle mouse button (MButton) -> Alt key
MButton::
KeyWait, MButton, T%HoldTime% ; Wait for the button to be released within the threshold
if (ErrorLevel) {
; If the button is held for more than the threshold
Send, {Alt Down} ; Press Alt
SetTimer, CheckHoldAlt, On ; Start a timer to monitor for release
} else {
; Otherwise, it was a quick press
Send, {MButton} ; Send the middle mouse button event
}
return
CheckHoldAlt:
if !GetKeyState("MButton", "P") {
; If the middle mouse button is released
if GetKeyState("Alt", "P") {
Send, {Alt Up} ; Release Alt
}
SetTimer, CheckHoldAlt, Off ; Stop monitoring
}
return
; Mouse wheel handling for faster scrolling when both buttons are held
*WheelUp::
if (GetKeyState("XButton1", "P") && GetKeyState("XButton2", "P")) {
if GetKeyState("Ctrl", "P") {
Send, {Ctrl Up} ; Release Ctrl
}
if GetKeyState("Shift", "P") {
Send, {Shift Up} ; Release Shift
}
Send, {WheelUp %ScrollSpeed%}
} else {
Send, {WheelUp}
}
return
*WheelDown::
if (GetKeyState("XButton1", "P") && GetKeyState("XButton2", "P")) {
if GetKeyState("Ctrl", "P") {
Send, {Ctrl Up} ; Release Ctrl
}
if GetKeyState("Shift", "P") {
Send, {Shift Up} ; Release Shift
}
Send, {WheelDown %ScrollSpeed%}
} else {
Send, {WheelDown}
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment