-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotkey-requester.pb
453 lines (376 loc) · 13.5 KB
/
hotkey-requester.pb
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
; HotkeyRequester.pbi
; Dec 1, 2014
; Windows Only
; Written in PB 5.31 x86
; Author: missile69
; http://www.purebasic.fr/english/memberlist.php?mode=viewprofile&u=7421
;
; License: Free to use, modify, or publish any way you wish. Credit appreciated,
; not required.
;
; Main function: HotkeyRequester(WindowNumber.i, Title$, Instruction$, *funcToCall)
;WindowNumber = Window that will respond to #WM_Hotkey. Usually your main window as closed windows
; can't respond to hotkey messages.
;Title$ = Title bar text
;Instruction$ =
;*funcToCall = Pointer to the function you want called when this hotkey is triggered. Function
; should not require parameters
;
;Return Value = ID number of hotkey if successfully set. Zero otherwise.
;
; Opens a simple window where your program's user can easily select pick a
; global hotkey. HotkeyRequester features "live" updating of their keypresses and
; color-based visual feedback to show the validity of their chosen Hotkey
;
; Global hotkeys, as opposed to PB's keyboard shortcuts, are
; available even when your program's window is not focused or is not visible.
EnableExplicit
Enumeration whk 100
#wndHKRequester
#tmrUpdate
#fntHKDisplay
;Gadgets
#lblHKDisplay
#btnOk
#btnCancel
EndEnumeration
Structure _UserHK
vkey.i
mods.i
hWnd.i
ID.i
*function
friendlyString.s
EndStructure
NewList hkList._UserHK()
;This flag ensures only one #WM_Hotkey message is received even if user keeps
;hotkey buttons pushed down.
#MOD_NOREPEAT = 16384
;Colors
#myGreen = $37A537
#myRed = $BE3F3F
Procedure.i _whk_idExists(hkID.i)
Shared hkList()
If hkID
ForEach hkList()
If hkList()\ID = hkID
ProcedureReturn @hkList()
Break
EndIf
Next
EndIf
ProcedureReturn 0
EndProcedure
Procedure.i _whk_isDuplicate(*UserHK._UserHK)
Shared hkList()
Protected r.i
ForEach hkList()
If hkList()\mods = *UserHK\mods And hkList()\vkey = *UserHK\vkey
r = #True
Break
EndIf
Next
ProcedureReturn r
EndProcedure
Procedure WM_HOTKEY_Event(wParam.i, lParam.i)
;This function should be called from your window callback when a
;#WM_HOTKEY message is received.
Shared hkList()
;Debug "WM_HOTKEY id = " + Str(wParam)
If _whk_idExists(wParam)
CallFunctionFast(hkList()\function)
EndIf
EndProcedure
Procedure RemoveHotkey(hkID.i)
;Returns non-zero on success, else returns zero.
;hkID = ID number associated with this hotkey. #PB_All can be used to unregister all
; hotkeys when your program ends.
Shared hkList()
Protected i.i
If hkID = #PB_All
ForEach hkList()
UnregisterHotKey_(hkList()\hWnd, hkList()\ID)
i + 1
Next
ClearList(hkList())
;Debug "Unregistered " + Str(i) + " Hotkeys"
ElseIf hkID
;If the given hotkey ID is valid, unregister it and delete from list.
If _whk_idExists(hkID)
UnregisterHotKey_(hkList()\hWnd, hkList()\ID)
DeleteElement(hkList())
EndIf
EndIf
EndProcedure
Procedure.i _whk_addHotkey(*UserHK._UserHK, Test.b = #False)
;Hotkey = Pointer to filled out _userHK-structured variable. \ID value is always auto-generated
; and the is the return value.
;Test = If true, unregisters the hotkey before exiting and doesn't add it to our hkList().
;
;Return Value = Hotkey ID on success. 0 on error (usually means that hotkey is already used by another program).
Shared hkList()
Protected r.i
Static hkAutoID.i
hkAutoID + 1
With *UserHK
If \friendlyString And \function And \mods And \vkey
\ID = hkAutoID
If _whk_isDuplicate(*UserHK) = #False
r = RegisterHotKey_(\hWnd, \ID, \mods, \vKey)
If r
If Test
UnregisterHotKey_(\hWnd, \ID)
hkAutoID - 1
Else
AddElement(hkList())
hkList()\friendlyString = \friendlyString
hkList()\function = \function
hkList()\hWnd = \hWnd
hkList()\ID = \ID
hkList()\mods = \mods
hkList()\vkey = \vkey
EndIf
r = \ID
Else
hkAutoID - 1
EndIf
Else
hkAutoID - 1
EndIf
EndIf
EndWith
ProcedureReturn r
EndProcedure
Procedure.i HotkeyKeycode(hkID.i)
;Returns the key code associated with given ID.
Shared hkList()
If _whk_idExists(hkID)
ProcedureReturn hkList()\vkey
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i HotkeyModifiers(hkID.i)
;Returns the modifiers associated with given ID. They can be any combination
;of #MOD_ALT, #MOD_CONTROL, #MOD_SHIFT, and #MOD_NOREPEAT
Shared hkList()
If _whk_idExists(hkID)
ProcedureReturn hkList()\mods
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.s HotkeyFriendlyName(hkID.i)
;Returns the user-friendly hotkey description associated with the given hkID
Shared hkList()
If hkID
If _whk_idExists(hkID)
ProcedureReturn hkList()\friendlyString
EndIf
EndIf
ProcedureReturn #NULL$
EndProcedure
Procedure.i isHotkey(hkID.i)
;Returns non-zero if this hotkey ID is valid and registered. Zero when not.
If hkID
ProcedureReturn _whk_idExists(hkID)
EndIf
ProcedureReturn 0
EndProcedure
Procedure.i _whk_checkKB(*UserHK._UserHK)
;Checks Ctrl, Alt, Shift and Keycodes 48-122 which correspond to all numbers and basic visible characters.
;Since hotkeys can only have one vKey code associated with them, this function only returns the key code
;for the first (lowest numbered on Ascii chart) pressed key it finds even if, for example, two letters
;were pressed.
;
;Return value = Non-zero if keyboard state was successfully retrieved. The _UserHK structure passed by
; reference will have its mods, vKey, and friendlyString fields filled in if their
; corresponding keys were pressed.
ClearStructure(*UserHK, _UserHK)
Protected r.i, i.i
Dim kb.b(255)
r = GetKeyboardState_(@kb())
If r
With *UserHK
If kb(#VK_CONTROL) & $ff > 1
\mods + #MOD_CONTROL
\friendlyString + "Ctrl + "
EndIf
If kb(#VK_MENU) & $ff > 1
\mods + #MOD_ALT
\friendlyString + "Alt + "
EndIf
If kb(#VK_SHIFT) & $ff > 1
\mods + #MOD_SHIFT
\friendlyString + "Shift + "
EndIf
If \mods
\mods + #MOD_NOREPEAT
EndIf
For i = '0' To 'z'
If kb(i) & $ff > 1
;Debug Chr(i) + " pressed!"
\friendlyString + Chr(i)
\vKey = i
Break
EndIf
Next
EndWith
EndIf
ProcedureReturn r
EndProcedure
Procedure.i HotkeyRequester(WindowNumber.i, Title$, Instruction$, *funcToCall)
;WindowNumber = Window that will respond to #WM_Hotkey. Usually your main window as closed windows
; can't respond to hotkey messages.
;Title$ = Title bar text
;Instruction$ =
;*funcToCall = Pointer to the function you want called when this hotkey is triggered. Function
; should not require parameters
;
;Return Value = ID number of hotkey if successfully set. Zero otherwise.
Protected._UserHK currKeys, prevKeys
Protected r.i, e.i, h.i, w.i, hkIsValid.i
Protected MaxHK$ = "Ctrl + Alt + Shift + W"
If IsWindow(WindowNumber)
WindowNumber = WindowID(WindowNumber)
If OpenWindow(#wndHKRequester, 0, 0, 400, 150, Title$, #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
TextGadget(#PB_Any, 10, 10, WindowWidth(#wndHKRequester) - 20, 36, Instruction$, #PB_Text_Center)
ButtonGadget(#btnOk, 320, 105, 70, 35, "OK")
DisableGadget(#btnOk, #True)
ButtonGadget(#btnCancel, 240, 105, 70, 35, "Cancel")
;Set up hotkey display area
LoadFont(#fntHKDisplay, "Arial", 14, #PB_Font_Bold)
StartDrawing(WindowOutput(#wndHKRequester))
DrawingFont(FontID(#fntHKDisplay))
h = TextHeight(MaxHK$) + 20
w = TextWidth(MaxHK$) + 20
StopDrawing()
TextGadget(#lblHKDisplay, (400-w)/2, 50, w, h, "None", #PB_Text_Center|#SS_CENTERIMAGE)
SetGadgetColor(#lblHKDisplay, #PB_Gadget_BackColor, #White)
SetGadgetFont(#lblHKDisplay, FontID(#fntHKDisplay))
AddWindowTimer(#wndHKRequester, #tmrUpdate, 150)
Repeat
e = WaitWindowEvent()
Select e
Case #PB_Event_Timer
If _whk_checkKB(@currKeys)
With currKeys
;If keys were pressed
If \friendlyString
;Show pressed keys to user
SetGadgetText(#lblHKDisplay, \friendlyString)
If \friendlyString <> prevKeys\friendlyString
SetGadgetColor(#lblHKDisplay, #PB_Gadget_FrontColor, #Black)
EndIf
If \mods And \vkey
If \mods <> prevKeys\mods Or \vkey <> prevKeys\vkey
\function = *funcToCall
\hWnd = WindowNumber
;Test this hotkey combination.
r = _whk_addHotkey(@currKeys, #True)
If r
;Hotkey is good and able to be registered
prevKeys = currKeys
hkIsValid = #True
SetGadgetColor(#lblHKDisplay, #PB_Gadget_FrontColor, #myGreen)
Else
hkIsValid = #False
SetGadgetColor(#lblHKDisplay, #PB_Gadget_FrontColor, #myRed)
EndIf
EndIf
EndIf
Else
;Keep showing previous valid hotkey if there is one
If prevKeys\ID
If GetGadgetData(#btnOk) = 0
DisableGadget(#btnOk, #False)
SetGadgetData(#btnOk, 1)
EndIf
SetGadgetText(#lblHKDisplay, prevKeys\friendlyString)
SetGadgetColor(#lblHKDisplay, #PB_Gadget_FrontColor, #myGreen)
Else
If GetGadgetData(#btnOk)
DisableGadget(#btnOk, #True)
SetGadgetData(#btnOk, 0)
EndIf
SetGadgetText(#lblHKDisplay, "None")
SetGadgetColor(#lblHKDisplay, #PB_Gadget_FrontColor, #Black)
EndIf
EndIf
EndWith
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case #btnOk
If prevKeys\ID
;Add hotkey for real this time.
r = _whk_addHotkey(@prevKeys)
EndIf
Break
Case #btnCancel
r = 0
Break
EndSelect
Case #PB_Event_CloseWindow
r = 0
Break
EndSelect
ForEver
RemoveWindowTimer(#wndHKRequester, #tmrUpdate)
FreeFont(#fntHKDisplay)
CloseWindow(#wndHKRequester)
EndIf
EndIf
ProcedureReturn r
EndProcedure
DisableExplicit
;////////////////////////////////////// Sample Code \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
Enumeration
#wndMain
#strShowHideHK
#btnEditShowHideHK
#btnDelShowHideHK
EndEnumeration
Procedure ShowHideHKActivated()
SetWindowData(#wndMain, ~GetWindowData(#wndMain))
HideWindow(#wndMain, GetWindowData(#wndMain))
Debug "Press " + GetGadgetText(#strShowHideHK) + " to show/hide window"
EndProcedure
Procedure myWinCallback(hWnd.i, uMsg.i, wParam.i, lParam.i)
If uMsg = #WM_HOTKEY
WM_HOTKEY_Event(wParam, lParam)
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
If OpenWindow(#wndMain, 0, 0, 500, 500, "Hotkey Settings", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
SetWindowCallback(@myWinCallback(), #wndMain)
TextGadget(#PB_Any, 10, 10, 300, 20, "Hotkey for Showing/Hiding this window:")
StringGadget(#strShowHideHK, 10, 40, 200, 25, "None", #PB_String_ReadOnly)
ButtonGadget(#btnEditShowHideHK, 220, 40, 100, 25, "Change Hotkey")
ButtonGadget(#btnDelShowHideHK, 330, 40, 100, 25, "Delete Hotkey")
Define e.i, hkShowHide.i, desc.s = "Press the key combination you want to associate with this action"
Repeat
e = WaitWindowEvent()
If e = #PB_Event_Gadget
Select EventGadget()
Case #btnEditShowHideHK
If isHotkey(hkShowHide)
;If the hotkey was already set, delete it before showing requester again
RemoveHotkey(hkShowHide)
EndIf
hkShowHide = HotkeyRequester(#wndMain, "Select hotkey to show/hide main window", desc, @ShowHideHKActivated())
If hkShowHide
SetGadgetText(#strShowHideHK, HotkeyFriendlyName(hkShowHide))
Else
SetGadgetText(#strShowHideHK, "None")
EndIf
Case #btnDelShowHideHK
RemoveHotkey(hkShowHide)
SetGadgetText(#strShowHideHK, "None")
EndSelect
EndIf
Until e = #PB_Event_CloseWindow
EndIf
RemoveHotkey(#PB_All)
End
CompilerEndIf