Your Digital Media Has Never Looked So Good

 
Suitie
Topic Author
Posts: 2
Joined: Tue May 16, 2017 8:44 am

left and right always fired twice!!

Tue May 16, 2017 9:12 am

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? 
 
User avatar
squirreltown
Posts: 752
Joined: Sun Apr 21, 2013 2:20 pm

Re: left and right always fired twice!!

Tue May 16, 2017 9:47 am

Button press and button release are two different events.
Kinetics Screensaver (kineticsscreensaver), Kinetics³ Screensaver(kinetics3), Kinetics Painter Screensaver (kineticspainter), Kinetics Splash Screensaver (kineticssplash)
 
User avatar
RokuNB
Posts: 308
Joined: Fri Mar 31, 2017 2:22 pm

Re: left and right always fired twice!!

Tue May 16, 2017 10:11 am

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.
 
Suitie
Topic Author
Posts: 2
Joined: Tue May 16, 2017 8:44 am

Re: left and right always fired twice!!

Wed May 17, 2017 2:21 am

Thank you! I get it now 
 
tim_beynart
Posts: 185
Joined: Wed Jul 15, 2015 8:30 am

Re: left and right always fired twice!!

Fri Aug 04, 2017 1:43 pm

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?
 
User avatar
RokuNB
Posts: 308
Joined: Fri Mar 31, 2017 2:22 pm

Re: left and right always fired twice!!

Fri Aug 04, 2017 4:11 pm

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.
 
tim_beynart
Posts: 185
Joined: Wed Jul 15, 2015 8:30 am

Re: left and right always fired twice!!

Tue Aug 08, 2017 9:38 am

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
 
User avatar
RokuNB
Posts: 308
Joined: Fri Mar 31, 2017 2:22 pm

Re: left and right always fired twice!!

Tue Aug 08, 2017 10:03 am

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.
 
tim_beynart
Posts: 185
Joined: Wed Jul 15, 2015 8:30 am

Re: left and right always fired twice!!

Tue Aug 08, 2017 10:56 am

that explains it, thanks.

Who is online

Users browsing this forum: balbant and 4 guests