Created
June 30, 2017 12:58
-
-
Save weitzj/18fae42e2fbb6ac26679b0c2a4dd891a to your computer and use it in GitHub Desktop.
Hammerspoon Config
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
--required to be non zero for dragging windows to work some weird timing issue with hammerspoon fighting against osx events | |
hs.window.animationDuration = 0.00000001 | |
-- require "vim" | |
moveOnScreenCmd = {"cmd", "shift"} | |
hs.hotkey.bind(moveOnScreenCmd, "H", function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y | |
f.w = max.w / 2 | |
f.h = max.h | |
win:setFrame(f) | |
end) | |
hs.hotkey.bind(moveOnScreenCmd, "L", function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x + max.w / 2 | |
f.y = max.y | |
f.w = max.w / 2 | |
f.h = max.h | |
win:setFrame(f) | |
end) | |
hs.hotkey.bind(moveOnScreenCmd, "K", function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y | |
f.w = max.w | |
f.h = max.h / 2 | |
win:setFrame(f) | |
end) | |
hs.hotkey.bind(moveOnScreenCmd, "J", function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y + max.h / 2 | |
f.w = max.w | |
f.h = max.h / 2 | |
win:setFrame(f) | |
end) | |
otherHyper = {"cmd", "alt", "ctrl"} | |
hs.hotkey.bind(otherHyper, 'L', hs.grid.pushWindowPrevScreen) | |
hs.hotkey.bind(otherHyper, 'H', hs.grid.pushWindowNextScreen) | |
hs.hotkey.bind(otherHyper, 'space', function() | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y | |
f.w = max.w | |
f.h = max.h | |
win:setFrame(f) | |
end) | |
local mouseCircle = nil | |
local mouseCircleTimer = nil | |
function mouseHighlight() | |
-- Delete an existing highlight if it exists | |
if mouseCircle then | |
mouseCircle:delete() | |
if mouseCircleTimer then | |
mouseCircleTimer:stop() | |
end | |
end | |
-- Get the current co-ordinates of the mouse pointer | |
mousepoint = hs.mouse.getAbsolutePosition() | |
-- Prepare a big red circle around the mouse pointer | |
mouseCircle = hs.drawing.circle(hs.geometry.rect(mousepoint.x - 150, mousepoint.y - 150, 300, 300)) | |
mouseCircle:setStrokeColor({["red"]=1,["blue"]=0,["green"]=0,["alpha"]=0.8}) | |
mouseCircle:setFill(true) | |
mouseCircle:setFillColor({["red"]=1,["blue"]=1,["green"]=0,["alpha"]=0.8}) | |
mouseCircle:setStrokeWidth(40) | |
mouseCircle:bringToFront(true) | |
mouseCircle:show() | |
-- Set a timer to delete the circle after 3 seconds | |
mouseCircleTimer = hs.timer.doAfter(2, function() | |
mouseCircle:hide(0.2) | |
hs.timer.doAfter(0.6, function() mouseCircle:delete() end) | |
end) | |
end | |
hs.hotkey.bind(moveOnScreenCmd, "/", mouseHighlight) | |
-- Reload config {{{1 | |
function reloadConfig(files)---- {{{2 | |
doReload = false | |
for _,file in pairs(files) do | |
if file:sub(-4) == ".lua" then | |
hs.reload() | |
break | |
end | |
end | |
end-- }}}2 | |
hs.hotkey.bind(otherHyper, "R", function() | |
hs.reload() | |
hs.alert.show("Config reloaded") | |
hs.notify.new({title="Hammerspoon", informativeText="Config reloaded"}):send() | |
end) | |
-- HANDLE SCROLLING | |
local oldmousepos = {} | |
-- positive multiplier (== natural scrolling) makes mouse work like traditional scrollwheel | |
local scrollmult = 4 | |
mousetap = hs.eventtap.new({0,3,5,14,25,26,27}, function(e) | |
oldmousepos = hs.mouse.getAbsolutePosition() | |
local mods = hs.eventtap.checkKeyboardModifiers() | |
local pressedMouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) | |
-- If OSX button 4 is pressed, allow scrolling | |
local shouldScroll = 3 == pressedMouseButton | |
if shouldScroll then | |
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') | |
scroll:post() | |
-- put the mouse back | |
hs.mouse.setAbsolutePosition(oldmousepos) | |
return true, {scroll} | |
else | |
return false, {} | |
end | |
-- print ("Mouse moved!") | |
-- print (dx) | |
-- print (dy) | |
end) | |
mousetap:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment