Button to insert text?

I’m trying to implement a button to insert some text into a textbox.

I’ve been trying to use events to call a function on a textbox when the button is pressed.

In the following code, the call to the event does nothing. If I move the call outside of the handleButtonEvent it gets called.

Why won’t the dispatchEvent call work inside of this function?

I think I need to use the event on the textbox to get the current cursor position to enter the text I want to add. For example, let’s say I’m editing the textBox and I want to hit a button to enter the “foo”. I’m trying to invoke the event on the textbox manually so I can do the string insertion.

Maybe there’s another way to do it?

function scene:createScene( event ) local group = self.view -- create a white background to fill screen local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) bg:setFillColor( 0 ) -- black local function inputListener( event ) print ("event name = " .. event.name) if event.type == "myEvent" then print ( "insert string" ) -- print( "start position = " .. event.startPosition ) end end local textBox = native.newTextBox( 200, 200, 280, 140 ) textBox.text = "This is line 1.\nAnd this is line2" textBox.isEditable = true textBox:addEventListener( "userInput", inputListener ) textBox:addEventListener("myEvent", inputListener) local event = { name="myEvent", target=textBox } textBox:dispatchEvent (event) -- this one works, but it's outside of the button event handler -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) textBox:dispatchEvent (event) -- doesn't work here end end -- Create the widget local button1 = widget.newButton { left = 100, top = 100, id = "button1", label = "Default", onEvent = handleButtonEvent } -- all objects must be added to group (e.g. self.view) group:insert( bg ) group:insert ( button1 ) group:insert ( textBox ) end

in your handleButtonEvent when you dispatch the event it’s dispatching the button event not the event you created. to correct rename your event to something else then dispatch it
the word event is just a variable it could be anything

That solved the problem of the dispatch - thanks!

However, the desired behavior is not what I wanted. I’m trying to insert some text at the location of the cursor in the textBox. It seems as if the startPosition variable is not present.

Any ideas?

-- this windws up with a nil value for event.startPosition local function inputListener( event ) print ("event name = " .. event.name) if event.name == "myEvent" then print ( "insert string" ) print( "start position = " .. event.startPosition ) end end local textBox = native.newTextBox( 200, 200, 280, 140 ) textBox.text = "This is line 1.\nAnd this is line2" textBox.isEditable = true textBox:addEventListener( "userInput", inputListener ) textBox:addEventListener("myEvent", inputListener) local myEvent = { name="myEvent", target=textBox } -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) textBox:dispatchEvent (myEvent) end end

use textBox.text … what ever string you want to add here

textBox.text will give you the current text in the box and … means to add to

But I’d like to add the text at the current cursor position in the textBox. Otherwise, I’d only be able to add text at the end (which I guess could be OK).

Any ideas if there is a current cursor position property on textBox? I looked and couldnt find one.

it’s returning nil cause your custom event doesn’t have a startPosition event
perhaps if I saw some screenshots of your app I would better understand what your trying to achieve

what’s the purpose of the button. normally the user would just touch the textBox to edit

currently the way your code is setup if the user touches the box you can then read the events once they touch the button those events are replaced by your custom event events which only have a name and target events.

All I’m trying to do is have a button insert text into a textBox at the current cursor position.

I was trying to have the button dispatch an event in the textBox. It does look like it’s calling the event, but it’s not bringing with it the attributes of the textBox.

Not sure I followed your last response. (BTW, I really appreciate your help!)

your welcome
try setting up a variable so when the field has focus you save the position then using the string lib. split the text at position and insert button text when pressed

Good idea, but the text position doesn’t get updated when the cursor moves. It only get’s updated when more text is inserted.

The value of the textBox’s startposition stays the same until additional text is added. Moving around with the cursor doesn’t continually update the startposition.

Might have to handle this one character at a time and process cursor moves and input. Seems like an overly complicated what to do it.

Any other suggestions?

in your handleButtonEvent when you dispatch the event it’s dispatching the button event not the event you created. to correct rename your event to something else then dispatch it
the word event is just a variable it could be anything

That solved the problem of the dispatch - thanks!

However, the desired behavior is not what I wanted. I’m trying to insert some text at the location of the cursor in the textBox. It seems as if the startPosition variable is not present.

Any ideas?

-- this windws up with a nil value for event.startPosition local function inputListener( event ) print ("event name = " .. event.name) if event.name == "myEvent" then print ( "insert string" ) print( "start position = " .. event.startPosition ) end end local textBox = native.newTextBox( 200, 200, 280, 140 ) textBox.text = "This is line 1.\nAnd this is line2" textBox.isEditable = true textBox:addEventListener( "userInput", inputListener ) textBox:addEventListener("myEvent", inputListener) local myEvent = { name="myEvent", target=textBox } -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) textBox:dispatchEvent (myEvent) end end

use textBox.text … what ever string you want to add here

textBox.text will give you the current text in the box and … means to add to

But I’d like to add the text at the current cursor position in the textBox. Otherwise, I’d only be able to add text at the end (which I guess could be OK).

Any ideas if there is a current cursor position property on textBox? I looked and couldnt find one.

it’s returning nil cause your custom event doesn’t have a startPosition event
perhaps if I saw some screenshots of your app I would better understand what your trying to achieve

what’s the purpose of the button. normally the user would just touch the textBox to edit

currently the way your code is setup if the user touches the box you can then read the events once they touch the button those events are replaced by your custom event events which only have a name and target events.

All I’m trying to do is have a button insert text into a textBox at the current cursor position.

I was trying to have the button dispatch an event in the textBox. It does look like it’s calling the event, but it’s not bringing with it the attributes of the textBox.

Not sure I followed your last response. (BTW, I really appreciate your help!)

your welcome
try setting up a variable so when the field has focus you save the position then using the string lib. split the text at position and insert button text when pressed