Last active
May 18, 2018 23:44
-
-
Save poppen/e6873b3ed0cb0f3208d591f40fdee133 to your computer and use it in GitHub Desktop.
KarabinerでやっていたことをHammerspoonで代替する(USキーボード用)
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iterm2で
C-J
が改行になってしまう問題はどうしようもないので、とりあえず、AquaSKKの日本語ONをC-0
にすることで回避