LoginSignup
8
4

More than 5 years have passed since last update.

hammerspoonを使って特定のアプリのショートカットキーを上書きする

Posted at

経緯

MacのSkypeアプリは、文字を消そうと思って"^H"を押すと「ブラウザが開いてヘルプページが表示される」というひどい仕様になっている。
このままだと文字の編集中のストレスが尋常ではないので、このアプリでだけ"^H"を"delete"に上書きしたくなった。

つまり、Macで特定のアプリで特定のキーバインドを上書きする方法が必要になったのだが、hammerspoonを使ってうまく対応することができた。

対処方法

hammerspoonのconfigを次のように編集すると期待通りの動きになる。

~/.hammerspoon/init.lua
local function press_delete()
  hs.eventtap.event.newKeyEvent({}, 'delete', true):post()
  hs.timer.usleep(1)
  hs.eventtap.event.newKeyEvent({}, 'delete', false):post()
end
ctrl_h_hkey = hs.hotkey.new({'ctrl'}, 'H', press_delete, nil, press_delete)

hs.window.filter.new('Skype')
  :subscribe(hs.window.filter.windowFocused, function() ctrl_h_hkey:enable() end)
  :subscribe(hs.window.filter.windowUnfocused, function() ctrl_h_hkey:disable() end)

hs.window.filterでwindowの切り替えを検知し、Skypeがフォーカスされた時にキーリマップを有効にし、フォーカスが外れた時に無効にするようにしている。
キーのリマップ方法については参考記事を参考にした。

参考記事

8
4
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
8
4