Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
bmceldowney
Visitor

Is there a way to get the currently focused node?

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?
0 Kudos
12 REPLIES 12
EnTerr
Roku Guru

Re: Is there a way to get the currently focused node?

"bmceldowney" wrote:
            if type(child.focusedChild) = "roInvalid" then exit while


Don't you ever do this! It will break if the value turns out to be `invalid` and not `roInvalid`.
Instead, do this - which works with both:
if child.focusedChild = invalid then exit while
0 Kudos
EnTerr
Roku Guru

Re: Is there a way to get the currently focused node?

"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.
0 Kudos
belltown
Roku Guru

Re: Is there a way to get the currently focused node?

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.
0 Kudos
bmceldowney
Visitor

Re: Is there a way to get the currently focused node?

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.
0 Kudos
EnTerr
Roku Guru

Re: Is there a way to get 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?
0 Kudos
bmceldowney
Visitor

Re: Is there a way to get the currently focused node?

From the console, 7.6.

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.
0 Kudos
EnTerr
Roku Guru

Re: Is there a way 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). 
0 Kudos
EnTerr
Roku Guru

Re: Is there a way to get the currently focused node?

"bmceldowney" wrote:
From the console, 7.6.

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)
0 Kudos
belltown
Roku Guru

Re: Is there a way to get the currently focused node?

"EnTerr" wrote:
"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.
0 Kudos