Skip to content

Instantly share code, notes, and snippets.

@dmcbane
Created July 8, 2022 21:54
Show Gist options
  • Save dmcbane/3f2eccf6a84079ec38c908ab77ac169b to your computer and use it in GitHub Desktop.
Save dmcbane/3f2eccf6a84079ec38c908ab77ac169b to your computer and use it in GitHub Desktop.
Hammerspoon configuration with middle button scrolling and mouse scrollwheel reversal
-- SETUP THE LOGGER
local log = hs.logger.new('macOS','debug')
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED
local scrollMouseButton = 2
local deferred = false
overrideOtherMouseDown = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(e)
-- log.d("down")
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
if scrollMouseButton == pressedMouseButton
then
deferred = true
return true
end
end)
overrideOtherMouseUp = hs.eventtap.new({ hs.eventtap.event.types.otherMouseUp }, function(e)
-- log.d("up")
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
if scrollMouseButton == pressedMouseButton
then
if (deferred) then
overrideOtherMouseDown:stop()
overrideOtherMouseUp:stop()
hs.eventtap.otherClick(e:location(), pressedMouseButton)
overrideOtherMouseDown:start()
overrideOtherMouseUp:start()
return true
end
return false
end
return false
end)
local oldmousepos = {}
local scrollmult = -4 -- negative multiplier makes mouse work like traditional scrollwheel
dragOtherToScroll = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDragged }, function(e)
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
-- log.d ("pressed mouse " .. pressedMouseButton)
if scrollMouseButton == pressedMouseButton
then
-- log.d("scroll");
deferred = false
oldmousepos = hs.mouse.absolutePosition
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.absolutePosition = oldmousepos
return true, {scroll}
else
return false, {}
end
end)
overrideOtherMouseDown:start()
overrideOtherMouseUp:start()
dragOtherToScroll:start()
-- REVERSE SCROLLWHEEL WITHOUT AFFECTING TRACKPAD
reverseScrollWheel = hs.eventtap.new({ hs.eventtap.event.types.scrollWheel }, function(e)
-- (I) Scrolling data. This field typically contains the change in vertical position since the
-- last scrolling event from a Mighty Mouse scroller or a single-wheel mouse scroller.
local da1 = e:getProperty(hs.eventtap.event.properties['scrollWheelEventDeltaAxis1'])
log.d ("vert pos delta " .. da1)
e:setProperty(hs.eventtap.event.properties['scrollWheelEventDeltaAxis1'], 0 - da1)
-- (N) Contains scrolling data which represents a line-based or pixel-based change in vertical
-- position since the last scrolling event from a Mighty Mouse scroller or a single-wheel mouse scroller.
local fpda1 = e:getProperty(hs.eventtap.event.properties['scrollWheelEventFixedPtDeltaAxis1'])
log.d ("line or pixel based vert pos delta " .. fpda1)
e:setProperty(hs.eventtap.event.properties['scrollWheelEventFixedPtDeltaAxis1'], 0 - fpda1)
-- (I) Pixel-based scrolling data. The scrolling data represents the change in vertical
-- position since the last scrolling event from a Mighty Mouse scroller or a single-wheel mouse scroller.
local pda1 = e:getProperty(hs.eventtap.event.properties['scrollWheelEventPointDeltaAxis1'])
log.d ("pixel based vert pos delta " .. pda1)
e:setProperty(hs.eventtap.event.properties['scrollWheelEventPointDeltaAxis1'], 0 - pda1)
return false
end)
reverseScrollWheel:start()
-- LOAD SPOON TO ALLOW MANUAL WINDOW POSITIONING
-- the values for the hotkeys below are the same as spoon.WindowHalfsAndThirds.defaultHotkeys
hs.loadSpoon("WindowHalfsAndThirds")
spoon.WindowHalfsAndThirds:bindHotkeys({
left_half = { {"ctrl", "cmd"}, "Left" },
right_half = { {"ctrl", "cmd"}, "Right" },
top_half = { {"ctrl", "cmd"}, "Up" },
bottom_half = { {"ctrl", "cmd"}, "Down" },
third_left = { {"ctrl", "alt" }, "Left" },
third_right = { {"ctrl", "alt" }, "Right" },
third_up = { {"ctrl", "alt" }, "Up" },
third_down = { {"ctrl", "alt" }, "Down" },
top_left = { {"ctrl", "cmd"}, "1" },
top_right = { {"ctrl", "cmd"}, "2" },
bottom_left = { {"ctrl", "cmd"}, "3" },
bottom_right= { {"ctrl", "cmd"}, "4" },
max_toggle = { {"ctrl", "alt", "cmd"}, "f" },
max = { {"ctrl", "alt", "cmd"}, "Up" },
undo = { { "alt", "cmd"}, "z" },
center = { { "alt", "cmd"}, "c" },
larger = { { "alt", "cmd", "shift"}, "Right" },
smaller = { { "alt", "cmd", "shift"}, "Left" },
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment