Trigger event for Enter on ButtonGadgets

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Trigger event for Enter on ButtonGadgets

Post by PB »

This might apply for other gadgets too, but I didn't test.

I noticed today that if I press Space on a ButtonGadget,
it triggers an event as though I clicked it with the mouse.
This is good, and wanted. But pressing Enter on it does
nothing. Can this be added to work like Space, please?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Trigger event for Enter on ButtonGadgets

Post by BorisTheOld »

PB wrote:But pressing Enter on it does nothing.
It does for Linux, but not for Windows. :?

We get around the problem by creating a custom event whenever a button has the focus and the Enter key is pressed.

We also create custom button events for Got-Focus and Lost-Focus. It's tricky, but it can be done without having to use operating system API code.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Trigger event for Enter on ButtonGadgets

Post by PB »

Yeah, at the moment I've had to make procedures to handle
the #WM_KEYDOWN and #PB_Event_Gadget events for both
Space and Enter.

Would rather it all be handled with #PB_Event_Gadget instead.
Not a priority request, but it makes everything all that easier.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Trigger event for Enter on ButtonGadgets

Post by jassing »

PB wrote:Yeah, at the moment I've had to make procedures to handle
the #WM_KEYDOWN and #PB_Event_Gadget events for both
Space and Enter.

Would rather it all be handled with #PB_Event_Gadget instead.
Not a priority request, but it makes everything all that easier.
It's standard windows object behaviour-- You can define a button to be "default" and that should trigger if the window received 'enter'.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Trigger event for Enter on ButtonGadgets

Post by BorisTheOld »

jassing wrote:It's standard windows object behaviour-- You can define a button to be "default" and that should trigger if the window received 'enter'.
PB only allows this when the button is created. What's needed is the ability to have the button with focus respond to the Enter key.

Data entry operators find it more natural to use Enter to terminate a field, rather than use Tab or Spacebar, and certainly not the mouse.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Trigger event for Enter on ButtonGadgets

Post by PB »

> It's standard windows object behaviour-- You can define a button to
> be "default" and that should trigger if the window received 'enter'.

Doesn't work. Only Space triggers an event. Observe:

Code: Select all

If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
  ButtonGadget(1,50,50,60,25,"Hit Enter",#PB_Button_Default)
  ButtonGadget(2,150,50,60,25,"Button2")
  ButtonGadget(3,250,50,60,25,"Button3")
  SetActiveGadget(1)
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget
      Debug EventGadget()
    EndIf
  Until ev=#PB_Event_CloseWindow
EndIf
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Trigger event for Enter on ButtonGadgets

Post by doctorized »

February 21th, 2022 and Enter is not triggering event. On the other hand, Space is triggering event just fine.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Trigger event for Enter on ButtonGadgets

Post by mk-soft »

On windows is Enter is not a valid key for buttons

Solution:

Code: Select all


Enumeration MenuItem
  #MenuItem_Return
EndEnumeration

Procedure DoEventMenuReturn()
  Protected gadget
  
  gadget = GetActiveGadget()
  If IsGadget(gadget) And GadgetType(gadget) = #PB_GadgetType_Button
    PostEvent(#PB_Event_Gadget, GetActiveWindow(), gadget, #PB_EventType_LeftClick)
  EndIf
EndProcedure

Procedure DoEventGadgetChangeFocus()
  Protected gadget
  
  gadget = EventGadget()
  Select GadgetType(gadget)
    Case #PB_GadgetType_Editor
      Select EventType()
        Case #PB_EventType_Focus
          Debug "Remove Return"
          RemoveKeyboardShortcut(0, #PB_Shortcut_Return)
        Case #PB_EventType_LostFocus
          Debug "Add Return"
          AddKeyboardShortcut(0, #PB_Shortcut_Return, #MenuItem_Return)
      EndSelect
  EndSelect
EndProcedure

  
If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
  CreateMenu(0, WindowID(0))
  EditorGadget(0, 10, 10, 380, 140)
  ButtonGadget(1, 50, 160, 60, 25,"Hit Enter",#PB_Button_Default)
  ButtonGadget(2, 150, 160, 60, 25,"Button2")
  ButtonGadget(3, 250, 160, 60, 25,"Button3")
  SetActiveGadget(1)
  
  BindEvent(#PB_Event_Gadget, @DoEventGadgetChangeFocus())
  BindMenuEvent(0, #MenuItem_Return, @DoEventMenuReturn())
  AddKeyboardShortcut(0, #PB_Shortcut_Return, #MenuItem_Return)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Gadget
        Debug "Gadget " + EventGadget()
        
    EndSelect
  ForEver
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: Trigger event for Enter on ButtonGadgets

Post by BarryG »

mk-soft wrote: Mon Feb 21, 2022 11:07 pmOn windows is Enter is not a valid key for buttons
What? It certainly is for Win32 executables. Or are you referring to PureBasic exes?
Post Reply