LoginSignup
2
3

More than 5 years have passed since last update.

Hammerspoonで特定のアプリでだけショートカットキーを有効にする

Posted at

Hammerspoonで特定のアプリでだけキーバインドを有効にするのが意外と大変だったのでメモ。

まずキーマップを作って、アプリの変更を監視して、特定のタイトルを持つアプリが前面に来た時にキーバインドを有効にします。

local YourKeyMap = hs.hotkey.modal.new()

local function appTitle()
   app = hs.application.frontmostApplication()
   if app ~= nil then
      return app:title()
   end
end

local function chooseKeyMap()
   if appTitle() == 'YourAppTitle' then
      print('Turning ON keybindings for: ' .. appTitle())
      YourKeyMap:enter()      
   else
      print('Turnning OFF keybindings for: ' .. appTitle())      
      YourKeyMap:exit()
   end
end

local function appWatcherFunction(appName, eventType, appObject)
   if (eventType == hs.application.watcher.activated) then
      chooseKeyMap()
   end
end

local function sendKey(mods, key)
   return function()
      hs.eventtap.keyStroke(mods, key, 10000)
   end
end

YourKeyMap:bind({'ctrl'}, 'a', sendKey({}, 'b'))

chooseKeyMap()
local appWatcher = hs.application.watcher.new(appWatcherFunction)
appWatcher:start()
2
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
2
3