Skip to content

Instantly share code, notes, and snippets.

@zphixon
Created January 2, 2018 01:05
Show Gist options
  • Save zphixon/789d9727caa8e30f281cb867f9d2a347 to your computer and use it in GitHub Desktop.
Save zphixon/789d9727caa8e30f281cb867f9d2a347 to your computer and use it in GitHub Desktop.
Trackball scroll wheel emulator
;;
;; esmb.ahk
;; Author: Erik Elmore <[email protected]>
;; Version: 1.1 (Aug 16, 2005)
;;
;; Emulate scrolling/middle button
;;
;; Enables you to use any key with cursor movement
;; to emulate a scrolling middle button. While
;; the trigger key is held down, you may move the
;; mouse cursor up and down to send scroll wheel
;; events. If the cursor does not move by the
;; time the trigger key is released, then a middle
;; button click is generated. Additionally, the page
;; key on its own is mouse button 4, or mouse button
;; 5 when pressed at the same time as the trigger
;; button. I wrote this for my 4-button Logitech
;; Marble Mouse (trackball), which has no middle
;; button or scroll wheel.
;;
;; Updates
;;
;; 2013-02-28 "yetanotherjosh"
;; * lock cursor location when scrolling
;;
;; 2018-01-01 Zack Hixon <[email protected]>
;; * fixed style issues
;; * enabled use of forward/back buttons
;;
;;;;;;;;;;;;;;;;;;;
;; Configuration ;;
;;;;;;;;;;;;;;;;;;;
#NoTrayIcon ; hide tray icon
threshold = 8 ; higher number is lower sensitivity
triggerKey = XButton1 ; button to use for scrolling
pageKey = XButton2 ; button to use for page movement
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; End of configuration ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;
#Persistent
CoordMode, Mouse, Screen
Hotkey, %triggerKey%, triggerKeyDown
Hotkey, %triggerKey% up, triggerKeyUp
Hotkey, %pageKey%, page
keyDown = false
SetTimer, scrollTimer, 10
return
page:
if keyDown = true
; if trigger key is down, go forward
MouseClick, X2
else {
; otherwise, go backward
MouseClick, X1
}
return
triggerKeyDown:
; reset variables
moved = false
firstIteration = true
keyDown = true
delta = 0
; save mouse position before scroll
MouseGetPos, origX, origY
return
triggerKeyUp:
keyDown = false
m := moved = "false" ; didn't move
p := A_PriorHotkey = triggerKey ; last hk was scroll key
c := m && p ; ahk is dumb
if c
MouseClick, middle
return
scrollTimer:
if keyDown = false
return
; get distance scrolled
MouseGetPos, , newY
distance := newY - origY
if distance
moved = true
; calculate direction
delta := (delta + distance)
ticks := (delta // threshold)
delta := (delta - (ticks * threshold))
wheelDir := "WheelDown"
if (ticks < 0) {
wheelDir := "WheelUp"
ticks := (-1 * ticks)
}
; don't scroll immediately
if firstIteration = true
firstIteration = false
else {
Loop % ticks {
MouseClick, %wheelDir%
}
}
; reset mouse positon
MouseMove, origX, origY, 0
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment