Forum Discussion

Suitie's avatar
Suitie
Visitor
9 years ago

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? 

8 Replies

  • 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.
  • 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?
  • "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.
  • 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
  • "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.