Page 1 of 1

Window event carriage return

Posted: Wed Feb 12, 2020 2:44 am
by lesserpanda
Hi, how do I enable a form to capture the carriage return when pressed.

So I have form with username and password for login purposes.

When the user presses carriage return, I want it to be like the 'login' button which I capture and process.

I looked at all the form events, and I didn't find it.

Can someone please advice.

Re: Window event carriage return

Posted: Wed Feb 12, 2020 3:29 am
by TI-994A
lesserpanda wrote:...When the user presses carriage return, I want it to be like the 'login' button which I capture and process...
One approach:

Code: Select all

Procedure doLogin() 
  MessageRequester("Login", "Login button clicked!")
  ; login processes here...
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(0, 0, 0, 300, 130, "Keyboard Shortcuts", wFlags)
  StringGadget (1, 10, 10, 280, 30, "Name")
  StringGadget (2, 10, 50, 280, 30, "Password")  
  ButtonGadget (3, 10, 90, 280, 30, "LOGIN")  
  SetActiveGadget(1)
  
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 1)  
  
  Repeat
    event = WaitWindowEvent()
    Select event
        
      Case #PB_Event_CloseWindow
        appQuit = #True
        
      Case #PB_Event_Gadget
        Select EventGadget()
            
          Case 3 ;login button
            doLogin()             
            
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()            
            
          Case 1 ;return key
            Select GetActiveGadget()
                
              Case 1 ;name box
                SetActiveGadget(2)
                
              Case 2 ;password box
                doLogin()                  
                
            EndSelect            
            
        EndSelect
        
    EndSelect
    
  Until appQuit
  
EndIf
When the keyboard focus is on the NAME input, carriage return will set the focus to the PASSWORD input. When the keyboard focus is on the PASSWORD input, carriage return will simulate the LOGIN button being clicked.

Re: Window event carriage return

Posted: Wed Feb 12, 2020 5:11 am
by lesserpanda
Hi thanks for the quick reply and great code. Much appreciated.

It's all good now and can capture the chr(13) but how do I get the values of those StringGadgets? Sorry for the newbie question. First day on PB.

I was trying ..

Code: Select all

If OpenWindow(0, x, y, width, height, "Login", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_WindowCentered)
    strEmail = StringGadget(1, 430, 130, 220, 25, "", #PB_String_LowerCase)
    TextGadget(#PB_Any, 350, 130, 70, 25, "Email:")
    ButtonGadget(#PB_Any, 520, 210, 130, 45, "Login")
    TextGadget(#PB_Any, 350, 170, 70, 25, "Password")
    strPassword = StringGadget(2, 430, 170, 220, 25, "", #PB_String_Password)
    AddKeyboardShortcut(0, #PB_Shortcut_Return, 1) 
    Repeat
      event = WaitWindowEvent()
      Select event
          
        Case #PB_Event_CloseWindow
          appQuit = #True
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
            Case 3 ;login button
              email.s = GetGadgetText(strEmail)
              password.s = GetGadgetText(strPassword)
              doLogin(email, password)        
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()           
              
            Case 1 ;return key
              Select GetActiveGadget()
                  
                Case 1 ;name box
                  SetActiveGadget(2)
                  
                Case 2 ;password box               
                  email.s = GetGadgetText(strEmail)
                  password.s = GetGadgetText(strPassword)
                  doLogin(email, password)        
                  
              EndSelect           
              
          EndSelect
          
      EndSelect
      
    Until appQuit
  EndIf
But it says specified #Gadget is not intialized

Re: Window event carriage return

Posted: Wed Feb 12, 2020 5:39 am
by TI-994A
lesserpanda wrote:...how do I get the values of those StringGadgets?
Simply like this:

Code: Select all

Procedure doLogin() 
  msgText$  = "Name: " + GetGadgetText(1) + #CRLF$ + 
              "Password: " + GetGadgetText(2)
  MessageRequester("Login", msgText$)
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(0, 0, 0, 300, 130, "Keyboard Shortcuts", wFlags)
  StringGadget (1, 10, 10, 280, 30, "Name")
  StringGadget (2, 10, 50, 280, 30, "Password")  
  ButtonGadget (3, 10, 90, 280, 30, "LOGIN")  
  SetActiveGadget(1)
  
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 1)  
  
  Repeat
    event = WaitWindowEvent()
    Select event
        
      Case #PB_Event_CloseWindow
        appQuit = #True
        
      Case #PB_Event_Gadget
        Select EventGadget()
            
          Case 3 ;login button
            doLogin()             
            
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()            
            
          Case 1 ;return key
            Select GetActiveGadget()
                
              Case 1 ;name box
                SetActiveGadget(2)
                
              Case 2 ;password box
                doLogin()                  
                
            EndSelect            
            
        EndSelect
        
    EndSelect
    
  Until appQuit
  
EndIf
One point to note: the following syntax assigns the number 2 as the gadget number (result is non-zero if the gadget is instantiated successfully):

Code: Select all

result = StringGadget(2, 430, 170, 220, 25, "", #PB_String_Password)
The syntax for assigning the gadget number dynamically to the strPassword variable is:

Code: Select all

strPassword = StringGadget(#PB_Any, 430, 170, 220, 25, "", #PB_String_Password)
That is the cause of the specified #Gadget is not intialized error.

Re: Window event carriage return

Posted: Wed Feb 12, 2020 9:12 am
by lesserpanda
Got it.. Thank you very much!