For the below code, I have two questions…
For this particular snippet it was part of a scene where I threw a bunch of components into the scene just to test them out… there is a button component for which I “select” when I press the down key. Originally, when I pressed any other key was setting m.button.setFocus(False).
Question 1: Why, when I set this to false did my focus just disappear into the scene and no additional key events could be captured?
Question 2: I ‘fixed’ the problem by losing focus on the button, which didn’t highlight it anymore, and then returning focus to m.top. Is this the correct “pattern” to use? It feels odd to me that I would have to explicitly lose focus on something before re-gaining the parent focus I once I had.
I can paste more code, but I figured this is a pretty ‘n00b’ question.
Function onKeyEvent(key as String, press as Boolean) as Boolean
handled = false
if press
if key = "down"
? "down was pressed"
m.button.setFocus(True)
handled = True
else
m.button.setFocus(False)
m.top.setFocus(True)
? "some other button was pressed"
handled = True
end if
end if
return handled
end Function
per the doc, “Setting the remote control focus to false is rarely necessary, and can lead to unexpected behavior.” https://sdkdocs.roku.com/display/sdkdoc/ifSGNodeFocus#ifSGNodeFocus-setFocus(onasBoolean)asBoolean
You probably don’t want to set focus to top but to another component here. otherwise you should let the parent component handle the keypress and set focus on wherever it should go next?
ok.. so I read that page like 10x while hunting this problem down and I completely ignored that giant yellow cautionary message… thanks for pointing that out to me.
So a follow up question (and I apologize if I am missing this yet again in the documentation):
Below is the other portion of this code… when I setFocus(True) to some other “component” in this scene can anything in this scene “take focus”? For instance, since I just have one button, up and down can always go to the button focus, but then when I want to move focus can I move it to the rectangle or to do the label (even tho it doesn’t make sense to move the focus there) – my expectation is to move the focus to some “ghost” element/component such that focus is “hidden” from the user, but then pressing up or `down can then be captured/set back to the button.
DISCLAIMER: This is not functional code, but rather a smattering of various Roku SDK examples/SDK docs to help me learn, so I completely understand that this is not ‘best’, just asking so I can understand how this works in context of what I’ve frankensteined together.
Thanks!
<?xml version = "1.0" encoding = "utf-8" ?>
<component name = "HelloWorld" extends = "Scene" >
<script type="text/brightscript" uri="pkg:/components/helloworld_scene.brs" />
<children >
<Label
id = "TownName"
text = "Sacramento, CA"
color = "#FFFFFF"
translation="[300, 50]"
font = "font:LargeBoldSystemFont"
/>
<Poster
id="sunnyImage"
uri="pkg:/images/weather_sunny.png"
width="0"
height="0"
translation="[50,40]"
/>
<Button
id="button3"
minWidth="300"
text="Click Me"
translation="[200, 200]"
showFocusFootprint = "true"
/>
<Rectangle
id = "exampleRectangle"
width = "512"
height = "288"
color = "#FFFFFF"
translation = "[40, 500]"
opacity=".5">
<Label
id = "buttonLabel1"
width="0"
text = "Sample Button Text"
color="#000000"
/>
</Rectangle>
</children>
</component>
yeah, no need to apologize, we are all learning and the docs aren’t the easiest to follow.
To answer your question, “yes” any Node element can be made focusable by setting its “focusable” field to true https://sdkdocs.roku.com/display/sdkdoc/Node
so in your example, add to your rectangle’s XML definition:
focusable="true"
then you can do other stuff like have the node observe it’s focusedChild and when it changes, change size or color or position if node.hasFocus() = true