Page 1 of 1

How to effect "enter" key to a StringGadget?

Posted: Mon Oct 08, 2012 5:11 am
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

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

Posted: Mon Oct 08, 2012 5:28 am
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  


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

Posted: Mon Oct 08, 2012 5:56 am
by AlanFoo
Dear Rashad...

Many thanks..

Got it.

Regards
Alan

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

Posted: Mon Oct 08, 2012 6:13 am
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.

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

Posted: Mon Oct 08, 2012 6:26 am
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

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

Posted: Mon Oct 08, 2012 8:13 am
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

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

Posted: Mon Oct 08, 2012 7:39 pm
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.

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

Posted: Tue Oct 09, 2012 3:17 am
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  

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

Posted: Wed Oct 10, 2012 12:03 am
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.