I’m wondering if there is a reliable way to get a reference to the currently focused node. I’m familiar with observing the focusedChild property, but that only gets me the immediate child in the focus chain. I’d prefer to be able to do something along these lines:
sub init()
m.top.observeField("focusedChild", "focusChanged")
end sub
sub focusChanged(evt)
if m.top.isInFocusChain()
child = evt.getData()
while true
if type(child.focusedChild) = "roInvalid" then exit while
child = child.focusedChild
end while
'do some stuff with the focused node
end if
end sub
But according to the focusedChild documentation:
Accessing the value of the field will result in script errors.
Is there a way to get the node that actually has focus?
“bmceldowney” wrote:
I’m wondering if there is a reliable way to get a reference to the currently focused node.
Curious - why do you need to know the node that has the input focus? What’s the use case.
I was looking for the same thing a while back. The use case was a component that acted as a modal dialog (my own component, not a Dialog Node, because standard Dialog nodes are not very customizable). When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It’s not sufficient to just turn off the dialog’s visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the “lastFocusedNode”, then re-set the focus as it exits. I ended up doing something kludgy like adding an “exit” field to the dialog component, which the invoking component observed to allow it to set the focus back. It’s a pain if you’re doing that in multiple different places. I miss the “screen stack” concept of the old SDK.
Thanks for the heads up about Invalid checking, I’ll use that in the future.
In a nutshell, the use case is a component I’m creating that extends LayoutGroup. It checks the focused child bounds against screen bounds to determine if the focused node is partially or entirely offscreen and then adjusts it’s own translation to make sure the focused node is inside the screen bounds. It works great if the focused node is an immediate descendant of my component, but I’d like it to work on nodes that are nested as well.
So far my biggest hurdle is the fact that simply accessing the focusedChild property results in a “Execution timeout (runtime error &h23)”.
I’m hoping there’s another way to get a reference to the currently focused node.
“bmceldowney” wrote:
So far my biggest hurdle is the fact that simply accessing the focusedChild property results in a “Execution timeout (runtime error &h23)”.
This is a strange error, what firmware? 7.5 or 7.6 beta?
Programmatically or from console?
According to the Roku docs, accessing the value of focusedChild will result in “script errors”, so I’m assuming that’s the problem. That’s why I’m looking for any alternative methods to get the currently focused node.
“belltown” wrote:
When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It’s not sufficient to just turn off the dialog’s visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the “lastFocusedNode”, then re-set the focus as it exits.
I can think of couple of complications to this - which may not apply to your case but in principle - first, it’s possible the last focused node does not exist at this point (e.g. somebody removed it in the mean time) - what’s to do then? And second, somebody was saying there are cases in which setFocus() cannot restore the exact focused item - something to do with compound components (was it grids or row lists… i don’t remember).
According to the Roku docs, accessing the value of focusedChild will result in “script errors”, so I’m assuming that’s the problem. That’s why I’m looking for any alternative methods to get the currently focused node.
And according to me, that timeout is a bug in the 7.6 beta which you are enrolled in - please report in the beta forum
(Try on 7.5 if you have one handy)
“belltown” wrote:
When exiting the dialog, I wanted to get back to where I was before the dialog was invoked. It’s not sufficient to just turn off the dialog’s visibility; you have to re-set the focus to the previous component. It would be easy if the modal component could just access the “lastFocusedNode”, then re-set the focus as it exits.
I can think of couple of complications to this - which may not apply to your case but in principle - first, it’s possible the last focused node does not exist at this point (e.g. somebody removed it in the mean time) - what’s to do then? And second, somebody was saying there are cases in which setFocus() cannot restore the exact focused item - something to do with compound components (was it grids or row lists… i don’t remember).
In the first case, return Invalid. In the second case, I’d have to know more about those “cases in which setFocus() cannot restore the exact focused item”.
It’s a moot point anyway. I’m not going to hold my breath waiting for Roku to implement such a feature, just carry on and work around it.
“EnTerr” wrote:
And according to me, that timeout is a bug in the 7.6 beta which you are enrolled in - please report in the beta forum
(Try on 7.5 if you have one handy)
Hmm, that’s curious. Well, thank you for your assistance, I’ll give 7.5 a shot and post in the beta forum as you suggest.
“belltown” wrote:
In the first case, return Invalid. In the second case, I’d have to know more about those “cases in which setFocus() cannot restore the exact focused item”.
What?
I think you and me were thinking different things - me, that there be a .getFocusedNode() method that returns currently focused node that can be stored in variable lastFocusedNode and then my concern is by the time one gets to use that stored info is unusable (node was detached).
In your case, you are imagining some magical function getLastFocusedNode() that would keep track of what, prior focused nodes? That’s confusing to me, what is the definition of “last” - for example if i do .setFocus() on a node that is already focused, does “last” become the same node or does it do special check that it’s the “last focused node that is not the current focused one”.
According to the Roku docs, accessing the value of focusedChild will result in “script errors”, so I’m assuming that’s the problem. That’s why I’m looking for any alternative methods to get the currently focused node.
It’s not a bug, focusedChild can be used with observeField to know if the object himself lose or get the focus. So, the last child has himself as focusedChild.
I need the same loop like you, but we need to that to break the loop :
if(child.focusedChild = invalid or child.isSameNode(child.focusedChild)) then