native.newTextBox - detect "enter" key (or prevent newline)

Dear Corona community,

I am working with a textbox and I’d like to make the “enter” key close the keyboard instead of writing a newline. Unfortunately event.phase == “submitted” only works for textfields and event.keyName always returns nil.

Is there any way to detect the “enter” key in a textbox, or any other chance to prevent the creation of a newline when “enter” key is pressed?

I know I can create an additional button above the textbox to native.setKeyboardFocus(nil), but I am looking for a solution without any additional buttons.

Thank you

found something that might work:

In the “editing” event.phase, I compare the last character with a newline character:

if(string.sub(event.text, string.len(event.text), string.len(event.text)) == “\n”) then
       native.setKeyboardFocus(nil)
end

Should this do the trick on all devices, or is “\n” something device-specific?

Thanks

// edit: Argh, this works only if user presses “enter” as last character. When editing within the text, it of course does not get detected :confused:

// 2nd edit: This works:

if(string.find(event.text, “\n”)) then
      event.target.text = string.gsub(event.text, “\n”, “”)
      native.setKeyboardFocus(nil)
 end

However, on devices you can see that the newline gets printed, before removed again. So, not very beautiful. I’d appreciate any other hints on how to do that :slight_smile:

found something that might work:

In the “editing” event.phase, I compare the last character with a newline character:

if(string.sub(event.text, string.len(event.text), string.len(event.text)) == “\n”) then
       native.setKeyboardFocus(nil)
end

Should this do the trick on all devices, or is “\n” something device-specific?

Thanks

// edit: Argh, this works only if user presses “enter” as last character. When editing within the text, it of course does not get detected :confused:

// 2nd edit: This works:

if(string.find(event.text, “\n”)) then
      event.target.text = string.gsub(event.text, “\n”, “”)
      native.setKeyboardFocus(nil)
 end

However, on devices you can see that the newline gets printed, before removed again. So, not very beautiful. I’d appreciate any other hints on how to do that :slight_smile: