Detecting Escape in TextFields

I can only see how to detect that a field was submitted. How do I detect if the user presses Back/Escape so I can close the TextField?

Here’s the API documentation:

https://docs.coronalabs.com/api/library/native/newTextField.html

Have you created a text field with a listener and dumped out all events and text values as they are entered to see what text values and events escape triggers?

local defaultField local function textListener( event ) print ("Event ------------------------------- @ ", system.getTimer()) for k,v in pairs(event) do print(k,v) end print ("----------------------------------------") end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )

Use the above code, then run it:

  • In OS X simulator and look at console for output
  • on iOS Device attached to computer via USB/Lightning Cable and look at xCode device console for attached console.
  • on Android device and use logcat to watch output.

If someone else want to help appuciani here by doing the above and posting back that would be cool too.  I can’t run the test right now unfortunately.

PS - What is ‘back’  is that the back button on and android device or the backspace key on a keyboard?  I’m assuming this is for mobile and the ‘back’ button on android.

If that is true, the key listener doesn’t catch back although it may get an ended or other event.

To catch the android ‘back’ button press, use a ‘key’ listener something like this:

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if( keyName == "back" and phase == "down" ) then print("Got back button, do something about it here.") end return false end Runtime:addEventListener( "key", onKeyEvent );

Hi, thanks for responding.

Yes, as far as I can tell, no event is sent at all when the escape key is pressed. I do hear a Windows chime sound playing over my speakers, indicating that the button is essentially being rejected.

As far as I can tell, Corona does not support detecting the escape key, only the Enter key.

While text fields work in Windows now, you should test this on device to see what you actually get.  There may be variations between the OSes (Windows, OS X, Android, iOS) because you’re using a native element.  

Corona’s staff/engineers have done their best to give you a single experience (for the purpose of development), but they can’t do anything about variations.

(I realize this is frustrating as it would be nicer to simply be able to look this up.  That said, once you figure this out, please post back your findings and how you figured it out here.  Don’t forget, the forums are for getting answers and helping others in the future with the resolution to your question.)

Have you created a text field with a listener and dumped out all events and text values as they are entered to see what text values and events escape triggers?

local defaultField local function textListener( event ) print ("Event ------------------------------- @ ", system.getTimer()) for k,v in pairs(event) do print(k,v) end print ("----------------------------------------") end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField:addEventListener( "userInput", textListener )

Use the above code, then run it:

  • In OS X simulator and look at console for output
  • on iOS Device attached to computer via USB/Lightning Cable and look at xCode device console for attached console.
  • on Android device and use logcat to watch output.

If someone else want to help appuciani here by doing the above and posting back that would be cool too.  I can’t run the test right now unfortunately.

PS - What is ‘back’  is that the back button on and android device or the backspace key on a keyboard?  I’m assuming this is for mobile and the ‘back’ button on android.

If that is true, the key listener doesn’t catch back although it may get an ended or other event.

To catch the android ‘back’ button press, use a ‘key’ listener something like this:

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if( keyName == "back" and phase == "down" ) then print("Got back button, do something about it here.") end return false end Runtime:addEventListener( "key", onKeyEvent );

Hi, thanks for responding.

Yes, as far as I can tell, no event is sent at all when the escape key is pressed. I do hear a Windows chime sound playing over my speakers, indicating that the button is essentially being rejected.

As far as I can tell, Corona does not support detecting the escape key, only the Enter key.

While text fields work in Windows now, you should test this on device to see what you actually get.  There may be variations between the OSes (Windows, OS X, Android, iOS) because you’re using a native element.  

Corona’s staff/engineers have done their best to give you a single experience (for the purpose of development), but they can’t do anything about variations.

(I realize this is frustrating as it would be nicer to simply be able to look this up.  That said, once you figure this out, please post back your findings and how you figured it out here.  Don’t forget, the forums are for getting answers and helping others in the future with the resolution to your question.)