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: 
Suitie
Visitor

left and right always fired twice!!

First of all, I am a very beginner in Roku development, so I am sorry if my question is silly... I've followed all tutorials available and now I am trying to create a little app just to learn... but i get an "error" in the simplest function, and I am frustrated
My code:
'''
sub init()
     m.top.setFocus(true)
End sub
function onKeyEvent(key as String, press as Boolean) as Boolean
    if (key = "left") then 
        print "pressing left"
        return true
    end if
    if (key = "right") then 
        print "pressing right"
        return true
    end if
    return false
end function
'''
What I get when I press "left":
pressing left
pressing left
OnKeyEvent is fired twice whenever I press a key, the first one when the key is down, and the second one when it is up. Why is this happening? Where is the mistake? 
0 Kudos
8 REPLIES 8
squirreltown
Roku Guru

Re: left and right always fired twice!!

Button press and button release are two different events.
Kinetics Screensavers
0 Kudos
RokuNB
Roku Guru

Re: left and right always fired twice!!

Check the `press` param - the first time it's `true`, the second - `false`. You get notified once when user presses the button and 2nd time when they release it - this is helpful e.g. when writing games or scrolling through lists. It's up to you to decide which one of the 2 cases is the action item for you.

"onKeyEvent()" wrote:
The press parameter is a boolean value that is true if the key was pressed, and false if the key was released.
0 Kudos
Suitie
Visitor

Re: left and right always fired twice!!

Thank you! I get it now 
0 Kudos
tim_beynart
Channel Surfer

Re: left and right always fired twice!!

I never see press=true when using the OK, Play, or FastForward buttons. The only time I ever see it in the log is when I use the BACK button. Is this normal?
0 Kudos
RokuNB
Roku Guru

Re: left and right always fired twice!!

"tim_beynart" wrote:
I never see press=true when using the OK, Play, or FastForward buttons. The only time I ever see it in the log is when I use the BACK button. Is this normal?

Which node has the focus? Where in the focus chain is your observer?
My guess is somebody else consumed the "press" events for these - be it Roku or your own component.
0 Kudos
tim_beynart
Channel Surfer

Re: left and right always fired twice!!

The Video node has focus. It is a child of my main Scene. In my Scene I have the boilerplate handler. One other node in my app uses OnKeyEvent to capture right and left keys, returns false otherwise. I guess I could mess around with a less complex app to see what the default behavior is, but in general I do not see events bubble like I would expect. My expectations could be way off though.
function onKeyEvent(key, press) as Boolean
   result = false
    ? "onKeyEvent", key, press
    ... magic goes here...
   return result
end function
0 Kudos
RokuNB
Roku Guru

Re: left and right always fired twice!!

"tim_beynart" wrote:
The Video node has focus. It is a child of my main Scene. In my Scene I have the boilerplate handler. One other node in my app uses OnKeyEvent to capture right and left keys, returns false otherwise. I guess I could mess around with a less complex app to see what the default behavior is, but in general I do not see events bubble like I would expect. My expectations could be way off though.

The video node handles these keys, right? So it consuming them seems the right thing to do. If you want to hear them first but the Video also receiving them, then set some child node (group?) under the video and set input focus on it - the return false in your handler so the event is keeps going up, to the video.
0 Kudos
tim_beynart
Channel Surfer

Re: left and right always fired twice!!

that explains it, thanks.
0 Kudos