Skip to content

Instantly share code, notes, and snippets.

@poppen
Last active May 18, 2018 23:44
Show Gist options
  • Save poppen/e6873b3ed0cb0f3208d591f40fdee133 to your computer and use it in GitHub Desktop.
Save poppen/e6873b3ed0cb0f3208d591f40fdee133 to your computer and use it in GitHub Desktop.
KarabinerでやっていたことをHammerspoonで代替する(USキーボード用)
local VK_SEMICOLON = 0x29
local VK_ESC = 0x35
local VK_RIGHT_BRACKET = 0x21
local VK_J = 0x26
-- Auto reload config
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
local myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")
-- Key remapping
local function switchToJp()
hs.eventtap.keyStroke({'ctrl'}, '0')
end
local function switchToUs()
hs.eventtap.keyStroke({}, 'l')
end
function flagsMatches(flags, modifiers)
local set = {}
for _, i in ipairs(modifiers) do set[string.lower(i)] = true end
for _, j in ipairs({'fn', 'cmd', 'ctrl', 'alt', 'shift'}) do
if set[j] ~= flags[j] then return false end
end
return true
end
keyEventtap = hs.eventtap.new({
hs.eventtap.event.types.keyDown
}, function(event)
local bundleId = string.lower(hs.application.frontmostApplication():bundleID())
local keyCode = event:getKeyCode()
local flags = event:getFlags()
-- hs.console.printStyledtext(keyCode)
-- Swap : and ;
if keyCode == VK_SEMICOLON then
if flagsMatches(flags, {'shift'}) then
event:setKeyCode(VK_SEMICOLON)
event:setFlags({})
else
event:setKeyCode(VK_SEMICOLON)
event:setFlags({shift=true})
end
end
-- For vim with AquaSKK on iterm2
if bundleId == 'com.googlecode.iterm2' then
if keyCode == VK_J and flagsMatches(flags, {'ctrl'}) then
switchToJp()
event:setKeyCode('')
event:setFlags({})
end
if keyCode == VK_RIGHT_BRACKET and flagsMatches(flags, {'ctrl'}) then
switchToUs()
end
if keyCode == VK_ESC then
switchToUs()
end
end
end)
keyEventtap:start()
@poppen
Copy link
Author

poppen commented Apr 13, 2017

iterm2でC-Jが改行になってしまう問題はどうしようもないので、とりあえず、AquaSKKの日本語ONをC-0にすることで回避

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment