dsdshcym / dsdshcym.github.io

https://yiming.dev/
1 stars 1 forks source link

Use Hammerspoon to auto switch input methods - dsdshome #3

Open utterances-bot opened 5 years ago

utterances-bot commented 5 years ago

Use Hammerspoon to auto switch input methods - dsdshome

用 Hammerspoon 实现切换 App 时自动切换输入法

https://dsdshcym.github.io/blog/2017/08/09/use-hammerspoon-to-auto-switch-input-methods/

lizyn commented 5 years ago

如果要让 window filter 监测多个事件, 不应该用 or 连接事件, 而应该用事件的列表吧.

dsdshcym commented 5 years ago

@lizyn 这里并不是想要监听多个事件,而是因为有些 App (如:Spotlight)比较特殊,启动时并不会触发 hs.window.filter.windowFocused 事件,而是 hs.window.filter.windowCreated 事件,所以使用了 or 来设置 hs.window.filter.windowFocused 为默认值。

如果更合理一点的话,应该是

local function set_app_input_method(app_name, set_input_method_function, event)
  hs.window.filter.new(app_name)
    :subscribe(event, function()
                 set_input_method_function()
              end)
end

set_app_input_method('Hammerspoon', English, hs.window.filter.windowCreated)
set_app_input_method('Spotlight', English, hs.window.filter.windowCreated)
set_app_input_method('Emacs', English, hs.window.filter.windowFocused)
set_app_input_method('Slack', English, hs.window.filter.windowFocused)
set_app_input_method('iTerm2', English, hs.window.filter.windowFocused)
set_app_input_method('Google Chrome', English, hs.window.filter.windowFocused)

set_app_input_method('WeChat', Chinese, hs.window.filter.windowFocused)
set_app_input_method('Telegram', Chinese, hs.window.filter.windowFocused)
lizyn commented 5 years ago

嗯嗯,那是我误解了。虽然无关, 但是提一下, 对于 iTerm2 这种既可能触发 focused 又可能触发 created 的应用,如果要同时监测这两种事件, 就可以用 event = {hs.window.filter.windowFocused, hs.window.filter.windowCreated}