-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathLastkey.ahk
276 lines (265 loc) · 10.4 KB
/
Lastkey.ahk
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
; =================================================================================
; | LastKey.ahk by Holle. Special thanks to "just me" for creating the functions "IsDeadKey()" and "CombineKeys()" plus great support.
; |
; | What's the use of it? --- The global variable LastKey contains the last pressed key or key-combination.
; |
; | Set Mode: LastKeyMode := 1/2/3 ( default: "LastKeyMode := 2" )
; |
; | Mode 1 : The last pressed key (visible char, or keyname). Example1: "LControl + F10" --> "F10"
; | Mode 2 : Same like Mode 1, but combinations will stored too. Example: "LControl + F10" --> "^F10"
; | Mode 3 : Same like Mode 2, but combinations will differentiated by the side. Example: "LControl + F10" --> "<^F10"
; | https://www.autohotkey.com/boards/viewtopic.php?f=6&t=1740&sid=4a1eb1580fd458d392da552fe5cf439c
; | Tip: If the result of the key-combination a visible key (like "Alt + Control + q" --> "@"), in all modes the visible key ist stored.
; =================================================================================
#SingleInstance Force
#Persistent
#NoEnv
Process, Priority, , High
SetBatchLines, -1
hDriver:=DllCall("GetModuleHandle", "uint", 0) ; can be the good solution if dead key header and other...
hKbdHook := DllCall("SetWindowsHookEx", "int", 0x0D, "uint", RegisterCallback("LastKey"), "uint", hDriver, "uint", 0)
OnExit, UnhookKeyboardAndExit
Goto LastKeyIncludePoint
LastKey(nCode, wParam, lParam) {
global LastKey , LastKeyMode , ShiftHook , ControlHook , AltHook , WinHook
static DeadKey := "" , DeadKeyShift := ""
VK:=NumGet(lParam+0), SC:=NumGet(lParam+4), flag:=NumGet(lParam+8), KeyName := GetKeyName("vk" dec2hex(vk,1))
if (flag > 127) { ; "key up"
if KeyName contains Shift
ShiftHook := false
else if KeyName contains Control
ControlHook := false
else if KeyName contains Alt
AltHook := false
else if KeyName contains Win
WinHook := false
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
}
ShiftState := GetKeyState("Shift","P")?"+":""
ControlState := GetKeyState("Control","P")?"^":""
AltState := GetKeyState("Alt","P")?"!":""
LWinState := GetKeyState("LWin","P")?"<#":""
RWinState := GetKeyState("RWin","P")?">#":""
WinState := GetKeyState("Win","P")?"#":LWinState?"#":RWinState?"#":""
LShiftState := GetKeyState("LShift","P")?"<+":""
RShiftState := GetKeyState("RShift","P")?">+":""
LControlState := GetKeyState("LControl","P")?"<^":""
RControlState := GetKeyState("RControl","P")?">^":""
LAltState := GetKeyState("LAlt","P")?"<!":""
RAltState := GetKeyState("RAlt","P")?">!":""
AltGrState := RAltState&&ControlState?"<^>!":""
if ((KeyName = "RAlt") && ControlState)
KeyName := "AltGr"
if (StrLen(KeyName) = 1) { ; Single Key
StringLower, KeyName, KeyName
if DeadKey { ; the key before was a "Dead Key"
sleep 1
LastKey := CombineKeys(KeyName, ShiftState, DeadKey, DeadKeyShift) , DeadKey := DeadKeyShift := ""
}
else if DeadKey := IsDeadKey(VK, , ShiftState ControlState AltState) ; this key is a "Dead key"
DeadKeyShift := ShiftState
else {
CombineKey := CombineKeys(KeyName, ShiftState ControlState AltState)
if ((asc(CombineKey) > 32) && (asc(CombineKey) != asc(KeyName))) || (LastKeyMode = 1) ; visible char
LastKey := CombineKey
else if (LastKeyMode = 3)
LastKey := LControlState RControlState LAltState RAltState LWinState RWinState KeyName
else
LastKey := ControlState AltState WinState KeyName
}
}
else { ; named key
if LastKey contains Button,Wheel,Joy
{
if ShiftHook && (SubStr(KeyName,-4) = "Shift")
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
if ControlHook && (SubStr(KeyName,-6) = "Control")
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
if AltHook && (SubStr(KeyName,-2) = "Alt")
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
if WinHook && (SubStr(KeyName,-2) = "Win")
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
}
prefix := ""
if (LastKeyMode = 1)
LastKeyPrefix := KeyName
else {
States = LShiftState|RShiftState|LControlState|RControlState|LAltState|RAltState|LWinState|RWinState
Loop, parse, States, |
prefix .= KeyName=StrTrimRight(A_LoopField,5)?"":%A_LoopField%
LastKeyPrefix := prefix . KeyName
}
if (KeyName = "AltGr") {
Stringreplace, LastKeyPrefix, LastKeyPrefix, <^, , all
Stringreplace, LastKeyPrefix, LastKeyPrefix, >!, , all
}
if (LastKeyMode = 2) {
CutLastKey := StrTrimRight(LastKeyPrefix,1)
StringReplace, CutLastKey, CutLastKey, <, , All
StringReplace, CutLastKey, CutLastKey, >, , All
LastKeyPrefix := CutLastKey . SubStr(LastKeyPrefix,0,1)
}
LastKey := LastKeyPrefix
}
return DllCall("CallNextHookEx", "uint", hKbdHook, "int", nCode, "uint", wParam, "uint", lParam)
}
IsDeadKey(VK, SC := 0, Modifiers := "") { ; by just me
Static VK_Mod := {"+": 0x10, "^": 0x11, "!": 0x12}
VarSetCapacity(Chars, 32, 0)
VarSetCapacity(ModStates, 256, 0)
For Each, Modifier In StrSplit(Modifiers)
If VK_Mod.HasKey(Modifier)
NumPut(0x80, ModStates, VK_Mod[Modifier], "UChar")
CharCount := DllCall("User32.dll\ToUnicode", "UInt", VK, "UInt", SC, "Ptr", &ModStates, "Str", Chars
, "Int", 16, "UInt", 0, "Int")
If (CharCount < 0) { ; the specified key is a dead-key, remove it from the keyboard buffer
VarSetCapacity(Dummy, 32, 0)
VarSetCapacity(ModStates, 256, 0)
DllCall("User32.dll\ToUnicode", "UInt", 0x20, "UInt", 0, "Ptr", &ModStates, "Str", Dummy
, "Int", 16, "UInt", 0, "Int")
}
Return (CharCount = -1 ? Chars : "")
}
CombineKeys(Key, KeyModifiers := "", DeadKey := "", DeadKeyModifiers := "", Locale := 0) { ; by just me
; for more information about CombineKeys() visit http://ahkscript.org/boards/viewtopic.php?t=1336#p8806
Static VK_Mod := {"+": 0x10, "^": 0x11, "!": 0x12}
HKL := 0 ; initialize HKL
If (Locale) {
Locale := SubStr("0000000" . Locale, -7)
If !(HKL := DllCall("User32.dll\LoadKeyboardLayout", "Str", Locale, "UInt", 0x81, "UInt"))
Return ""
}
If (DeadKey) {
VarSetCapacity(Chars, 32, 0)
VarSetCapacity(ModStates, 256, 0)
DeadKey := SubStr(DeadKey, 1, 1)
VK := GetKeyVK(DeadKey)
SC := GetKeySC(DeadKey)
For Each, Modifier In StrSplit(DeadKeyModifiers)
If VK_Mod.HasKey(Modifier)
NumPut(0x80, ModStates, VK_Mod[Modifier], "UChar")
CharCount := DllCall("User32.dll\ToUnicodeEx", "UInt", VK, "UInt", SC, "Ptr", &ModStates, "Str", Chars
, "Int", 16, "UInt", 0 , "UInt", HKL, "Int")
If (CharCount <> -1) { ; the specified dead-key is invalid
If (HKL)
DllCall("User32.dll\UnloadKeyboardLayout", "UInt", HKL)
Return ""
}
}
VarSetCapacity(Chars, 32, 0)
VarSetCapacity(ModStates, 256, 0)
Key := SubStr(Key, 1, 1)
VK := GetKeyVK(Key)
SC := GetKeySC(Key)
For Each, Modifier In StrSplit(KeyModifiers)
If VK_Mod.HasKey(Modifier)
NumPut(0x80, ModStates, VK_Mod[Modifier], "UChar")
CharCount := DllCall("User32.dll\ToUnicodeEx", "UInt", VK, "UInt", SC, "Ptr", &ModStates, "Str", Chars
, "Int", 16, "UInt", 0, "UInt", HKL, "Int")
If (CharCount < 0) { ; the specified key is a dead-key, remove it from the keyboard buffer and return ""
VarSetCapacity(ModStates, 256, 0)
DllCall("User32.dll\ToUnicodeEx", "UInt", 0x20, "UInt", 0, "Ptr", &ModStates, "Str", Chars
, "Int", 16, "UInt", 0, "UInt", HKL, "Int")
If (HKL)
DllCall("User32.dll\UnloadKeyboardLayout", "UInt", HKL)
Return ""
}
If (HKL)
DllCall("User32.dll\UnloadKeyboardLayout", "UInt", HKL)
If (CharCount < 1) ; the specified combination has no translation for the current state of the keyboard
Return ""
Return StrGet(&Chars, CharCount, "UTF-16")
}
dec2hex(value,CutPrefix = 0) { ; by Holle
; Decimal to Hexadecimal
; Example 1 --> dec2hex(26) = 0x1A
; Example 2 --> dec2hex(26,1) = 1A (Prefix is cut)
Format := A_FormatInteger
SetFormat, IntegerFast, H
value += 0
value .= ""
SetFormat, IntegerFast, %Format%
if CutPrefix
StringTrimLeft, value, value, 2
return value
}
StrTrimRight(string, count) { ; Example --> StrTrimRight("This is a test", 3) --> "This is a t"
StringTrimRight, result, string, %count%
Return result
}
UnhookKeyboardAndExit:
DllCall("UnhookWindowsHookEx", "uint", hKbdHook)
ExitApp
*~LButton::
*~MButton::
*~RButton::
*~XButton1::
*~XButton2::
*~WheelUp::
*~WheelDown::
*~WheelLeft::
*~WheelRight::
*~Joy1::
*~Joy2::
*~Joy3::
*~Joy4::
*~Joy5::
*~Joy6::
*~Joy7::
*~Joy8::
*~Joy9::
*~Joy10::
*~Joy11::
*~Joy12::
*~Joy13::
*~Joy14::
*~Joy15::
*~Joy16::
*~Joy17::
*~Joy18::
*~Joy19::
*~Joy20::
*~Joy21::
*~Joy22::
*~Joy23::
*~Joy24::
*~Joy25::
*~Joy26::
*~Joy27::
*~Joy28::
*~Joy29::
*~Joy30::
*~Joy31::
*~Joy32::
StringTrimLeft, KeyName, A_ThisHotkey, 2 ; Remove *~
ShiftState := GetKeyState("Shift","P")?"+":""
ControlState := GetKeyState("Control","P")?"^":""
AltState := GetKeyState("Alt","P")?"!":""
LWinState := GetKeyState("LWin","P")?"<#":""
RWinState := GetKeyState("RWin","P")?">#":""
WinState := GetKeyState("Win","P")?"#":LWinState?"#":RWinState?"#":""
LShiftState := GetKeyState("LShift","P")?"<+":""
RShiftState := GetKeyState("RShift","P")?">+":""
LControlState := GetKeyState("LControl","P")?"<^":""
RControlState := GetKeyState("RControl","P")?">^":""
LAltState := GetKeyState("LAlt","P")?"<!":""
RAltState := GetKeyState("RAlt","P")?">!":""
AltGrState := RAltState&&ControlState?"<^>!":""
hooks = Shift|Control|Alt|Win
Loop, parse, hooks, |
KeyHook := A_LoopField . "State" , %A_LoopField%Hook := (%KeyHook%)?true:false
if (LastKeyMode = 1)
LastKeyPrefix := KeyName
else
LastKeyPrefix := LShiftState RShiftState LControlState RControlState LAltState RAltState LWinState RWinState KeyName
if (LastKeyMode = 2) {
CutLastKey := StrTrimRight(LastKeyPrefix,1)
StringReplace, CutLastKey, CutLastKey, <, , All
StringReplace, CutLastKey, CutLastKey, >, , All
LastKeyPrefix := CutLastKey . SubStr(LastKeyPrefix,0,1)
}
LastKey := LastKeyPrefix
return
LastKeyIncludePoint: ; make it possible to include at the beginning
{
}