如何简化和修复 Roku 场景的 BrightScript 代码?

问题描述 投票:0回答:1

我正在开发一个 Roku SceneGraph 项目,并尝试创建一个类似浏览器的基本功能。重点是让用户在 TextEditBox 中输入 URL 并使用 Button 导航到该 URL。我最初包含了一个鼠标指针,但我想简化实现并删除所有与鼠标相关的功能。我的目标是纯粹专注于:

处理远程控制按键事件(选择激活按钮,选择上/下进行焦点导航)。 单击按钮时验证并导航到输入的 URL。 这是我当前的 BrightScript (BrowserScene.brs) 代码:

sub init()
    ' Find UI components
    m.urlInput = m.top.findNode("urlInput")
    m.goButton = m.top.findNode("goButton")

    ' Ensure nodes exist
    if m.urlInput = invalid then
        print "Error: urlInput node is missing from XML."
        return
    end if
    if m.goButton = invalid then
        print "Error: goButton node is missing from XML."
        return
    end if

    ' Observe key events
    m.top.observeField("keyEvent", "onKeyEvent")

    ' Set focus to the URL input field
    m.urlInput.setFocus(true)
end sub

sub onKeyEvent(event as object)
    if type(event) = "roSGNodeEvent" and event.isKeyEvent()
        key = event.getKey()
        press = event.getInt()

        if press = 1 ' Key down event
            if key = "select" then
                handleButtonClick()
            end if
        end if
    end if
end sub

sub handleButtonClick()
    if m.goButton.isInFocusChain()
        onGoPressed()
    else if m.urlInput.isInFocusChain()
        m.urlInput.setFocus(true)
    end if
end sub

sub onGoPressed()
    url = m.urlInput.text
    if url <> invalid and url.trim() <> "" then
        if not url.startsWith("http://") and not url.startsWith("https://")
            url = "https://" + url
        end if
        print "Navigating to: " + url
        ' Add navigation logic here
    else
        print "Error: Please enter a valid URL."
    end if
end sub



从代码中删除了所有与鼠标相关的逻辑。 专注于管理我的 XML 文件中定义的 urlInput (TextEditBox) 和 goButton (Button) 组件。

<component name="BrowserScene" extends="Scene">
    <script type="text/brightscript" uri="BrowserScene.brs" />

    <children>
        <TextEditBox
            id="urlInput"
            hintText="Enter a URL"
            translation="[50, 120]"
            width="600"
            height="50"
            focusable="true" />

        <Button
            id="goButton"
            text="Go"
            translation="[660, 120]"
            width="100"
            height="50"
            focusable="true" />
    </children>
</component>

我遇到的问题是

Install Failure: Compilation Failed.  pkg:/source/BrowserScene.brs Syntax Error.  (compile error &h02) in pkg:/source/BrowserScene.brs(line numbers) 

roku
1个回答
0
投票

您不应该自己观察

keypress
,它会在幕后触发。如果您将
onKeyEvent
函数放入具有下一个签名的场景/UI 组件中,您将收到按键事件:

function onKeyEvent(_key_ as String, _press_ as Boolean) as Boolean  
    if (press)
        if (key = "play")
            ' put your logic here
        end if
    end if
end function

来源:https://developer.roku.com/docs/references/scenegraph/component-functions/onkeyevent.md

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.