Help with string.upper()

When a user inputs text into my textField, it’s always lowercase letters. I have the keyboard appearing when they need to type, but is there a way to make the keyboard appear but the Shift key “pressed” already?

If not, can I use string.upper() to only make the first letter capitalized?

Thanks

In your keyboard handling function on each key input you can upper case the text field.

local function fieldHandler( textField )         return function( event )                 if ( "began" == event.phase ) then                         -- This is the "keyboard has appeared" event                         -- In some cases you may want to adjust the interface when the keyboard appears.                                  elseif ( "ended" == event.phase ) then                         -- This event is called when the user stops editing a field: for example, when they touch a different field                                          elseif ( "editing" == event.phase ) then                         textField().text = string.upper(textField().text)                 elseif ( "submitted" == event.phase ) then                         -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard                         print( textField().text )                                                  -- Hide keyboard                         native.setKeyboardFocus( nil )                 end         end end

Or something like that.

Rob

Thanks for the reply. But what if I want only the first letter capitalized? I ask because when the keyboard appears on screen, the Shift key isn’t “pressed” (like in other applications) and the letters are all written lowercase. The user of course can press the Shift key, but I don’t want it to be an inconvenience. 

Would you say string.upper is the correct api to use? I’m thinking maybe we can combine it with character classes (%a)? 

Or easier yet, if there is a way to have the shift key be depressed when the keyboard appears.

Thanks

You could google around for lua string recipes and see if there are any functions other people have written. If not you could combine the substring function to grab the first character, upper it, append the rest of the string as entered.

Rob

I appreciate the help. Here’s what I have so far:

[lua]

firstLetter, allOthers = string.match( event.target.text, “(%a)(%w+)”) ---------> assuming .text is “word”

print ("First Letter equals "…firstLetter) --------> shows “w”

print ("AllOthers equals "… allOthers)   --------> shows “ord”

–string.upper(firstLetter)

string.upper(tostring (firstLetter))         

print ("First Letter equals "…firstLetter) --------> still shows lowercase; “w”

print (firstLetter…allOthers)                    --------> shows “word”

[/lua]

Any insight on this? I was thinking by making it into a string it should work 

I was thinking more:

text = string.upper( string.sub( text, 1, 1 ) )  … string.sub( text, 2 )

Absolutely perfect, thanks Rob 

In your keyboard handling function on each key input you can upper case the text field.

local function fieldHandler( textField )         return function( event )                 if ( "began" == event.phase ) then                         -- This is the "keyboard has appeared" event                         -- In some cases you may want to adjust the interface when the keyboard appears.                                  elseif ( "ended" == event.phase ) then                         -- This event is called when the user stops editing a field: for example, when they touch a different field                                          elseif ( "editing" == event.phase ) then                         textField().text = string.upper(textField().text)                 elseif ( "submitted" == event.phase ) then                         -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard                         print( textField().text )                                                  -- Hide keyboard                         native.setKeyboardFocus( nil )                 end         end end

Or something like that.

Rob

Thanks for the reply. But what if I want only the first letter capitalized? I ask because when the keyboard appears on screen, the Shift key isn’t “pressed” (like in other applications) and the letters are all written lowercase. The user of course can press the Shift key, but I don’t want it to be an inconvenience. 

Would you say string.upper is the correct api to use? I’m thinking maybe we can combine it with character classes (%a)? 

Or easier yet, if there is a way to have the shift key be depressed when the keyboard appears.

Thanks

You could google around for lua string recipes and see if there are any functions other people have written. If not you could combine the substring function to grab the first character, upper it, append the rest of the string as entered.

Rob

I appreciate the help. Here’s what I have so far:

[lua]

firstLetter, allOthers = string.match( event.target.text, “(%a)(%w+)”) ---------> assuming .text is “word”

print ("First Letter equals "…firstLetter) --------> shows “w”

print ("AllOthers equals "… allOthers)   --------> shows “ord”

–string.upper(firstLetter)

string.upper(tostring (firstLetter))         

print ("First Letter equals "…firstLetter) --------> still shows lowercase; “w”

print (firstLetter…allOthers)                    --------> shows “word”

[/lua]

Any insight on this? I was thinking by making it into a string it should work 

I was thinking more:

text = string.upper( string.sub( text, 1, 1 ) )  … string.sub( text, 2 )

Absolutely perfect, thanks Rob