Skip to content

Instantly share code, notes, and snippets.

@cwagner22
Last active April 15, 2023 16:29
Show Gist options
  • Save cwagner22/13e772cda1c4ad80d23452e530f4b760 to your computer and use it in GitHub Desktop.
Save cwagner22/13e772cda1c4ad80d23452e530f4b760 to your computer and use it in GitHub Desktop.
Hammerspoon script to scroll with right click + trackball. Tested with MX Ergo.
-- https://github.com/tekezo/Karabiner/issues/814
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED
local scrollMouseButton = 2
local deferred = false
overrideOtherMouseDown =
hs.eventtap.new(
{hs.eventtap.event.types.rightMouseDown},
function(e)
deferred = true
return true
end
)
overrideOtherMouseUp =
hs.eventtap.new(
{hs.eventtap.event.types.rightMouseUp},
function(e)
if (deferred) then
overrideOtherMouseDown:stop()
overrideOtherMouseUp:stop()
hs.eventtap.rightClick(e:location(), pressedMouseButton)
overrideOtherMouseDown:start()
overrideOtherMouseUp:start()
return true
end
return false
end
)
local oldmousepos = {}
local scrollmult = -6 -- negative multiplier makes mouse work like traditional scrollwheel
dragOtherToScroll =
hs.eventtap.new(
{hs.eventtap.event.types.rightMouseDragged},
function(e)
deferred = false
oldmousepos = hs.mouse.getAbsolutePosition()
local dx = e:getProperty(hs.eventtap.event.properties["mouseEventDeltaX"])
local dy = e:getProperty(hs.eventtap.event.properties["mouseEventDeltaY"])
local scroll = hs.eventtap.event.newScrollEvent({dx * scrollmult, dy * scrollmult}, {}, "pixel")
-- put the mouse back
hs.mouse.setAbsolutePosition(oldmousepos)
return true, {scroll}
end
)
overrideOtherMouseDown:start()
overrideOtherMouseUp:start()
dragOtherToScroll:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment