Hotkey gadgets would be nice!
Posted: Tue Aug 22, 2006 10:23 pm
Indeed! 

http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
#HotKeyGadget = 99 ; or whatever, just remember this is a #gadget so don't reuse it
Procedure ShowMsg(a,b,c,d,e)
If c = 0
SetGadgetText(1, "HOTKEY IS SET")
ElseIf c=1
SetGadgetText(1, "HOTKEY IS PRESSED")
EndIf
Delay(1000)
SetGadgetText(1, "")
EndProcedure
OpenWindow(0,0,0,320,200,"Hotkey Test",#PB_Window_SystemMenu | #PB_Window_ScreenCentered |#PB_Window_MinimizeGadget)
CreateGadgetList(WindowID(0)) ; without this you'd want to do InitCommonControls_()
ButtonGadget(0, 220,10, 80, 20, "Set")
TextGadget(1, 100, 60, 200, 20, "")
hwndHot = CreateWindowEx_(0,"msctls_hotkey32","",#WS_CHILD|#WS_VISIBLE,10,10,200,20,WindowID(0),#HotkeyGadget,GetModuleHandle_(0),0)
SetFocus_(hwndHot)
Repeat
ev = WaitWindowEvent()
Select ev
Case #WM_SYSCOMMAND
If EventwParam() = #SC_HOTKEY
timeSetEvent_(10,10,@ShowMsg(),1,#TIME_ONESHOT)
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case 0
wHotkey.w
; Retrieve the hot key (virtual key code And modifiers)
wHotkey = SendMessage_(hwndHot, #HKM_GETHOTKEY, 0, 0)
; Use the result As wParam For WM_SETHOTKEY.
If SendMessage_(WindowID(0), #WM_SETHOTKEY, wHotkey, 0)
timeSetEvent_(10,10,@ShowMsg(),0,#TIME_ONESHOT)
EndIf
EndSelect
EndSelect
Until ev=#WM_CLOSE
Code: Select all
SendMessage_(hwndHot,#WM_SETFONT,GetStockObject_(#DEFAULT_GUI_FONT),0)
Code: Select all
#HotKeyGadget = 99 ; or whatever, just remember this is a #gadget so don't reuse it
Procedure ShowMsg(a,b,c,d,e)
If c = 0
SetGadgetText(1, "HOTKEY IS SET")
ElseIf c=1
SetGadgetText(1, "HOTKEY IS PRESSED")
EndIf
Delay(1000)
SetGadgetText(1, "")
EndProcedure
Procedure$ GetHotkeyText(hotkeycontrol)
Protected locale, hotkey.w, key, key$, mod$, ctrl$, shift$, alt$, result$
locale = GetKeyboardLayout_(GetCurrentThreadId_())
hotkey.w = SendMessage_(hotkeycontrol, #HKM_GETHOTKEY, 0, 0)
key$ = Space(100)
; Virtual key
key = MapVirtualKeyEx_(hotkey&$FF,0,locale)<<16
If hotkey>>8 & #HOTKEYF_EXT
key | (1<<24) ; Set the extended key bit
EndIf
GetKeyNameText_(key, @key$, 100)
; Modifier(s)
ctrl$ = Space(100) : shift$ = Space(100) : alt$ = Space(100)
GetKeyNameText_(MapVirtualKeyEx_(#VK_CONTROL,0,locale)<<16, @ctrl$, 100) : ctrl$ + " + "
GetKeyNameText_(MapVirtualKeyEx_(#VK_SHIFT, 0,locale)<<16, @shift$, 100) : shift$ + " + "
GetKeyNameText_(MapVirtualKeyEx_(#VK_MENU, 0,locale)<<16, @alt$, 100) : alt$ + " + "
mod$ = ""
If hotkey>>8 & #HOTKEYF_CONTROL : mod$ + ctrl$ : EndIf
If hotkey>>8 & #HOTKEYF_SHIFT : mod$ + shift$ : EndIf
If hotkey>>8 & #HOTKEYF_ALT : mod$ + alt$ : EndIf
; Output string
result$ = mod$+key$
If result$ = "" : result$ = "None" : EndIf
ProcedureReturn result$
EndProcedure
OpenWindow(0,0,0,320,200,"Hotkey Test",#PB_Window_SystemMenu | #PB_Window_ScreenCentered |#PB_Window_MinimizeGadget)
ButtonGadget(0, 220,10, 80, 20, "Set")
TextGadget(1, 100, 60, 200, 20, "")
TextGadget(2, 70,140,80,20, "Hotkey Text:")
TextGadget(3, 150,140,140,20,"")
hwndHot = CreateWindowEx_(0,"msctls_hotkey32","",#WS_CHILD|#WS_VISIBLE,10,10,200,20,WindowID(0),#HotkeyGadget,GetModuleHandle_(0),0)
SetFocus_(hwndHot)
txt$ = GetHotkeyText(hwndhot)
SetGadgetText(3, txt$)
Repeat
ev = WaitWindowEvent()
Select ev
Case #WM_SYSCOMMAND
If EventwParam() = #SC_HOTKEY
timeSetEvent_(10,10,@ShowMsg(),1,#TIME_ONESHOT)
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case 0
; Retrieve the hot key (virtual key code And modifiers)
wHotkey.w = SendMessage_(hwndHot, #HKM_GETHOTKEY, 0, 0)
; Use the result As wParam For WM_SETHOTKEY.
If SendMessage_(WindowID(0), #WM_SETHOTKEY, wHotkey, 0)
; Retrieve the contents of the control in text form
txt$ = GetHotkeyText(hwndhot)
If txt$ <> "None"
timeSetEvent_(10,10,@ShowMsg(),0,#TIME_ONESHOT)
EndIf
SetGadgetText(3, txt$)
EndIf
SetFocus_(hwndHot)
EndSelect
EndSelect
Until ev=#PB_Event_CloseWindow
:roll:Michael Vogel wrote:...is there also a possibility to check combinations including the Windows key (#VK_MOD)?
...how I can use the same font for the hotkey control gadget like in all other gadgets?