Keyboard input to a variable

This might be the dumbest question on this forum, but I´m pretty new here so please forgive me. But how do I get an variable from a text field. Say I want to create a "Hello " function. Where you input your name in a text field. What would the code look like? Please help me out [import]uid: 93074 topic_id: 20516 reply_id: 320516[/import]

Hey there,

It isn’t a dumb question at all :slight_smile: We’re all new at some point.

Here’s a quick plug and play demo that shows you how to use the name. It prints to the terminal and shows up as text on the screen;

[lua]display.setStatusBar (display.HiddenStatusBar)

local textBox = native.newTextField( 0, 200, 320, 30)

local textBtn = display.newRect( 250, 160, 60, 20 )

local showText = display.newText("", 160, 10, native.systemFont, 12)

local function setText ()
print (“Hello”, textBox.text)
showText.text = ("Hello " …textBox.text… “”)
end
textBtn:addEventListener(“tap”, setText)[/lua]

Let me know how you go with that and if you have any other questions.

Peach :slight_smile: [import]uid: 52491 topic_id: 20516 reply_id: 80559[/import]

Worked great! Thanks for the help :slight_smile:

I didn´t knew I had to have a “execute” button. Is it possible to do without? Like when you hit return on the keyboard on iOS? [import]uid: 93074 topic_id: 20516 reply_id: 80702[/import]

Just another question, why did you add the …"" in the end at line 11? [import]uid: 93074 topic_id: 20516 reply_id: 80706[/import]

Hey there,

Yes - you CAN use the return button on the keyboard; I just put that together as a quick example and it turns out the last time I made an app like this I used a submit button so it was still in my head.

Check this out; http://developer.anscamobile.com/reference/index/nativenewtextfield

The listener section is where you can set something up for the return key.

For line 11, that’s force of habit for me - I’d normally put an “!” after the name or the like, which would go there. If you had no desire for any punctuation or additional text you could simply change that line to;
[lua]showText.text = ("Hello " …textBox.text)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 20516 reply_id: 80782[/import]

Hey, thanks again for your support.

But I´m fairly new to programing is there any where I can understand this functions things better?

-Robin [import]uid: 93074 topic_id: 20516 reply_id: 81082[/import]

There are several tutorials here : www.learningcorona.com

:slight_smile: [import]uid: 84637 topic_id: 20516 reply_id: 81095[/import]

Thanks Danny, had a look. But I think what I need is to find out how to get my head around functions in general. Tried to look at Carlos Webinar but the presentation was way out of sync [import]uid: 93074 topic_id: 20516 reply_id: 81115[/import]

Corona For Newbies has 4 parts and may help in that area in particular; http://techority.com/2011/01/12/corona-for-newbies-part-1/

Peach :slight_smile: [import]uid: 52491 topic_id: 20516 reply_id: 81182[/import]

Peach, thanks for your link to your tutorial, it help a lot. But can you help me out on this code, I having trouble understanding the how to set up the textfield listener:

[code]
display.setStatusBar (display.HiddenStatusBar)

local textBox = native.newTextField( 60, 200, 32, 30, txtList)
textBox.alpha = 1
textBox.rotation = 0
textBox:setTextColor(0, 0, 0 )
local textBox2 = native.newTextField( 0, 250, 320, 30)

local textBtn = display.newRect( 250, 160, 60, 20 )

local showText = display.newText("", 160, 10, native.systemFont, 12)

function setText ()
print (“Hello”, textBox.text, textBox2.text)
showText.text = (“Hello " …textBox.text - textBox2.text …”")

end
textBtn:addEventListener(“tap”, setText)
txtList(“userInput”)
if ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end
[/code] [import]uid: 93074 topic_id: 20516 reply_id: 83415[/import]

That code would throw errors.

Try this;

[lua]display.setStatusBar (display.HiddenStatusBar)

local txtList

local textBox = native.newTextField( 60, 200, 32, 30, txtList)
textBox.alpha = 1
textBox.rotation = 0
textBox:setTextColor(0, 0, 0 )
local textBox2 = native.newTextField( 0, 250, 320, 30)

local textBtn = display.newRect( 250, 160, 60, 20 )

local showText = display.newText("", 160, 10, native.systemFont, 12)

function setText ()
print (“Hello”, textBox.text, textBox2.text)
showText.text = (“Hello " …textBox.text… “-” …textBox2.text …”")

end
textBtn:addEventListener(“tap”, setText)

txtList = function (event)
if ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end[/lua]

Also note I changed line 17 - I don’t know what your goal was there but;

[lua]showText.text = (“Hello " …textBox.text - textBox2.text …”")[/lua]

That is trying to subtract one text from the other - that wont work because they aren’t numbers, they’re strings.

Were you trying to subtract or were you trying to print with a “-” sign? That’s what I’ve edited it to do but let me know.

Peach :slight_smile: [import]uid: 52491 topic_id: 20516 reply_id: 83488[/import]

Thanks again for your help. But when I tested the code i Xcode Simulator I could´t get it to hide the keyboard when pushing enter. However I found that I i use

local function txtList( event )  
 if ( "submitted" == event.phase ) then  
 native.setKeyboardFocus( nil )  
 elseif ( "ended" == event.phase ) then  
 native.setKeyboardFocus( nil )  
 end  
end  

Before the text box(line 5 in your reply) it would hide the keyboard. Why is that? Is not the order of when the function is stated insignificant?

Also I thought the “ended” event should close the keyboard if you pressed outside the keyboard, but that didn´t happened. Is it only if I push an other textfield?

Also I had
local function txtList( event )
instead of
txtList = function (event)
Is there any difference between those two? [import]uid: 93074 topic_id: 20516 reply_id: 83614[/import]

The difference is a forward declaration - but that code above I didn’t test so if you needed to tweak it to get it working that is par the course :wink:

RE the ended phase, these fields use “submitted” rather than “ended”.

You could add a listener to the background to close the keyboard if it is open when the background was touched though. (Then it would close if you clicked anywhere outside of it.)

Peach :slight_smile: [import]uid: 52491 topic_id: 20516 reply_id: 83709[/import]