Skip to content

Instantly share code, notes, and snippets.

@tavlean
Last active May 16, 2025 04:35
Show Gist options
  • Save tavlean/7c4ac4946b4b135c278329ef7e7f42f9 to your computer and use it in GitHub Desktop.
Save tavlean/7c4ac4946b4b135c278329ef7e7f42f9 to your computer and use it in GitHub Desktop.
Updated Hammerspoon script to scroll with right click + trackball. Tested with Logi M575
-- 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 -- positive multiplier for natural scrolling
local smoothFactor = 0.5 -- Adjust this value (0 to 1) to control the smoothing effect
dragOtherToScroll =
hs.eventtap.new(
{hs.eventtap.event.types.rightMouseDragged},
function(e)
deferred = false
oldmousepos = hs.mouse.absolutePosition()
local dx = e:getProperty(hs.eventtap.event.properties["mouseEventDeltaX"]) * smoothFactor
local dy = e:getProperty(hs.eventtap.event.properties["mouseEventDeltaY"]) * smoothFactor
-- Get modifier keys
local flags = e:getFlags()
-- Create a scroll event with both vertical and horizontal components
local scroll = hs.eventtap.event.newScrollEvent(
{dx * scrollmult, dy * scrollmult},
{},
"pixel"
)
-- Set the flags on the scroll event to match the current modifier keys
scroll:setFlags(flags)
-- Restore the mouse position
hs.mouse.absolutePosition(oldmousepos)
return true, {scroll}
end
)
overrideOtherMouseDown:start()
overrideOtherMouseUp:start()
dragOtherToScroll:start()
@tavlean
Copy link
Author

tavlean commented Sep 20, 2024

This update addresses the deprecation warning by making two changes:

  1. Changed oldmousepos = hs.mouse.getAbsolutePosition() to oldmousepos = hs.mouse.absolutePosition()
  2. Changed hs.mouse.setAbsolutePosition(oldmousepos) to hs.mouse.absolutePosition(oldmousepos)

These changes use the newer hs.mouse.absolutePosition function, which can both get and set the mouse position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment