1

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.

MayurK
  • 1,925
  • 14
  • 27

1 Answers1

1

Global node is shared between nodes of one Rosgscreen, if you screen becomes closed, you can't access it's fields. So you have to observe keyboards text and update you local variable myurl each time text is entered.

  • Thank you @Roman Parashchyn! Here my observation is that the main thread will be waiting at "msg = wait(0, m.textPort)". For now I changed the timeout value to 100ms and copying text to my local variable. Is there any better solution for this problem? – MayurK Jul 08 '16 at 11:01
  • M.global.observefield("fieldname", port) will generate event each time this field changes. You will receive rosgnodeevent where you can read proper value – Roman Parashchyn Jul 09 '16 at 15:08
  • Hi @Roman Parashchyn, I added M.global.observefield("text", m.textPort) in main.brs but it did not work. But "text" is a field in keyboard component right? Is it fine if I just put "text" in observefield()? – MayurK Jul 11 '16 at 05:42
  • Did you check if m.global.mytext was really created and has any value? Also from your description you used m.global.mytextport, ports could not be stored in rosgnodes, check if it's valid. – Roman Parashchyn Jul 11 '16 at 05:57
  • Also, try to print all none invalid messages from port – Roman Parashchyn Jul 11 '16 at 05:58
  • (1) Now I am not using m.global.mytextport. I have put M.global.observefield("text", m.textPort) inside Sub getTextFromUser(). (2) I gave 100ms timeout in wait() and printed all the messages. I am getting invalid message prints. So port should be valid. – MayurK Jul 11 '16 at 06:05
  • Could you please send your while loop – Roman Parashchyn Jul 11 '16 at 06:06
  • while(true) msg = wait(0, m.textPort) msgType = type(msg) print "Got message: ";msg if msgType = "roSGScreenEvent" if msg.isScreenClosed() return end if end if end while – MayurK Jul 11 '16 at 06:10
  • Try to move all the stuff with creating and observing global node after show() is called – Roman Parashchyn Jul 11 '16 at 06:11
  • OK.. Can you answer this question? http://stackoverflow.com/questions/38300097/roku-reading-a-text-file-present-in-server This will also help.. – MayurK Jul 11 '16 at 06:17