Search code examples
rokubrightscriptscenegraph

Trigger Roku back action


Is there a way to trigger a roku back action via BrightScript ?

I want to pop the current screen off the navigation stack, and show the previous screen.

I need this for a couple reason :

1.) A back button will be present on screen, that the user can navigate to and press. Pressing this button should trigger a back action similar to a pressing the back button on the remote. (Think of a browsers window.history.back())

2.) I want to trigger this back action when certain callback events are called.

I currently use the following task to navigate :

function init()
    ? "[NavigationTask] init()"
    m.top.functionName = "gotoScreen"
end function

sub gotoScreen()
    if m.top.sceneName <> invalid then
        handleNav(m.top.sceneName)
    else
        print "[NavigationTask] sceneName not specified"
    end if
end sub

sub handleNav(sceneName as String)
    print "[NavigationTask] handleNav"

    screen = CreateObject("roSGScreen")
    m.port = createObject("roMessagePort")
    screen.SetMessagePort(m.port)

    if screen <> invalid
        scene = screen.CreateScene(sceneName)
        screen.Show()

        while(true)
            msg = wait(0, m.port)
            msgType = type(msg)
            print "[NavigationTask] msg : "; msgType" scene="sceneName 
            if msgType = "roSGScreenEvent"
                if msg.isScreenClosed() then return
            end if
        end while
    else 
        print "[NavigationTask] Invalid roSGScreen"
    end if   
end sub

Task is called like so :

   m.NavigationTask   = createObject("roSGNode","NavigationTask")
   m.NavigationTask.sceneName = videoScreen()
   m.NavigationTask.control  ="RUN"

Solution

  • I wouldn't recomend handling navigation in a task thread because of Thread Rendezvous, and also you could just have one roSGScreen and one Scene and have Groups/Rectangles act as your app "screens". This would be a way more performant solution.

    But anyways, back to your question, you can simulate a back press programmatically by using the External Control API.

    ecpRequest = CreateObject("roUrlTransfer")
    url = "http://" + roku_ip + ":8060/keypress/back"
    ecpRequest.setURL(url)
    ecpRequest.postFromString("")
    

    You can obtain the roku ip address from roDeviceInfo.