[G4P] Trigger a GButton when pressing enter in a textfield

edited September 2015 in Library Questions

Hi,

My question is simple but I don't know how to do it, sorry for my poor English.

How to trigger a GButton if I press Entered key when the Focus is set on a textfield?

The only way how I solve this, is copy the code (MY CODE) in "button1_click1" to "textfield_change1" like this:

public void button1_click1(GButton source, GEvent event) { //_CODE_:button:763418:
//MY CODE
} //_CODE_:button:763418:

public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield_main:267071:
  if(event == GEvent.ENTERED){
  //MY CODE
  }
} //_CODE_:textfield_main:267071:

But, copy and paste the same code repeateadly on all textfields and buttons wich I need to do this is a ugly way to do this.

Any ideas?

Thanks

Tagged:

Answers

  • Answer ✓

    The answer is simple, create a method in the main sketch tab to hold the code you want to edit e.g.

    // This method is in the main tab NOT the gui tab
    void doThisCode (){
        // MY CODE
    }
    

    Then in the gui tab call this method in the event handlers

    public void button1_click1(GButton source, GEvent event) { //_CODE_:button:763418:
        doThisCode ();
    } //_CODE_:button:763418:
    
    public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield_main:267071:
      if(event == GEvent.ENTERED){
          doThisCode ();
      }
    } //_CODE_:textfield_main:267071:
    
  • Thank you very much @quark, and congrats for your great work in this community. We use G4P a lot.

    The reference that you make for your libraries are very helpful. This is the first time I had to ask in the forum :D

    Thanks again, regards.

  • Glad you find G4P useful :)

Sign In or Register to comment.