Skip to content

Instantly share code, notes, and snippets.

@lirongfei123
Created September 23, 2019 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lirongfei123/fe2c26078d9f643dedf7007c277bda05 to your computer and use it in GitHub Desktop.
Save lirongfei123/fe2c26078d9f643dedf7007c277bda05 to your computer and use it in GitHub Desktop.
local function Chinese()
hs.keycodes.currentSourceID("com.sogou.inputmethod.sogou.pinyin")
end
local function English()
hs.keycodes.currentSourceID("com.apple.keylayout.ABC")
end
-- app to expected ime config
local app2Ime = {
['/Applications/DingTalk.app'] = 'Chinese',
['/Applications/NeteaseMusic.app'] = 'Chinese',
['/Applications/微信.app'] = 'Chinese',
['/Applications/MindNode.app'] = 'Chinese',
['/Applications/Preview.app'] = 'Chinese'
}
local function isempty(s)
return s == nil or s == ''
end
function updateFocusAppInputMethod()
local ime = 'English'
if ((not isempty(hs.window.focusedWindow())) and (not isempty(hs.window.focusedWindow():application()))) then
local focusAppPath = hs.window.focusedWindow():application():path()
local expectedIme = app2Ime[focusAppPath]
if expectedIme ~= nil then
ime = expectedIme;
end
end
if ime == 'Chinese' then
Chinese()
else
English()
end
end
--[[
-- helper hotkey to figure out the app path and name of current focused window
hs.hotkey.bind({'ctrl', 'cmd'}, ".", function()
hs.alert.show("App path: "
..hs.window.focusedWindow():application():path()
.."\n"
.."App name: "
..hs.window.focusedWindow():application():name()
.."\n"
.."IM source id: "
..hs.keycodes.currentSourceID())
end)
--]]
-- Handle cursor focus and application's screen manage.
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
updateFocusAppInputMethod()
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment