Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created August 28, 2024 17:08
Show Gist options
  • Save christianselig/1d1e9ec24a26d7aeae6b6efe3fa9f533 to your computer and use it in GitHub Desktop.
Save christianselig/1d1e9ec24a26d7aeae6b6efe3fa9f533 to your computer and use it in GitHub Desktop.
Add middle-button panning support to Sketch to match Figma, Fusion 360, etc.
-- Make Sketch use the middle mouse button for dragging like Figma, Fusion 360, etc.
local middleMouseDown = false
-- Function to handle the middle mouse down event
local function middleMouseDownHandler(event)
if event:getType() == hs.eventtap.event.types.otherMouseDown then
if event:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) == 2 then
middleMouseDown = true
-- Simulate holding down the space bar
hs.eventtap.event.newKeyEvent(hs.keycodes.map['space'], true):post()
-- Simulate a left mouse button down at the current mouse location
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseDown, event:location()):post()
return true -- prevent the event from passing through
end
end
return false
end
-- Function to handle the middle mouse up event
local function middleMouseUpHandler(event)
if event:getType() == hs.eventtap.event.types.otherMouseUp then
if event:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) == 2 then
middleMouseDown = false
-- Simulate releasing the space bar
hs.eventtap.event.newKeyEvent(hs.keycodes.map['space'], false):post()
-- Simulate a left mouse button up at the current mouse location
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseUp, event:location()):post()
return true -- prevent the event from passing through
end
end
return false
end
-- Function to handle mouse dragging while middle mouse button is held
local function mouseDragHandler(event)
if middleMouseDown then
-- Simulate dragging with the left mouse button at the updated location
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseDragged, event:location()):post()
return true -- prevent the event from passing through
end
return false
end
-- Create an eventtap watcher for the middle mouse down, up, and drag events
local middleMouseWatcher = hs.eventtap.new({hs.eventtap.event.types.otherMouseDown, hs.eventtap.event.types.otherMouseUp, hs.eventtap.event.types.otherMouseDragged}, function(event)
local sketchApp = hs.application.find("com.bohemiancoding.sketch3")
if sketchApp == nil then
return false
end
--- If not the frontmost app, do nothing
if sketchApp:isFrontmost() then
if middleMouseDownHandler(event) or middleMouseUpHandler(event) or mouseDragHandler(event) then
return true
else
return false
end
else
return false
end
end)
-- Start the watcher
middleMouseWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment