Window event carriage return

You need some new stunning features ? Tell us here.
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Window event carriage return

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Window event carriage return

Post 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.
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
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Re: Window event carriage return

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Window event carriage return

Post 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.
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
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Re: Window event carriage return

Post by lesserpanda »

Got it.. Thank you very much!
Post Reply