Create focusable element in roku
- User
Create focusable element in roku
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create focusable element in roku
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's how to create focusable elements in Roku using BrightScript:
To make an element focusable in Roku, you need to set the focusable property to true:
' In your XML component
<Rectangle
id="myButton"
width="200"
height="80"
color="0x1C1C1CFF"
focusable="true"
/>
Here's a complete example of a focusable button component:
FocusableButton.xml:
<?xml version="1.0" encoding="utf-8"?>
<component name="FocusableButton" extends="Group">
<interface>
<field id="text" type="string" value="Button" />
<field id="buttonSelected" type="boolean" value="false" />
</interface>
<children>
<Rectangle
id="buttonBackground"
width="300"
height="60"
color="0x333333FF"
/>
<Label
id="buttonLabel"
width="300"
height="60"
horizAlign="center"
vertAlign="center"
font="font:MediumBoldSystemFont"
/>
</children>
<script type="text/brightscript" uri="FocusableButton.brs" />
</component>
FocusableButton.brs:
sub init()
m.buttonBackground = m.top.findNode("buttonBackground")
m.buttonLabel = m.top.findNode("buttonLabel")
' Make the component focusable
m.top.focusable = true
' Set up observers
m.top.observeField("focusedChild", "onFocusChange")
m.top.observeField("text", "onTextChange")
' Initial setup
onTextChange()
end sub
sub onFocusChange()
if m.top.hasFocus()
' Element has focus
m.buttonBackground.color = "0x9F9F9FFF"
m.buttonLabel.color = "0x000000FF"
else
' Element lost focus
m.buttonBackground.color = "0x333333FF"
m.buttonLabel.color = "0xFFFFFFFF"
end if
end sub
sub onTextChange()
m.buttonLabel.text = m.top.text
end sub
function onKeyEvent(key as string, press as boolean) as boolean
if press
if key = "OK"
m.top.buttonSelected = true
return true
end if
end if
return false
end function
MainScene.xml:
<?xml version="1.0" encoding="utf-8"?>
<component name="MainScene" extends="Scene">
<children>
<LayoutGroup
id="buttonGroup"
layoutDirection="vert"
itemSpacings="[20]"
translation="[100, 100]"
>
<FocusableButton
id="button1"
text="Play"
/>
<FocusableButton
id="button2"
text="Settings"
/>
<FocusableButton
id="button3"
text="Exit"
/>
</LayoutGroup>
</children>
<script type="text/brightscript">
<![CDATA[
sub init()
m.buttonGroup = m.top.findNode("buttonGroup")
m.button1 = m.top.findNode("button1")
m.button2 = m.top.findNode("button2")
m.button3 = m.top.findNode("button3")
' Set initial focus
m.button1.setFocus(true)
' Observe button selections
m.button1.observeField("buttonSelected", "onButtonSelected")
m.button2.observeField("buttonSelected", "onButtonSelected")
m.button3.observeField("buttonSelected", "onButtonSelected")
end sub
sub onButtonSelected(event as object)
button = event.getRoSGNode()
print "Button selected: " + button.text
end sub
]]>
</script>
</component>
Set focusable property: Always set focusable="true" on elements that should receive focus.
Handle focus changes: Use observeField("focusedChild", callback) to respond to focus changes.
Visual feedback: Always provide visual feedback when an element has focus (color change, border, scale, etc.).
Key handling: Implement onKeyEvent() to handle remote control input when the element has focus.
Focus management: Use setFocus(true) to programmatically set focus on an element.
Focus groups: Use containers like LayoutGroup or RowList for automatic focus navigation between child elements.
This creates a basic focusable element system that responds to remote control navigation and provides visual feedback for the focused state.