Allow Enter key to action gadgets

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: Allow Enter key to action gadgets

Post by PBJim »

Gérard wrote: Sat Dec 07, 2024 11:36 am

Code: Select all

      VK_RETURN = GetAsyncKeyState_(#VK_RETURN)
      If VK_RETURN > 1
        Select ActiveGadget
          Case 0, 1
            Debug "Button "+ActiveGadget+" pressed by Enter"
        EndSelect
      EndIf
    EndIf
Capturing Enter in PB, to imitate behaviour of other applications, can be a challenge in my own experience, especially with EditorGadget.

As soon as the below programme executes, an unrelated button event is triggered. Clicking inside the EditorGadget, triggers a double-button response. Then even entering a single character keypress in the EditorGadget also triggers a double-button response.

Code: Select all

Enumeration Window
  #WinApp
EndEnumeration

If OpenWindow(#WinApp, 0, 0, 622, 530, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonImageGadget(1, 10, 40, 200, 20, 9)
  TextGadget(20, 10, 135, 140, 25,  "Editor Gadget")
  EditorGadget(21, 10, 160, 445, 200, #PB_Editor_WordWrap)
  
  SetActiveGadget(0)
  Repeat
    ev = WaitWindowEvent()
    If GetActiveWindow() = #WinApp
      ActiveGadget = GetActiveGadget()
      VK_RETURN = GetAsyncKeyState_(#VK_RETURN)
      If VK_RETURN > 1
        Select ActiveGadget
          Case 0, 1
            Debug "Button "+ActiveGadget+" pressed by Enter"
        EndSelect
      EndIf
    EndIf
    If ev = #PB_Event_Gadget
      Debug "Button pressed" ; <- This is the goal when I press Enter on a ButtonGadget/ButtonImageGadget.
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow Enter key to action gadgets

Post by BarryG »

PBJim wrote: Sun Dec 08, 2024 10:29 pmCapturing Enter in PB, to imitate behaviour of other applications, can be a challenge in my own experience, especially with EditorGadget.
Yeah, I only do it for buttons, checkboxes, and optiongadgets at the moment.
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Allow Enter key to action gadgets

Post by fryquez »

Use a keyboard hook.

Code: Select all

Enumeration Window
  #WinApp
EndEnumeration

Procedure KeyboardProc(nCode.l, wParam, lParam)
  
  If nCode = #HC_ACTION And wParam = #VK_RETURN And (lParam >> 31) & 1
    
    Protected ActiveGadget = GetActiveGadget()
    
    If ActiveGadget <> -1
      Select GadgetType(ActiveGadget)
        Case #PB_GadgetType_CheckBox, #PB_GadgetType_Option, #PB_GadgetType_Editor
          ;
        Default
          PostEvent(#PB_Event_Gadget, GetActiveWindow(), ActiveGadget, #PB_EventType_LeftClick)
      EndSelect
    EndIf
    
  EndIf
  
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
  
EndProcedure

SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), 0, GetCurrentThreadId_())

If OpenWindow(#WinApp, 0, 0, 622, 530, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonImageGadget(1, 10, 40, 200, 20, 9)
  TextGadget(20, 10, 135, 140, 25,  "Editor Gadget")
  EditorGadget(21, 10, 160, 445, 200, #PB_Editor_WordWrap)
  
  SetActiveGadget(0)
  Repeat
    Define ev = WaitWindowEvent()
    
    Select ev
      Case #PB_Event_Gadget
        
        Select EventType()
          Case #PB_EventType_LeftClick       
            
            Select EventGadget()
              Case 0 : Debug "Button 0 clicked!"
              Case 1 : Debug "Button 1 clicked!"
              Case 2 : Debug "Button 2 clicked!"
            EndSelect
            
        EndSelect
        
    EndSelect
    
  Until ev = #PB_Event_CloseWindow
EndIf

PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: Allow Enter key to action gadgets

Post by PBJim »

fryquez wrote: Mon Dec 09, 2024 10:17 am Use a keyboard hook.
It certainly works fryquez

Incidentally, what happens in the below logic? Is this to allow Enter to be passed through as normal in those gadgets?

Code: Select all

Case #PB_GadgetType_CheckBox, #PB_GadgetType_Option, #PB_GadgetType_Editor
          ;
fryquez
Enthusiast
Enthusiast
Posts: 391
Joined: Mon Dec 21, 2015 8:12 pm

Re: Allow Enter key to action gadgets

Post by fryquez »

PBJim wrote: Mon Dec 09, 2024 2:49 pm Incidentally, what happens in the below logic? Is this to allow Enter to be passed through as normal in those gadgets?
The 3 mentioned GadgetType are ignored, all other will generate a PB_EventType_LeftClick event on Enter key release.

But yourself should decide on wich GadgetType you want this.
Post Reply