Last active
May 16, 2025 04:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This update addresses the deprecation warning by making two changes:
oldmousepos = hs.mouse.getAbsolutePosition()
tooldmousepos = hs.mouse.absolutePosition()
hs.mouse.setAbsolutePosition(oldmousepos)
tohs.mouse.absolutePosition(oldmousepos)
These changes use the newer
hs.mouse.absolutePosition
function, which can both get and set the mouse position.