前回の記事( karaninerにfnキーを割り当てたいが 無理だったのでHammerspoonにいきついた話 )でkarabinerをつかってなんとかmistelキーボードの代わりの動きができないかと四苦八苦しているなかで本記事の中心であるHammerSpoonに出会いました。
macへのinstallから、基本となるキーマッピングの方法までを解説します。
まず
インストールします
touch ~/.hammerspoon/init.lua
インストールしただけでは特に何も起こりません。
ファイルを作成します。
ここに挙動を書いていきます。
hotkeyのbindという関数を利用します
(参照:hammerspoonshiftspacecmdspace.html)
使えるkeyの文字列はkeycodes.mapに以下のようにあります
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, pad, pad*, pad+, pad/, pad-, pad=, pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, padclear, padenter, return, tab, space, delete, escape, help, home, pageup, forwarddelete, end, pagedown, left, right, down, up, shift, rightshift, cmd, rightcmd, alt, rightalt, ctrl, rightctrl, capslock, fn
書いてみる。
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
hs.hotkey.bind({'shift'}, 'space', 'triggered binding shift+space => %D', keyCode('d', {"cmd"}), nil, keyCode('d', {"cmd"}))
Reload Configする。
テストで、shift + space押してみる。
うわ!

なんか、動作してるっぽい
私の場合、言語切り替えでしたので、見事動作確認とれました!
![]()
これはいい!
と勢いづいた
細かい点はusleepはマイクロ秒であるということとhs.eventtap.event.newKeyEventを押さえておけばOKです
local function keyCode(key, modifiers)
modifiers = modifiers or {}
return function()
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), true):post()
hs.timer.usleep(100)
hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), false):post()
end
end
hs.hotkey.bind({'shift'}, 'space', 'triggered binding shift+space => %D', keyCode('d', {"cmd"}), nil, keyCode('d', {"cmd"}))
hs.hotkey.bind({'alt'}, 'i', nil, keyCode('up'), nil, keyCode('up'))
hs.hotkey.bind({'alt'}, 'k', nil, keyCode('down'), nil, keyCode('down'))
hs.hotkey.bind({'alt'}, 'j', nil, keyCode('left'), nil, keyCode('left'))
hs.hotkey.bind({'alt'}, 'l', nil, keyCode('right'), nil, keyCode('right'))
って書いたところで一応、動いたんですが
shiftとかAltとかと一緒に使えないので残念な結果となりました。あと遅いんです。この二点を改善していきます。どうにかならにあものかと。。。調べるとhttps://github.com/kkamdooong/hammerspoon-control-hjkl-to-arrow/blob/master/init.lua
で、なんかうまくいきそうです。
やってみると
これで解決できました!!
local function pressFn(mods, key)
if key == nil then
key = mods
mods = {}
end
return function() hs.eventtap.keyStroke(mods, key, 1000) end
end
local function remap(mods, key, pressFn)
hs.hotkey.bind(mods, key, pressFn, nil, pressFn)
end
remap({'ctrl'}, 'i', pressFn('up'))
remap({'ctrl'}, 'j', pressFn('left'))
remap({'ctrl'}, 'k', pressFn('down'))
remap({'ctrl'}, 'l', pressFn('right'))
remap({'ctrl', 'shift'}, 'i', pressFn({'shift'}, 'up'))
remap({'ctrl', 'shift'}, 'j', pressFn({'shift'}, 'left'))
remap({'ctrl', 'shift'}, 'k', pressFn({'shift'}, 'down'))
remap({'ctrl', 'shift'}, 'l', pressFn({'shift'}, 'right'))
remap({'ctrl', 'cmd'}, 'i', pressFn({'cmd'}, 'up'))
remap({'ctrl', 'cmd'}, 'j', pressFn({'cmd'}, 'left'))
remap({'ctrl', 'cmd'}, 'k', pressFn({'cmd'}, 'down'))
remap({'ctrl', 'cmd'}, 'l', pressFn({'cmd'}, 'right'))
remap({'ctrl', 'alt'}, 'i', pressFn({'alt'}, 'up'))
remap({'ctrl', 'alt'}, 'j', pressFn({'alt'}, 'left'))
remap({'ctrl', 'alt'}, 'k', pressFn({'alt'}, 'down'))
remap({'ctrl', 'alt'}, 'l', pressFn({'alt'}, 'right'))
remap({'ctrl', 'shift', 'cmd'}, 'i', pressFn({'shift', 'cmd'}, 'up'))
remap({'ctrl', 'shift', 'cmd'}, 'j', pressFn({'shift', 'cmd'}, 'left'))
remap({'ctrl', 'shift', 'cmd'}, 'k', pressFn({'shift', 'cmd'}, 'down'))
remap({'ctrl', 'shift', 'cmd'}, 'l', pressFn({'shift', 'cmd'}, 'right'))
remap({'ctrl', 'shift', 'alt'}, 'i', pressFn({'shift', 'alt'}, 'up'))
remap({'ctrl', 'shift', 'alt'}, 'j', pressFn({'shift', 'alt'}, 'left'))
remap({'ctrl', 'shift', 'alt'}, 'k', pressFn({'shift', 'alt'}, 'down'))
remap({'ctrl', 'shift', 'alt'}, 'l', pressFn({'shift', 'alt'}, 'right'))
remap({'ctrl', 'cmd', 'alt'}, 'i', pressFn({'cmd', 'alt'}, 'up'))
remap({'ctrl', 'cmd', 'alt'}, 'j', pressFn({'cmd', 'alt'}, 'left'))
remap({'ctrl', 'cmd', 'alt'}, 'k', pressFn({'cmd', 'alt'}, 'down'))
remap({'ctrl', 'cmd', 'alt'}, 'l', pressFn({'cmd', 'alt'}, 'right'))
remap({'ctrl', 'cmd', 'alt', 'shift'}, 'i', pressFn({'cmd', 'alt', 'shift'}, 'up'))
remap({'ctrl', 'cmd', 'alt', 'shift'}, 'j', pressFn({'cmd', 'alt', 'shift'}, 'left'))
remap({'ctrl', 'cmd', 'alt', 'shift'}, 'k', pressFn({'cmd', 'alt', 'shift'}, 'down'))
remap({'ctrl', 'cmd', 'alt', 'shift'}, 'l', pressFn({'cmd', 'alt', 'shift'}, 'right'))
おまけ、
-- delete
remap({'ctrl'}, 'u', pressFn('delete'))
remap({'ctrl'}, 'o', pressFn('delete'))
-- return
remap({'ctrl'}, 'space', pressFn('return'))
-- switch language
remap({'ctrl'}, 'f', pressFn({'cmd'}, 'd'))
これでなんとか明日も仕事ができますw
以上です