I am writing an application for Roku in which I need to enter some text using keyboard and get the text in main.brs
I started with KeyboardExample given here: https://sdkdocs.roku.com/display/sdkdoc/Widgets+Markup (Keyboard Markup).
To pass the text to main.brs, I used global node of the roSGScreen as given in the below link. https://sdkdocs.roku.com/display/sdkdoc/Scene+Graph+Data+Scoping (Global Scope).
The following code is present in my main.brs
Sub getTextFromUser()
screen = CreateObject("roSGScreen")
m.textPort = CreateObject("roMessagePort")
screen.setMessagePort(m.textPort)
m.global = screen.getGlobalNode()
m.global.id = "GlobalNode"
m.global.addFields( {myText: "Not initialized"} )
scene = screen.CreateScene("KeyboardExample")
screen.show()
while(true)
msg = wait(0, m.textPort)
msgType = type(msg)
if msgType = "roSGScreenEvent"
if msg.isScreenClosed()
myURL = m.global.myText
return
end if
end if
end while
End Sub
I have added the following code in keyboard.xml
function onKeyEvent(key as String, press as Boolean) as Boolean
keyboard = m.top.findNode("exampleKeyboard")
if keyboard.text <> invalid
m.global.myText= keyboard.text
end if
return false
end function
I am copying m.global.myText to a local variable when I receive roSGScreenEvent.isScreenClosed event.
The issue is when roSGScreenEvent.isScreenClosed is receved m.global.myText becomes invalid.
Can someone tell me what is going wrong here?
UPDATE I read about the roSGNode.ObserveField in the following link. https://sdkdocs.roku.com/display/sdkdoc/Handling+Node+Field+Changes
I added an observer for "text" field of the Keyboard as below.
keyboard.ObserveField("text", "changetext")
and I was getting changetext() callback whenever a key was pressed. But this method had the same issue.
According to the above link, an roMessagePort object can be passed to observer instread of function pointer. So I passed textPort to keyboard.xml using global variable and passed it to observer as below.
keyboard.ObserveField("text", m.global.myTextPort)
My expectation was to get roSGNodeEvent at textPort whenever a key was pressed. But I am not getting any events.