Last active
June 4, 2024 00:06
-
-
Save keewon/d0948389eda69bcf1a9f35968d1fe4d7 to your computer and use it in GitHub Desktop.
This file contains 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
-- Source:https://github.com/owenlo/Marble-Hammerspoon | |
-- Source:https://github.com/tekezo/Karabiner/issues/814#issuecomment-415388742 | |
-- Source:https://gist.github.com/ParatechX/c03c1146f7cd719dbfd65a337c7228f3 | |
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED | |
local backMouseButton = 3 | |
local forwardMouseButton = 4 | |
local deferred = false | |
local VSCODE = 'Code' | |
local VIM = 'MacVim' | |
function setOverrides(e) | |
overrideOtherMouseDown:stop() | |
overrideOtherMouseUp:stop() | |
hs.eventtap.otherClick(e:location(), 0, pressedMouseButton) | |
overrideOtherMouseDown:start() | |
overrideOtherMouseUp:start() | |
end | |
overrideOtherMouseDown = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(e) | |
-- print("down") | |
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) | |
if backMouseButton == pressedMouseButton or forwardMouseButton == pressedMouseButton then | |
deferred = true | |
return true | |
end | |
end) | |
overrideOtherMouseUp = hs.eventtap.new({ hs.eventtap.event.types.otherMouseUp }, function(e) | |
-- print("up") | |
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) | |
local appName = hs.application.frontmostApplication():name() | |
if backMouseButton == pressedMouseButton then | |
if (deferred) then | |
setOverrides(e) | |
if appName == VSCODE then | |
hs.eventtap.keyStroke({"ctrl"}, "-") | |
elseif appName == VIM then | |
else | |
hs.eventtap.keyStroke({"cmd"}, "[") | |
end | |
return true | |
end | |
return false | |
end | |
if forwardMouseButton == pressedMouseButton then | |
if (deferred) then | |
setOverrides(e) | |
if appName == VSCODE then | |
hs.eventtap.keyStroke({"ctrl", "shift"}, "-") | |
elseif appName == VIM then | |
else | |
hs.eventtap.keyStroke({"cmd"}, "]") | |
end | |
return true | |
end | |
return false | |
end | |
return false | |
end) | |
local scrollModifier = "ctrl" | |
local oldMousePosition = {} | |
local scrollIntensityOther = -4 -- negative multiplier makes mouse work like traditional scrollwheel | |
local scrollIntensityCtrl = -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']) | |
-- print ("pressed mouse " .. pressedMouseButton) | |
if backMouseButton == pressedMouseButton or forwardMouseButton == pressedMouseButton then | |
-- print("scroll"); | |
deferred = false | |
oldMousePosition = 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 * scrollIntensityOther, dy * scrollIntensityOther}, {}, 'pixel') | |
-- put the mouse back | |
hs.mouse.absolutePosition(oldMousePosition) | |
return true, {scroll} | |
else | |
return false, {} | |
end | |
end) | |
-- Ctrl Mouse Tracker | |
ctrlDownMouseTracker = hs.eventtap.new({ hs.eventtap.event.types.mouseMoved }, function(e) | |
oldMousePosition = hs.mouse.getAbsolutePosition() | |
local dx = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaX']) | |
local dy = e:getProperty(hs.eventtap.event.properties['mouseEventDeltaY']) | |
-- print("ctrlDownMouseTracker runs dx=" .. dx .. " dy=" .. dy) | |
hs.eventtap.scrollWheel({dx * scrollIntensityCtrl , dy * scrollIntensityCtrl }, {}, 'pixel') | |
-- put the mouse back | |
hs.mouse.setAbsolutePosition(oldMousePosition) | |
end) | |
dragCtrlToScroll = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e) | |
local modifiers = e:getFlags() | |
if ( modifiers[ scrollModifier ] ) | |
then | |
ctrlDownMouseTracker:start() | |
else | |
ctrlDownMouseTracker:stop() | |
end | |
end) | |
overrideOtherMouseDown:start() | |
overrideOtherMouseUp:start() | |
dragOtherToScroll:start() | |
dragCtrlToScroll:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment