How to effect "enter" key to a StringGadget?

Just starting out? Need help? Post your questions and find answers here.
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

How to effect "enter" key to a StringGadget?

Post by AlanFoo »

Hello experts...

Hope you can help..

I use stringGadgets for say do keying in passwords or other deails.
But after keying in the details e.g. password, with the curser still in the stringadget ... pressing enter does not effect anything to enable me to read the password .

Hope someone can advise me how to set the "enter key" to effect an action based on the curser's position being in the stringgadget 's box?

Thanks

Alan
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to effect "enter" key to a StringGadget?

Post by RASHAD »

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    
  StringGadget(0,  10, 10, 200, 24, "")
  
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 15)

  Repeat
    Select WaitWindowEvent()
    
     Case #PB_Event_Menu
; 
         Select EventMenu()
           Case 15
           If GetActiveGadget() = 0
             MessageRequester("Info", "Return key pressed", 0)
           EndIf
         EndSelect
         
    Case #PB_Event_CloseWindow
       Quit = 1
    
    Case #PB_Event_Gadget

      Select EventGadget()
        Case 0          
         
      EndSelect

    EndSelect

  Until Quit = 1

EndIf

End  

Egypt my love
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

Re: How to effect "enter" key to a StringGadget?

Post by AlanFoo »

Dear Rashad...

Many thanks..

Got it.

Regards
Alan
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How to effect "enter" key to a StringGadget?

Post by TI-994A »

Hi RASHAD! Shou akhbarak?

Hi Alan. As always, RASHAD has provided a very nice, simple and cross-platform solution. However, in case you may be interested, here's an alternative method, where the StringGadget() is subclassed, trapping the [ENTER] key and also all the non-alphanumerics:

Code: Select all

Enumeration
  #MainWindow
  #InputBox
  #InputBox2
EndEnumeration

Procedure InputProc(hWnd, uMsg, wParam, lParam)
  Shared sysProc
  If uMsg = #WM_CHAR 
    If wParam = #VK_RETURN 
      SetGadgetText(#InputBox2, GetGadgetText(#InputBox))
      SetActiveGadget(#InputBox2)
      ProcedureReturn
    ;filters the alpha-numerics and Backspace  
    ElseIf wParam < 48 Or wParam > 57
      If wParam < 65 Or wParam > 90
        If wParam < 97 Or wParam > 122
          If wParam <> 8
            ProcedureReturn
          EndIf
        EndIf
      EndIf
    EndIf    
  EndIf       
  ProcedureReturn CallWindowProc_(sysProc, hWnd, uMsg, wParam, lParam)
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 250, 120, "Subclassed StringGadget", wFlags)
StringGadget(#InputBox, 25, 20, 200, 20, "", #PB_String_Password)
StringGadget(#InputBox2, 25, 60, 200, 20, "")
SetActiveGadget(#InputBox)
sysProc = SetWindowLongPtr_(GadgetID(#InputBox), #GWL_WNDPROC, @InputProc())

While WaitWindowEvent() ! #PB_Event_CloseWindow : CloseWindow : Wend
Hope it may be useful to use.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to effect "enter" key to a StringGadget?

Post by ts-soft »

Here my version (using WinAPI!)

Code: Select all

 EnableExplicit

Procedure EmulateDialogCallback(hWnd, uMsg, wParam, lParam)
  Protected oldproc = GetProp_(hWnd, "oldproc")
 
  Select uMsg
    Case #WM_NCDESTROY
      RemoveProp_(hWnd, "oldproc")
    Case #WM_KEYDOWN
      Select GadgetType(GetDlgCtrlID_(hWnd))
        Case #PB_GadgetType_Button, #PB_GadgetType_ButtonImage
          If wParam = #VK_RETURN
            SendMessage_(hWnd, #BM_CLICK , 0, 0)
          EndIf
        Case #PB_GadgetType_String
          If wParam = #VK_RETURN
            SetFocus_(GetNextDlgTabItem_(GetParent_(hWnd), hWnd, #False))
          EndIf
        Case #PB_GadgetType_Option
          If wParam = #VK_DOWN
            SendMessage_(GetNextDlgGroupItem_(GetParent_(hWnd), hWnd, #False), #BM_CLICK , 0, 0)
          ElseIf wparam = #VK_UP
            SendMessage_(GetNextDlgGroupItem_(GetParent_(hWnd), hWnd, #True), #BM_CLICK , 0, 0)
          EndIf
      EndSelect
  EndSelect
 
  ProcedureReturn CallWindowProc_(oldproc, hWnd, uMsg, wParam, lParam)
EndProcedure

Procedure EmulateDialog(ID)
  Protected oldproc = SetWindowLongPtr_(GadgetID(ID), #GWL_WNDPROC, @EmulateDialogCallback())
  ProcedureReturn SetProp_(GadgetID(ID), "oldproc", oldproc)
EndProcedure

; Example

Define i
OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   
ButtonGadget(0, 10, 10, 200, 20, "Default Button")
StringGadget(1, 10, 40, 200, 20, "", #PB_String_Password)
StringGadget(2, 10, 70, 200, 20, "")
OptionGadget(3, 10, 100, 60, 20, "Option 1")
OptionGadget(4, 10, 130, 60, 20, "Option 2")
OptionGadget(5, 10, 160, 60, 20, "Option 3")
SetGadgetState(3, 1)
SetActiveGadget(0)
For i = 0 To 5
  EmulateDialog(i)
Next

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow : Break
    Case #PB_Event_Gadget
      Debug "Gadget: " + Str(EventGadget()) + " : Type: " + Str(EventType())
  EndSelect
ForEver
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
AlanFoo
Enthusiast
Enthusiast
Posts: 172
Joined: Fri Jul 24, 2009 6:24 am
Location: Malaysia

Re: How to effect "enter" key to a StringGadget?

Post by AlanFoo »

TI-994A wrote:Hi RASHAD! Shou akhbarak?

Hi Alan. As always, RASHAD has provided a very nice, simple and cross-platform solution. However, in case you may be interested, here's an alternative method, where the StringGadget() is subclassed, trapping the [ENTER] key and also all the non-alphanumerics:

Code: Select all

Enumeration
  #MainWindow
  #InputBox
  #InputBox2
EndEnumeration

Procedure InputProc(hWnd, uMsg, wParam, lParam)
  Shared sysProc
  If uMsg = #WM_CHAR 
    If wParam = #VK_RETURN 
      SetGadgetText(#InputBox2, GetGadgetText(#InputBox))
      SetActiveGadget(#InputBox2)
      ProcedureReturn
    ;filters the alpha-numerics and Backspace  
    ElseIf wParam < 48 Or wParam > 57
      If wParam < 65 Or wParam > 90
        If wParam < 97 Or wParam > 122
          If wParam <> 8
            ProcedureReturn
          EndIf
        EndIf
      EndIf
    EndIf    
  EndIf       
  ProcedureReturn CallWindowProc_(sysProc, hWnd, uMsg, wParam, lParam)
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(#MainWindow, #PB_Any, #PB_Any, 250, 120, "Subclassed StringGadget", wFlags)
StringGadget(#InputBox, 25, 20, 200, 20, "", #PB_String_Password)
StringGadget(#InputBox2, 25, 60, 200, 20, "")
SetActiveGadget(#InputBox)
sysProc = SetWindowLongPtr_(GadgetID(#InputBox), #GWL_WNDPROC, @InputProc())

While WaitWindowEvent() ! #PB_Event_CloseWindow : CloseWindow : Wend
Hope it may be useful to use.
Thanks TI-994A

Yes, it would be useful indeed for filling up forms.


Alan
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to effect "enter" key to a StringGadget?

Post by netmaestro »

The keyboard shortcut is the most straightforward and crossplatform way to achieve this. However, adding a keyboard shortcut and leaving it on all the time is not desirable as it will interfere with other gadgets that use the return key, for example the editorgadget. Fortunately the string gadget handles two useful events, #PB_EventType_Focus and #PB_EventType_LostFocus. If you look for these in your event loop, add the keyboard shortcut on the focus event and remove it on the lostfocus event, your solution will retain all its crossplatform functionality while working and playing well with the other kids.
BERESHEIT
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to effect "enter" key to a StringGadget?

Post by RASHAD »

Taking what NM said into consideration in other way

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
   
  StringGadget(0,10,10,200, 24, "")
  StringGadget(1,10,40,200, 24, "")
  EditorGadget(2,10,80,200,100)

  Repeat
    Select WaitWindowEvent()
   
     Case #PB_Event_Menu
;
         Select EventMenu()
           Case 15
               If Flag = 1 And GetActiveGadget() = 0
                 MessageRequester("Info", "Return key pressed for Gadget 0",0)
               ElseIf Flag = 1 And GetActiveGadget() = 1
                 MessageRequester("Info", "Return key pressed for Gadget 1",0)
               EndIf
         EndSelect
         
    Case #PB_Event_CloseWindow
       Quit = 1
   
    Case #PB_Event_Gadget

      Select EventGadget()
        Case 0
           If Flag = 0
               AddKeyboardShortcut(0, #PB_Shortcut_Return, 15)
               Flag = 1
           EndIf
           
        Case 1
            If Flag = 0
               AddKeyboardShortcut(0, #PB_Shortcut_Return, 15)
               Flag = 1
            EndIf
           
        Case 2
            If Flag = 1
               RemoveKeyboardShortcut(0,#PB_Shortcut_Return)
               Flag = 0
            EndIf            
         
      EndSelect

    EndSelect

  Until Quit = 1

EndIf

End  
Egypt my love
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: How to effect "enter" key to a StringGadget?

Post by BorisTheOld »

netmaestro wrote:The keyboard shortcut is the most straightforward and crossplatform way to achieve this. However, adding a keyboard shortcut and leaving it on all the time is not desirable as it will interfere with other gadgets that use the return key, for example the editorgadget. Fortunately the string gadget handles two useful events, #PB_EventType_Focus and #PB_EventType_LostFocus. If you look for these in your event loop, add the keyboard shortcut on the focus event and remove it on the lostfocus event, your solution will retain all its crossplatform functionality while working and playing well with the other kids.
Yes, this approach works very well. We've been using these techniques with VB, PowerBasic, and now with PB.

We've found that OS API functions often get in the way of what we wish to do, so we've developed various strategies for controlling the GUI from within our own code. For example, we don't rely on the tab order to navigate through complex screens. Instead, we control everything from within our code using our own control blocks. As much as possible we avoid relying on the operating system's default actions.

Each time an event occurs we change the program's state, then perform whatever actions are necessary based on predefined rules. For example, we might disable some string gadgets, enable some buttons and shortcut keys, activate some string gadgets, and move focus to the first string gadget in the active group.

With VB and PowerBasic, our programs consisted of thousands of control blocks and many special purpose modules, all linked together by the code that represents the rules.

Now, using PB, we're converting everything to classes. This retains our state-machine approach, but the code has a much more simple topology. The rules are determined by the properties of the class instances and by the way the instances are linked together.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply