-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.lua
More file actions
111 lines (94 loc) · 3.65 KB
/
input.lua
File metadata and controls
111 lines (94 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- 希望默认是英文输入状态, 需要的时候才切换到输入法进行中文输入.
--[[InputMethod = {
'baidu-pinyin': 'com.baidu.inputmethod.BaiduIM.pinyin',
'baidu-wubi': 'com.baidu.inputmethod.BaiduIM.wubi',
'sogou-pinyin': 'com.sogou.inputmethod.sogou.pinyin',
'qq-wubi': 'com.tencent.inputmethod.QQInput.QQWubi',
'squirrel-rime': 'com.googlecode.rimeime.inputmethod.Squirrel.Rime',
'squirrel-rime-upstream': 'im.rime.inputmethod.Squirrel.Rime',
'osx-pinyin': 'com.apple.inputmethod.SCIM.ITABC',
'osx-wubi': 'com.apple.inputmethod.SCIM.WBX',
'qingg': 'com.aodaren.inputmethod.Qingg',
'loginput': 'com.logcg.inputmethod.LogInputMac.LogInputMacSP',
'loginput2': 'com.logcg.inputmethod.LogInputMac2.LogInputMac2SP',
'general': 'GENERAL'
}
--]]
local hs = _G.hs
local keycodes = hs.keycodes
local log = require'hs.logger'.new('input','info')
local function log_rime()
local logstr,status,logtype,rc = hs.execute('squirrel_client -g ascii_mode',true)
log.i("log_rime_output " .. tostring(logstr))
log.i("log_rime_status " .. tostring(status))
log.i("log_rime_type " .. tostring(logtype))
log.i("log_rime_rc " .. tostring(rc))
end
local function changeInput_rime(input,Chinese)
local curInput = keycodes.currentSourceID()
if curInput == 'im.rime.inputmethod.Squirrel.Rime' then
if Chinese then
-- log_rime()
hs.execute('squirrel_client -u ascii_mode')
-- log_rime()
else
-- log_rime()
hs.execute('squirrel_client -s ascii_mode')
-- log_rime()
end
elseif curInput ~= input then
keycodes.currentSourceID(input)
end
end
-- local function changeInput(input)
-- if keycodes.currentSourceID() ~= input then
-- keycodes.currentSourceID(input)
-- end
-- end
local function Chinese()
-- changeInput("com.baidu.inputmethod.BaiduIM.wubi")
changeInput_rime('im.rime.inputmethod.Squirrel.Rime',true)
-- hs.alert.show("中文")
end
local function English()
-- changeInput("com.apple.keylayout.US")
changeInput_rime("com.apple.keylayout.US",false)
-- hs.alert.show("英文")
end
local function set_app_input_method(app_name, set_input_method_function, event)
event = event or hs.window.filter.windowFocused
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('Alfred 3', English, hs.window.filter.windowCreated)
-- set_app_input_method('VimR', English)
-- set_app_input_method('MacVim', English)
-- set_app_input_method('iTerm2', English)
-- set_app_input_method('Google Chrome', English)
set_app_input_method('微信', Chinese)
set_app_input_method('钉钉', Chinese)
-- hs.keycodes.inputSourceChanged(
-- function()
-- if string.find(hs.keycodes.currentSourceID(), "com.apple.keylayout.US") then
-- -- hs.alert.show("英文" .. hs.keycodes.currentSourceID())
-- hs.alert.show("英文")
-- else
-- hs.alert.show("中文")
-- end
-- end
-- )
-- -- -- 如果不知道当前的应用的名字和输入法SourceID, 可以用下面的函数查看:
-- hs.hotkey.bind({"cmd","ctrl","alt"}, ".", 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)