Page 2 of 2

Re: Allow Enter key to action gadgets

Posted: Sun Dec 08, 2024 10:29 pm
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

Re: Allow Enter key to action gadgets

Posted: Mon Dec 09, 2024 8:20 am
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.

Re: Allow Enter key to action gadgets

Posted: Mon Dec 09, 2024 10:17 am
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


Re: Allow Enter key to action gadgets

Posted: Mon Dec 09, 2024 2:49 pm
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
          ;

Re: Allow Enter key to action gadgets

Posted: Mon Dec 09, 2024 4:20 pm
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.