Why does native.setKeyboardFocus( nil ) never dismiss the keyboard for me?

My understanding is that the following…

-- TextField Listener  
 local function fieldHandler( getObj )  
  
 return function( event )  
  
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event is called when the user stops editing a field:  
 -- for example, when they touch a different field or keyboard focus goes away  
  
 comment.text = tostring( getObj().text )  
 native.setKeyboardFocus( nil ) --nothing happens?  
  
 elseif ( "ended" == event.phase ) then  
  
 end  
 end   
 end  
  
  
 inputTextField = native.newTextField(322, 1160, \_W \* 0.68, \_W \* 0.28)   
 inputTextField:addEventListener("userInput", fieldHandler)  
 native.setKeyboardFocus( inputTextField )  

…should close the keyboard when the user presses “return”.
This has NEVER worked for me on iOS or Android, is there a require or something that I may have missed?

Everything else in the listener works, such as retrieving the text from textfield in the ended phase. The only way I can dismiss the keyboard on Android is to press the back key, and on iOS I can only get rid of it by touching elsewhere.

Have I missed something? [import]uid: 84115 topic_id: 32775 reply_id: 332775[/import]

Don’t you need to specify the parameter for fieldHandler as “event”, otherwise I don’t think it is defined anywhere. So:

[code]
local function fieldhandler(event)

end

[/code] [import]uid: 94868 topic_id: 32775 reply_id: 130333[/import]

Don’t you need to specify the parameter for fieldHandler as “event”, otherwise I don’t think it is defined anywhere. So:

[code]
local function fieldhandler(event)

end

[/code] [import]uid: 94868 topic_id: 32775 reply_id: 130333[/import]

Thank you, I had a feeling I had missed something simple. I’ve just given it a quick test on Android, and the return key has changed into the dismiss keyboard key. I presume it will work on iOS as well, I’ll test that later.

Thanks again. [import]uid: 84115 topic_id: 32775 reply_id: 130416[/import]

Thank you, I had a feeling I had missed something simple. I’ve just given it a quick test on Android, and the return key has changed into the dismiss keyboard key. I presume it will work on iOS as well, I’ll test that later.

Thanks again. [import]uid: 84115 topic_id: 32775 reply_id: 130416[/import]

I left this for a few days, and now that I’ve gone back to it I realise that changing the parameter to event still wasn’t enough to fix it.

I did however find the answer. My code from above was using an old version of the textfield handler (which I should add is STILL being shown in the docs). I no longer need the listener to return a function, instead it should just carry out the if statements.

local function fieldHandler( event )  
  
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event is called when the user stops editing a field:  
 -- for example, when they touch a different field or keyboard focus goes away  
  
 removeTextField()--my own function to remove text objects  
 native.setKeyboardFocus( nil )  
  
 elseif ( "ended" == event.phase ) then  
 comment.text = tostring( event.text )  
 native.setKeyboardFocus( nil )   
 end  
 end  

Now the return key closes the keyboard, removes the textfield and uses the text from it on a newText object.

Thanks Screaming Leaf, I may not have seen the problem if you hadn’t pointed the “event” param out to me. [import]uid: 84115 topic_id: 32775 reply_id: 130928[/import]

I left this for a few days, and now that I’ve gone back to it I realise that changing the parameter to event still wasn’t enough to fix it.

I did however find the answer. My code from above was using an old version of the textfield handler (which I should add is STILL being shown in the docs). I no longer need the listener to return a function, instead it should just carry out the if statements.

local function fieldHandler( event )  
  
 if ( "began" == event.phase ) then  
 -- This is the "keyboard has appeared" event  
  
 elseif ( "submitted" == event.phase ) then  
 -- This event is called when the user stops editing a field:  
 -- for example, when they touch a different field or keyboard focus goes away  
  
 removeTextField()--my own function to remove text objects  
 native.setKeyboardFocus( nil )  
  
 elseif ( "ended" == event.phase ) then  
 comment.text = tostring( event.text )  
 native.setKeyboardFocus( nil )   
 end  
 end  

Now the return key closes the keyboard, removes the textfield and uses the text from it on a newText object.

Thanks Screaming Leaf, I may not have seen the problem if you hadn’t pointed the “event” param out to me. [import]uid: 84115 topic_id: 32775 reply_id: 130928[/import]