Input from Text Area/Input Box Etc upon pressing Enter Key and then Text getting Cleared

Hi All,

Can you guys please tell me how to deal with the sending input values upon pressing the Enter Key? For example, I have an input box, and a display label. The label shows whatever is input via the input box, but after typing my string in the box, I have to click somewhere else in order for it to be passed through and displayed eventually. I want to Press Enter Key, and the values should pass/

Secondly, Upon pressing the Enter Key, I want it to clear the field as well, as if its read for next input or whatever, but the main thing is that the Input Text Field should be cleared. How can this be done?


The best way is to handle this with a propertyChange Event:

Right-click your Text Field Input and select “Scripting”.

This will, however, prevent your user from ever supplying an empty string "" as a value.

Thanks! Anyway to get around that as well Cmallone?

Nope. Why would this be a problem? If your goal is to empty the Text Input on “Enter” press, then both an empty string "" and None values will result in the text of the input being set to the empty string, which means that checking against None is impossible. If you need empty string as a possible supplied value, you’ll need to find a different way to go about this.

Ok. My fist goal is to pass through the values upon hitting Enter key. I am using the following code, but it doesnt seem to change anything. can yo assist please?

if event.keyCode == event.VK_ENTER:
value = event.source.text

  1. When you’re supplying code snippets, use three of the back-tick character, a new line, then your code, then a new line, then three more of the back-tick character, or use the </>button in the reply area to generate a code-block. This allows for formatting your code in a much nicer manner.
  2. Why are you using a Key Event? The only reason you should use that is if you ONLY want the value to be read when Enter is pressed, and NOT if the user clicks elsewhere.
  3. It’s not clear what “it doesn’t change anything” means, because your code wouldn’t change anything - you’re only storing a value into a variable. When a user presses Enter, what do you want that code to do? Your current example takes the value of the Text Field’s text property and stores it into a variable named value, but performs no action.

You might be trying to do this (?):

if event.keyCode == event.VK_ENTER:
	event.source.parent.getComponent('Label').text = event.source.text
	event.source.text = ''

This would allow for supplying an empty string as a value, at the expense of requiring that the Enter key be pressed for the value to be supplied - clicking elsewhere or pressing Tab would leave the Text Field with the typed value without using it.


What I want is that when I enter a value, lets say “Abc” in the text input box and press Enter, it should be displayed on the display label, and the text input box shall get cleared.

That’s exactly what the code I supplied does.

if event.keyCode == event.VK_ENTER:
    # set Label text to Text Field value
	event.source.parent.getComponent('Label').text = event.source.text
    # set Text Field to display an empty string
	event.source.text = ''

If what I supplied is NOT doing that, then it might be that you have the supplied code in the wrong area. As you supplied your own code which referenced key events I assumed you were using a keyPressed Event as your trigger, so I placed my code there (and in hindsight didn’t declare that I had moved from the propertyChange Event to a keyPressed Event).


Thank. Now, the Enter key is working fine and values are passing through as they should, just trying to figure out the thing with field getting clear now.

I am using the very same logic to clear the text field, but somehow just not happening.

#Working Fine with Entering Value, but no Screen clearance
if event.keyCode == event.VK_ENTER:
>event.source.parent.getComponent(‘DisplayTag’).text = event.source.text
>event.source.text = ‘’

Could you post a screenshot of your code in the keyPressed Event?

  1. Right-click the Text Field and then select “Scripting”.
  2. Expand the “key” Event Handler directory and select “keyPressed”.
  3. Take a screenshot of this entire dialog.

Could you also post the Window structure from the Project Browser panel?

  1. Locate your Vision Window in the Project Browser.
  2. Scroll Make sure the Project browser panel is tall enough that you can capture the Text Field AND the Label.
  3. Take a screenshot of the Project Browser.
  4. Please include the names of the components you’re trying to use (it looks like DisplayTag is your Label, but I also would like the Text Field’s name.

Do you receive any error messages or dialogs? If so, these would be better at explaining the issue than I could be.

I will get back to you soon Cmall, thanks!