-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIME.ahk
More file actions
93 lines (80 loc) · 3.39 KB
/
IME.ahk
File metadata and controls
93 lines (80 loc) · 3.39 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
/*
@Example
; 获取当前输入法输入模式
f1::ToolTip IME.GetInputMode()
; 切换当前输入法输入模式
f2::IME.SetInputMode(!IME.GetInputMode())
*/
class IME {
static GetInputMode(hwnd := this.GetFocusedWindow()) {
if !this.GetOpenStatus(hwnd) {
return false
}
return this.GetConversionMode(hwnd) & 1
}
static SetInputMode(mode, hwnd := this.GetFocusedWindow()) {
if mode {
this.SetOpenStatus(true, hwnd)
switch this.GetKeyboardLayout(hwnd) {
case 0x08040804:
this.SetConversionMode(1025, hwnd)
case 0x04110411:
this.SetConversionMode(9, hwnd)
}
}
else {
this.SetOpenStatus(false, hwnd)
}
}
static ToggleInputMode(hwnd := this.GetFocusedWindow()) {
this.SetInputMode(!this.GetInputMode(hwnd), hwnd)
}
static GetOpenStatus(hwnd := this.GetFocusedWindow()) {
DllCall("SendMessageTimeoutW", "ptr", DllCall("imm32\ImmGetDefaultIMEWnd", "ptr", hwnd, "ptr"), "uint", 0x283, "ptr", 0x5, "ptr", 0, "uint", 0, "uint", 200, "ptr*", &status := 0)
return status
}
static SetOpenStatus(status, hwnd := this.GetFocusedWindow()) {
DllCall("SendMessageTimeoutW", "ptr", DllCall("imm32\ImmGetDefaultIMEWnd", "ptr", hwnd, "ptr"), "uint", 0x283, "ptr", 0x6, "ptr", status, "uint", 0, "uint", 200, "ptr*", 0)
}
static GetConversionMode(hwnd := this.GetFocusedWindow()) {
DllCall("SendMessageTimeoutW", "ptr", DllCall("imm32\ImmGetDefaultIMEWnd", "ptr", hwnd, "ptr"), "uint", 0x283, "ptr", 0x1, "ptr", 0, "uint", 0, "uint", 200, "ptr*", &mode := 0)
return mode
}
static SetConversionMode(mode, hwnd := this.GetFocusedWindow()) {
DllCall("SendMessageTimeoutW", "ptr", DllCall("imm32\ImmGetDefaultIMEWnd", "ptr", hwnd, "ptr"), "uint", 0x283, "ptr", 0x2, "ptr", mode, "uint", 0, "uint", 200, "ptr*", 0)
}
static GetKeyboardLayout(hwnd := this.GetFocusedWindow()) {
return DllCall("GetKeyboardLayout", "uint", DllCall("GetWindowThreadProcessId", "ptr", hwnd, "ptr", 0, "uint"), "ptr")
}
static SetKeyboardLayout(hkl, hwnd := this.GetFocusedWindow()) {
SendMessage(0x50, 1, hkl, hwnd)
}
static GetKeyboardLayoutList() {
if cnt := DllCall("GetKeyboardLayoutList", "int", 0, "ptr", 0) {
list := []
buf := Buffer(cnt * A_PtrSize)
loop DllCall("GetKeyboardLayoutList", "int", cnt, "ptr", buf) {
list.Push(NumGet(buf, (A_Index - 1) * A_PtrSize, "ptr"))
}
return list
}
}
static LoadKeyboardLayout(hkl) {
return DllCall("LoadKeyboardLayoutW", "str", Format("{:08x}", hkl), "uint", 0x101)
}
static UnloadKeyboardLayout(hkl) {
return DllCall("UnloadKeyboardLayout", "ptr", hkl)
}
static GetFocusedWindow() {
if foreHwnd := WinExist("A") {
guiThreadInfo := Buffer(A_PtrSize == 8 ? 72 : 48)
NumPut("uint", guiThreadInfo.Size, guiThreadInfo)
DllCall("GetGUIThreadInfo", "uint", DllCall("GetWindowThreadProcessId", "ptr", foreHwnd, "ptr", 0, "uint"), "ptr", guiThreadInfo)
if focusedHwnd := NumGet(guiThreadInfo, A_PtrSize == 8 ? 16 : 12, "ptr") {
return focusedHwnd
}
return foreHwnd
}
return 0
}
}