Skip to content

Instantly share code, notes, and snippets.

@bokuo-okubo
Last active October 23, 2017 13:26
Show Gist options
  • Save bokuo-okubo/12087429099e20c8a8b3b58809aa9be1 to your computer and use it in GitHub Desktop.
Save bokuo-okubo/12087429099e20c8a8b3b58809aa9be1 to your computer and use it in GitHub Desktop.
HammerSpoonConfig

my hammerspoon confing

local quitModal = hs.hotkey.modal.new('cmd','q')
function quitModal:entered()
hs.alert.show("Press Cmd+Q again to quit", 1)
hs.timer.doAfter(1, function() quitModal:exit() end)
end
local function doQuit()
local res = hs.application.frontmostApplication():selectMenuItem("Quit")
quitModal:exit()
end
quitModal:bind('cmd', 'q', doQuit)
quitModal:bind('', 'escape', function() quitModal:exit() end)
-- 左コマンド右コマンドを英数にリマップする
local prevKeyCode
local leftCommand = 0x37
local rightCommand = 0x36
local eisuu = 0x66
local kana = 0x68
local function keyStroke(modifiers, character)
hs.eventtap.keyStroke(modifiers, character)
end
local function jp()
keyStroke({}, kana)
end
local function eng()
keyStroke({}, eisuu)
end
local function handleEvent(e)
local keyCode = e:getKeyCode()
local isExcludeKeys = false
for _, v in pairs({'shift', 'alt', 'cmd', 'ctrl', 'space'}) do isExcludeKeys = e:getFlags()[v] end
local isCmdKeyUp = not(isExcludeKeys) and e:getType() == hs.eventtap.event.types.flagsChanged
if isCmdKeyUp and prevKeyCode == leftCommand then
-- print("detect left command")
eng()
elseif isCmdKeyUp and prevKeyCode == rightCommand then
-- print("detect right command")
jp()
end
prevKeyCode = keyCode
end
eventtap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.keyDown, hs.eventtap.event.types.keyUp}, handleEvent)
eventtap:start()
--- emacs like
local function keyCode(key, modifiers)
modifiers = modifiers or {}
return function()
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), true):post()
hs.timer.usleep(10)
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), false):post()
end
end
local function remapKey(modifiers, key, keyCode)
hs.hotkey.bind(modifiers, key, keyCode, nil, keyCode)
end
local function disableAllHotkeys()
for k, v in pairs(hs.hotkey.getHotkeys()) do
v['_hk']:disable()
end
end
local function enableAllHotkeys()
for k, v in pairs(hs.hotkey.getHotkeys()) do
v['_hk']:enable()
end
end
local function handleGlobalAppEvent(name, event, app)
if event == hs.application.watcher.activated then
-- hs.alert.show(name)
if name ~= "iTerm2" then
enableAllHotkeys()
else
disableAllHotkeys()
end
end
end
appsWatcher = hs.application.watcher.new(handleGlobalAppEvent)
appsWatcher:start()
-- カーソル移動
remapKey({'ctrl'}, 'f', keyCode('right'))
remapKey({'ctrl'}, 'b', keyCode('left'))
remapKey({'ctrl'}, 'n', keyCode('down'))
remapKey({'ctrl'}, 'p', keyCode('up'))
-- テキスト編集
remapKey({'ctrl'}, 'w', keyCode('x', {'cmd'}))
remapKey({'ctrl'}, 'y', keyCode('v', {'cmd'}))
-- コマンド
remapKey({'ctrl'}, 's', keyCode('f', {'cmd'}))
remapKey({'ctrl'}, '/', keyCode('z', {'cmd'}))
remapKey({'ctrl'}, 'g', keyCode('escape'))
-- ページスクロール
remapKey({'ctrl'}, 'v', keyCode('pagedown'))
remapKey({'alt'}, 'v', keyCode('pageup'))
remapKey({'cmd', 'shift'}, ',', keyCode('home'))
remapKey({'cmd', 'shift'}, '.', keyCode('end'))
require("double_cmdq_to_quit")
require("emacs")
require("eisu")
require("scroll_with_trackball")
-- HANDLE SCROLLING
local deferred = false
overrideRightMouseDown = hs.eventtap.new({ hs.eventtap.event.types.rightMouseDown }, function(e)
--print("down"))
deferred = true
return true
end)
overrideRightMouseUp = hs.eventtap.new({ hs.eventtap.event.types.rightMouseUp }, function(e)
-- print("up"))
if (deferred) then
overrideRightMouseDown:stop()
overrideRightMouseUp:stop()
hs.eventtap.rightClick(e:location())
overrideRightMouseDown:start()
overrideRightMouseUp:start()
return true
end
return false
end)
local oldmousepos = {}
local scrollmult = -4 -- negative multiplier makes mouse work like traditional scrollwheel
local handler = function(e)
-- print("scroll");
deferred = false
oldmousepos = hs.mouse.getAbsolutePosition()
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.setAbsolutePosition(oldmousepos)
return true, {scroll}
end
dragRightToScroll = hs.eventtap.new({ hs.eventtap.event.types.rightMouseDragged }, handler)
overrideRightMouseDown:start()
overrideRightMouseUp:start()
dragRightToScroll:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment