Event listeners

Hi Anvil team

Great work on the product. Coming from the Java Spring world - I’m very impressed by the prospects.

I’m interested to find out if it’s possible to add event listeners for keys such as tab/enter within a form. I have a use case where;

  1. I expect a string value (or a double) as an input on a form. In the event of a string being input and the user tabs or keys enter the entire details from the form hit a REST endpoint
  2. The back end performs some logic, based on the full payload, and converts the string token to a double value
  3. The string input gets overwritten by the returned double value

Thanks in advance
Paul

Hi @pmclornan, welcome to the forum!

The easiest way to run some code following user input is to use the event handlers. This great article explains it very well.

https://anvil.works/learn/tutorials/feedback-form/chapter-4/10-set-up-an-event-handler

If it is a text box used for data input, the standard event handlers are
image

When this event occurs, the associated function will run

  def text_box_1_pressed_enter(self, **event_args):
    """This method is called when the user presses Enter in this text box"""
...
    # Write your function in here to carry out the nex steps

  def text_box_1_lost_focus(self, **event_args):
    """This method is called when the TextBox loses focus"""
    # this could be an alternative event handler

You can make the same function run on either event just by changing the handler call.

Does this answer your question?

4 Likes

Great. Not sure why I didn’t spot this earlier.

Thanks for the prompt response too.

1 Like