Text area - inhibit enter key

Is there any way to stop the user pressing the Enter key to get a new line in the Text area control?

I was thinking along the lines of detecting when the user pressed the enter key in the Text area’s propertyChange event handler and setting the text property equal to its current value minus the last character, but I can’t get this to work. The propertyChange code I was trying was as follows:

if event.propertyName=="text": if event.source.text[-1:]=="\n": def remove_enter(control=event.source): control.text=control.text[:-1] system.util.invokeLater(remove_enter)

Mine doesn’t work either, but it eliminates another method.


if len(event.source.text)> 0 and ord(event.source.text[-1])==10:
	newstring=event.source.parent.getComponent('Text Area').text[:-1]
	def remove_enter(stringout):
		import system
		window=system.gui.getWindow("Capture Enter") # The name of the window this is residing in
		component=window.rootContainer.getComponent("Text Area")
		component.text=stringout
	system.gui.invokeLater(remove_enter(newstring))

No matter what I do, I get an Attempt to mutate in notification error. Apparently, the document isn’t allowed to write to itself. I assume you’re getting the same error?

While you have focus in the Text Area component it won’t let you write to the text property. You can only do it if you turn off the ‘Defer Updates’ property, but then any time someone hits the enter key it will move the cursor back to the first position…

This works for me:if event.propertyName=="text": if event.source.text[-1:]=="\n": def remove_enter(control=event.source): control.text=control.text[:-1] system.util.invokeLater(remove_enter)