vimでインサートモードを抜ける時のあれ(Hammerspoon版その2)
hs.eventtap
を使用したものに書き換え。
Sierra にアップグレードして困らないための準備
前準備
Hammerspoon のインストール
$ brew cask install hammerspoon
Lutzifer/keyboardSwitcher のインストール
本家の brew install
はコケるので、とりあえず以下でインストール。
$ brew install oppara/homebrew/keyboardswitcher
入力ソースの設定
init.lua
init.lua
local function keyCode(key, modifiers)
modifiers = modifiers or {}
return function()
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), true):post()
hs.timer.usleep(1000)
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), false):post()
end
end
local function switchToUs()
local cmd = '/usr/local/bin/keyboardSwitcher select U.S.'
os.execute(cmd)
hs.console.printStyledtext(cmd)
end
local ctrlBracket = hs.hotkey.bind({'ctrl'}, '[', keyCode('escape'), switchToUs)
local function handleGlobalEvent(name, event, app)
if event == hs.application.watcher.activated then
local bundleId = string.lower(app.frontmostApplication():bundleID())
if bundleId == 'com.apple.terminal' then
ctrlBracket:enable()
else
ctrlBracket:disable()
end
end
end
watcher = hs.application.watcher.new(handleGlobalEvent)
watcher:start()
備考
read は出来る
$ defaults read com.apple.HIToolbox AppleSelectedInputSources
$ defaults read com.apple.HIToolbox AppleSelectedInputSources | xargs | cut -d';' -f3 | cut -d'=' -f2
write が
$ defaults write com.apple.HIToolbox AppleSelectedInputSources -array '{ InputSourceKind = "Keyboard Layout"; "KeyboardLayout ID" = 0; "KeyboardLayout Name" = "U.S."; }'
多分、こっちが王道。escapeのループ問題も解決できそう。。
IntelliJとかJVMのIDE使いでもKarabinerなしでバックスラッシュを片手入力したい