Hotkey gadgets would be nice!

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Hotkey gadgets would be nice!

Post by Fluid Byte »

Indeed! :!:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I could guess, but what exactly would a hotkey gadget do?
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Like, it shows the hotkey combination you press. Word has one.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes, I agree, I think it would be nice to have this! While the team is working on it, in the meantime here's a little example of how you can "do it yourself". Run this program, set a hotkey and then test it by minimizing the window and then pressing your hotkey combination. If all goes well, your window will be restored when you hit the hotkey:

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 
BERESHEIT
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Cool code, netmaestro - thanks!

But - typically for me :roll: - I have some questions...

...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?

And, can the value assigned to the variable wHotkey be used to register a hotkey?

Thanks,
Michael
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> While the team is working on it

:lol: +1

Seriously: is there a way to make this gadget not be bold?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

@PB: Yes, just stick this line in the code below the CreateWindowEx:

Code: Select all

SendMessage_(hwndHot,#WM_SETFONT,GetStockObject_(#DEFAULT_GUI_FONT),0)
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Two more questions for netmaestro's example:

(1) Is there a way to get the text of the control as a string?
(2) Is there a way to color the background and text of it?

Thanks for any help.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

As to the color, I'm not sure. Still thinking for now. As to retrieving the text, I don't think there's a simple way. It isn't as easy as sending WM_GETTEXT or GetWindowText_() but you can parse the return value of HKM_GETHOTKEY and go from there:

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 
Should be fine for ascii or unicode. Tested with English and German locales, no problems found.
Last edited by netmaestro on Sat Jul 25, 2009 3:21 am, edited 3 times in total.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Thanks netmaestro; I did try #WM_GETTEXT and GetWindowText_() but had
no luck either. But with your amended code, I ran into this problem: Num 6?

Image
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You're right, I forgot to set the extended key bit in the created lParam so that GetKeyNameText_() can distinguish between nums and arrows. Please accept my apologies, posted code is modified and should work now.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

No need to apologise. I'm just glad you're able to help. Will look at it later.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

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?
:roll:
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

For the font just look up a few posts... :wink:
BERESHEIT
Post Reply