LoginSignup
4
3

More than 5 years have passed since last update.

vimでインサートモードを抜ける時のあれ(Hammerspoon版その2)

Last updated at Posted at 2017-02-02

IntelliJとかJVMのIDE使いでもKarabinerなしでバックスラッシュを片手入力したい
上記を参考に eventtap を使ってみる。

前準備

vimでインサートモードを抜ける時のあれ(Hammerspoon版)を参照

init.lua

init.lua
local VK_ESC = 53
local VK_LEFT_BRACKET = 33

local function switchToUs()
    local cmd = '/usr/local/bin/keyboardSwitcher select U.S.'
    os.execute(cmd)
    -- hs.console.printStyledtext(cmd)
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 keyCode = event:getKeyCode()
    local flags = event:getFlags()
    -- hs.console.printStyledtext(keyCode)

    if keyCode == VK_ESC then
        switchToUs()
    end

    if keyCode == VK_LEFT_BRACKET then
        if flagsMatches(flags, {'ctrl'}) then
            switchToUs()
        end
    end
end)

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
            keyEventtap:start()
        else
            keyEventtap:stop()
        end
    end
end
watcher = hs.application.watcher.new(handleGlobalEvent)
watcher:start()

結果

esc にも対応できて概ね満足:smile:

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3