native.newTextField phase doesnt go to 'Ended' or 'Submitted' when clicking out of it.

hello all,

I’m using native.newTextField at the bottom of the screen to let the user enter his/her name and save it to a score file. When the field is clicked I move the field to the top so it sits above the virtual keyboard of a phone/tablet.

Now when the field is clicked the event.phase is ‘begin’ . when a name is typed in the event.phase is ‘edifting’ (for each character.

if the user hits <Enter> it then goes to a phase of ‘submitted’ (sometimes ‘ended’) which allows me to move the field back to the bottom of the screen.

However if the user simply clicks out of the native.newTextField it doesnt go into a phase of ‘ended’ or ‘submitted’

Even if I click on another button, the field is still up there awaiting a <Enter>.

Is there a proper way for the phase to go to ‘ended’ when clicking out of it ? 
I can move the field back down in all other button/pictures/icon EventListeners but it’s pretty tedious !

thanks !

12:04:55.994  here is lncurrentUserField> fr <> fr <> began

12:04:57.518  here is lncurrentUserField> fre <> fr <> editing

12:04:57.667  here is lncurrentUserField> fred <> fr <> editing

12:05:05.317  here is lncurrentUserField> fred <> fr <> submitted        --<< only when hit <Enter>

function lncurrentUserField(event) &nbsp; &nbsp; &nbsp; print ("here is lncurrentUserField\>",currentUserField.text,"\<\>",glob.currentUserSaved,"\<\>",event.phase) &nbsp; &nbsp; &nbsp; if ( event.phase == "began" and string.upper(currentUserField.text) == "ENTER YOUR NAME") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUserField.text = "" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUserField.y = screenCenY - 50 &nbsp; &nbsp; &nbsp; elseif ( event.phase == "began" and string.upper(currentUserField.text) ~= "ENTER YOUR NAME" ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUserField.y = screenCenY - 50 &nbsp; &nbsp; &nbsp; elseif ( event.phase == "ended" or event.phase == "submitted" ) then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --\<\< it comes here if hit \<Enter\> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fnmoveCurrentUserFieldDown()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- but not if click outside &nbsp; &nbsp; &nbsp; end end currentUserField = native.newTextField( screenCenX , screenBot -20 , 200, 20 ) currentUserField:addEventListener( "userInput", lncurrentUserField )

Try adding an invisible background to initiate the “ended” phase, something like this:

[lua]

local cX, cY = display.contentCenterX, display.contentCenterY

local bg = display.newRect(cX, cY, display.actualContentWidth, display.actualContentHeight)
bg:setFillColor(0,0,0,0)
bg.isHitTestable = true

bg:addEventListener(“touch”, function() native.setKeyboardFocus(nil) end)


local y = cY - 150

local enterYourName = “ENTER YOUR NAME”

local currentUserField = native.newTextField(cX , y , 300, 40)
currentUserField.text = enterYourName


local function lncurrentUserField(event)
    local p = event.phase

    if p==“began” then
        if string.upper(currentUserField.text)==enterYourName then
            currentUserField.text = “”
        end
        currentUserField.y = y - 50

    elseif p==“ended” or p==“submitted” then
        native.setKeyboardFocus(nil)
        currentUserField.y = y
    end
end

currentUserField:addEventListener( “userInput”, lncurrentUserField )

[/lua]

Does this work?

Dave

native.setKeyboardFocus(nil) works a treat. 

And I don’t even need an extra invisible background as … I have a background ! So i just added a listener to it.

I’m just surprised we have to do this, that Corona doesn’t notice that the focus is nolonger on the field … 

but it’s working well, thank you !

Try adding an invisible background to initiate the “ended” phase, something like this:

[lua]

local cX, cY = display.contentCenterX, display.contentCenterY

local bg = display.newRect(cX, cY, display.actualContentWidth, display.actualContentHeight)
bg:setFillColor(0,0,0,0)
bg.isHitTestable = true

bg:addEventListener(“touch”, function() native.setKeyboardFocus(nil) end)


local y = cY - 150

local enterYourName = “ENTER YOUR NAME”

local currentUserField = native.newTextField(cX , y , 300, 40)
currentUserField.text = enterYourName


local function lncurrentUserField(event)
    local p = event.phase

    if p==“began” then
        if string.upper(currentUserField.text)==enterYourName then
            currentUserField.text = “”
        end
        currentUserField.y = y - 50

    elseif p==“ended” or p==“submitted” then
        native.setKeyboardFocus(nil)
        currentUserField.y = y
    end
end

currentUserField:addEventListener( “userInput”, lncurrentUserField )

[/lua]

Does this work?

Dave

native.setKeyboardFocus(nil) works a treat. 

And I don’t even need an extra invisible background as … I have a background ! So i just added a listener to it.

I’m just surprised we have to do this, that Corona doesn’t notice that the focus is nolonger on the field … 

but it’s working well, thank you !