Why is the button not activated by pressing return ?

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7586
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Why is the button not activated by pressing return ?

Post by infratec »

Hi,

I just discoverd, that a ButtonGadget() is not activated by pressing return if the button
is the active gadget.

Is this normal ?

What can I do to make it behave as I want ?
(PB 4.60 Win 32)

Code: Select all

OpenWindow(0, 0, 0, 120, 40, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

ButtonGadget(0, 10, 10, 100, 20, "Press return")
SetActiveGadget(0)

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget
    If EventGadget() = 0
      Debug "Pressed"
    EndIf
  EndIf
Until Event = #PB_Event_CloseWindow
Bernd
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Why is the button not activated by pressing return ?

Post by Little John »

infratec wrote:Hi,

I just discoverd, that a ButtonGadget() is not activated by pressing return if the button
is the active gadget.

Is this normal ?
Hi,

as far as I know this is normal. At lest on Windows, we are supposed to press [Space] instead of [Return]. :-)
infratec wrote:What can I do to make it behave as I want ?
(PB 4.60 Win 32)
I use AddKeyboardShortcut() to make it behave that way.

Regards, Little John
infratec
Always Here
Always Here
Posts: 7586
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Why is the button not activated by pressing return ?

Post by infratec »

Hm,

ok, AddKeyboardShortcut() is a solution.

But the MessageRequester() buttons from Windows (for example) works with return.
So I thought that also the buttons of PB are working with return and not with space :(

Bernd
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Why is the button not activated by pressing return ?

Post by Fangbeast »

Hi, I just discoverd, that a ButtonGadget() is not activated by pressing return if the button is the active gadget. Is this normal ? What can I do to make it behave as I want ?
As far as I can see, buttongadgets don't generate events upon given focus, not like a string gadget or list/listicon, they just become the active gdadget.

I found this code in the forum to generate events to send to gadgets though, from a post by t57042

(PB 4.60 Win 32)

Import "gadget.lib"
PB_Gadget_SendGadgetCommand(hWnd, EventType)
EndImport

PB_Gadget_SendGadgetCommand(GadgetID(1), #PB_EventType_LeftClick)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Why is the button not activated by pressing return ?

Post by RASHAD »

Hi everyone
@infratec I just used the document example
There is no mention for #PB_EventType_ReturnKey as you can see
Next is for Windows

Code: Select all



OpenWindow(0, 0, 0, 120, 40, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ButtonGadget(0, 10, 10, 100, 20, "Press return")
SetActiveGadget(0)

Repeat
       Event = WaitWindowEvent()       
       Select Event       
        Case #PB_Event_Gadget
           Select EventGadget()
             Case 0
               Select EventType()               
                 Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
                 Case #PB_EventType_RightClick       : Debug "Click with right mouse button"
                 Case #PB_EventType_LeftDoubleClick  : Debug "Double-click with left mouse button"
                 Case #PB_EventType_RightDoubleClick : Debug "Double-click with right mouse button"
               EndSelect
           EndSelect
           
        Case #WM_CHAR
          Select EventwParam()
            Case 13
                AG = GetActiveGadget()
                PostMessage_(GadgetID(AG), #WM_LBUTTONDOWN, 0, 0)
                PostMessage_(GadgetID(AG), #WM_LBUTTONUP, 0, 0)

          EndSelect
      
       EndSelect
     Until Event = #PB_Event_CloseWindow
Egypt my love
infratec
Always Here
Always Here
Posts: 7586
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Why is the button not activated by pressing return ?

Post by infratec »

RASHAD wrote: There no mention for #PB_EventType_ReturnKey as you can see
Yes,
but why it works with the space key ?

Btw. thanks for your code.
But to be crosscompatible I will use AddKeyboardShortcut() and put all my button event stuff in
own procedures.
Last edited by infratec on Fri Feb 24, 2012 12:41 pm, edited 1 time in total.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Why is the button not activated by pressing return ?

Post by Little John »

This is an example how I use to solve the problem:

Code: Select all

EnableExplicit

;-- Windows
Enumeration
   #MyWin
EndEnumeration

;-- Gadgets
Enumeration
   #Btn_Ok
EndEnumeration

;-- Keys for shortcuts
Enumeration 1
   #KeyReturn
EndEnumeration


Define Event, Gadget

OpenWindow(#MyWin, 0, 0, 120, 40, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ButtonGadget(#Btn_Ok, 10, 10, 100, 20, "OK")
SetActiveGadget(#Btn_Ok)
AddKeyboardShortcut(#MyWin, #PB_Shortcut_Return, #KeyReturn)

Repeat
   Event = WaitWindowEvent()
   Gadget = EventGadget()
   
   If Event = #PB_Event_Menu
      If EventMenu() = #KeyReturn
         Event = #PB_Event_Gadget
         If GetActiveGadget() = #Btn_Ok
            Gadget = #Btn_Ok
         EndIf               
      EndIf
   EndIf
   
   If Event = #PB_Event_Gadget
      If Gadget = #Btn_Ok
         Debug "'OK' pressed"
      EndIf   
   EndIf
Until Event = #PB_Event_CloseWindow
Regards, Little John
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Why is the button not activated by pressing return ?

Post by RASHAD »

:mrgreen:
I never noticed that or maybe I forget about that after long time
I think the team have some problems with the Return Key and different types of
objects
Egypt my love
Randy Walker
Addict
Addict
Posts: 991
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Why is the button not activated by pressing return ?

Post by Randy Walker »

Variation of the above. (but no libs required and still using Windows API stuff)

You can actually *see* the button move.

Code: Select all

OpenWindow(0, 0, 0, 120, 40, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ButtonGadget(0, 10, 10, 100, 20, "Press return")
SetActiveGadget(0)

Repeat
  Event = WaitWindowEvent()
  Select Event       
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          Select EventType()               
            Case #PB_EventType_LeftClick        : Debug "Click with left mouse button"
            Case #PB_EventType_RightClick       : Debug "Click with right mouse button"
            Case #PB_EventType_LeftDoubleClick  : Debug "Double-click with left mouse button"
            Case #PB_EventType_RightDoubleClick : Debug "Double-click with right mouse button"
          EndSelect
      EndSelect
      
    Case #WM_CHAR
      Select EventwParam()
        Case 13
          keybd_event_(#VK_SPACE,0,0,0)
          WaitWindowEvent()
          WaitWindowEvent()
          WaitWindowEvent()
          keybd_event_(#VK_SPACE,0,0,0)
          keybd_event_(#VK_SPACE,0,#KEYEVENTF_KEYUP,0)
          
      EndSelect
      
  EndSelect
Until Event = #PB_Event_CloseWindow
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Why is the button not activated by pressing return ?

Post by charvista »

@Bernd,
This way you can have the exact same effect as the MessageRequester.
As it uses API, it is Windows only.

Code: Select all

Procedure zOclick(Gadget.i)
      GetCursorPos_(cp.POINT)
      GetWindowRect_(GadgetID(Gadget),r.RECT)
      SetCursorPos_((r\left),(r\top))
      mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
      mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)
      SetCursorPos_(cp\x,cp\y)
EndProcedure

Win=OpenWindow(#PB_Any, 0, 0, 320, 240, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
    BtnOk=ButtonGadget(#PB_Any, 10, 180, 88, 26, "OK", #PB_Button_Default)
    AddKeyboardShortcut(Win, #PB_Shortcut_Return, 13)
    SetActiveGadget(BtnOk)
    MessageRequester("Real MessageRequester","This is the real MessageRequester")
    Repeat
        Event = WaitWindowEvent()
        Select Event
            Case #PB_Event_Menu
                Select EventMenu()
                    Case 13
                        zOclick(BtnOk)
                EndSelect  
            Case #PB_Event_Gadget
                Select EventGadget()
                    Case BtnOk
                        Debug "Pressed"
                EndSelect
        EndSelect
    Until Event = #PB_Event_CloseWindow
- Clicking on OK works.
- Pressing Space works.
- Pressing Return works.

The procedure zOclick simulates a mouse click.
Additionally, the flag #PB_Button_Default on the button definition gives the appearance of a default button.
Cheers!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply