forked from mihaifm/screenkey.ahk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenkey.ahk
305 lines (249 loc) · 6.43 KB
/
screenkey.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Config area
; Font settings
fontSize = 20
fontName = Verdana
fontStyle = Bold
; The max number of keys that are listed on the screen
numButtons := 5
; Gui transparecy
transparent := true
; Distance from the buttons to the edge of the window
winMargin := 25
; When this is true, old keys are cleared after the speed timer interval
useSpeedTimer := true
speedTimer := 1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Font metrics functions
FontLen(text)
{
global
return StrLen(text) * fontSize + 25
}
FontHeight()
{
global
return (fontSize * 3)
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Gui Setup
Gui +ToolWindow +AlwaysOnTop
Gui Font, s%fontSize% %fontStyle%, %fontName%
; Create the buttons with 0 width
Loop % numButtons
{
height := FontHeight()
if (A_Index == 1)
Gui Add, Button, w0 h%height% X%winMargin% ym
else
Gui Add, Button, w0 h%height% ym
}
; Show the gui with a default 200 width
Gui Show, w200, Screenkey.ahk
; Save the window id
WinGet k_ID, ID, A
If (transparent)
{
TransColor = F1ECED
Gui Color, %TransColor%
WinSet TransColor, %TransColor% 220, ahk_id %k_ID%
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Hotkeys
CaptureKeyboardInputs()
{
global
static keys
keys=Space,Enter,Tab,Esc,BackSpace,Del,Ins,Home,End,PgDn,PgUp,Up,Down,Left,Right,CtrlBreak,ScrollLock,PrintScreen,CapsLock
,Pause,AppsKey,NumLock,Numpad0,Numpad1,Numpad2,Numpad3,Numpad4,Numpad5,Numpad6,Numpad7,Numpad8,Numpad9,NumpadDot
,NumpadDiv,NumpadMult,NumpadAdd,NumpadSub,NumpadEnter,NumpadIns,NumpadEnd,NumpadDown,NumpadPgDn,NumpadLeft,NumpadClear
,NumpadRight,NumpadHome,NumpadUp,NumpadPgUp,NumpadDel,Media_Next,Media_Play_Pause,Media_Prev,Media_Stop,Volume_Down,Volume_Up
,Volume_Mute,Browser_Back,Browser_Favorites,Browser_Home,Browser_Refresh,Browser_Search,Browser_Stop,Launch_App1,Launch_App2
,Launch_Mail,Launch_Media,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22
,1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
,[,],',=,\,/
Loop Parse, keys, `,
Hotkey, ~*%A_LoopField%, KeyHandleLabel, UseErrorLevel
return
KeyHandleLabel:
KeyHandle()
return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Hotkey handles
current := 1
clear := false
KeyHandle()
{
global
keyname := RegExReplace(A_ThisHotKey, "~\*", "")
dispkey := Up(keyname)
mods := ModifierNames(keyname)
dispkey := mods . dispkey
len := FontLen(dispkey)
; resize the button with what needs to be displayed
GuiControl Move, Button%current%, w%len%
ControlSetText Button%current%, %dispkey%, ahk_id %k_ID%
if (current == 1 && clear)
{
; Clear the rest of buttons - resize them to 0 width
Loop % numButtons - 1
{
curr := A_Index + 1
GuiControl Move, Button%curr%, w0
}
}
; move to the next button
current++
if (current > numButtons)
{
current := 1
clear := true
}
else
{
; reposition the remaining buttons
Loop
{
if (A_Index < current)
continue
prev := A_Index - 1
GuiControlGet Button%prev%, Pos
newX := % Button%prev%X + Button%prev%W
GuiControl Move, Button%A_Index%, X%newX%
if (A_Index >= numButtons)
break
}
}
; calculate the new total width of the window
winWidth := 0
Loop % numButtons
{
GuiControlGet Button%A_Index%, Pos
w := Button%A_Index%W
winWidth := w + winWidth
}
WinMove ahk_id %k_ID%,,,, winWidth + 2 * winMargin
; install the speed timer that clears all the buttons when the next key is hit
if (useSpeedTimer)
{
SetTimer HandleSpeedType, %speedTimer%
}
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Helper functions
IsUpperCase(key)
{
caps := GetKeyState("CapsLock", "T")
shift := GetKeyState("Shift", "P")
if (caps && !shift || !caps && shift)
{
return true
}
return false
}
ModifierNames(key)
{
mods := ""
shift := GetKeyState("Shift", "P")
ctrl := GetKeyState("Ctrl", "P")
alt := GetKeyState("Alt", "P")
win := GetKeyState("LWin", "P")
special := "[,],',=,\,/,;,``,."
if (StrLen(key) == 1)
{
if (key >= "0" && key <="9")
shift := false
if (key >= "a" && key <="z")
shift := false
if (key >= "A" && key <="Z")
shift := false
if (Instr(special, key))
shift := false
}
if (shift)
mods := mods . "Shift+"
if (ctrl)
mods := mods . "Ctrl+"
if (alt)
mods := mods . "Alt+"
if (win)
mods := mods . "Win+"
return (mods)
}
Up(inkey)
{
outkey := ""
if (IsUpperCase(inkey) && StrLen(inkey) == 1 && inkey >= "a" && inkey <= "z")
{
StringUpper outkey, inkey
return outkey
}
shift := GetKeyState("Shift", "P")
if (shift)
{
if (inkey == "1")
return "!"
if (inkey == "2")
return "@"
if (inkey == "3")
return "#"
if (inkey == "4")
return "$"
if (inkey == "5")
return "%"
if (inkey == "6")
return "^"
if (inkey == "7")
return "&&"
if (inkey == "8")
return "*"
if (inkey == "9")
return "("
if (inkey == "0")
return ")"
if (inkey == "-")
return "_"
if (inkey == "=")
return "+"
if (inkey == "[")
return "{"
if (inkey == "]")
return "}"
if (inkey == "\")
return "|"
if (inkey == ",")
return "<"
if (inkey == ".")
return ">"
if (inkey == "/")
return "?"
if (inkey == "``")
return "~"
if (inkey == ";")
return ":"
if (inkey == "'")
{
quote = "
return quote
}
}
return inkey
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Install hotkeys
CaptureKeyboardInputs()
; special handling for special characters
~*;::
~*,::
~*.::
~*`::
KeyHandle()
return
HandleSpeedType:
current := 1
clear := true
SetTimer HandleSpeedType, Off
return
GuiClose:
ExitApp