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

Is it possible to count remote key press?

Here I want to count each remote key press.  I want to do following way
If "Down" key pressed for the first time => I can do something

If "Down" key pressed for the second time => I can do something more
A step by step job.


function onKeyEvent(key as String, press as Boolean) as Boolean
handled = false

if press then

if(key = "down")
//do something
end if
//and again if pressed, do something
end if
return handled
end function


So how can I count the remote down key press ? I hope you got the idea.
0 Kudos
3 REPLIES 3
EnTerr
Roku Guru

Re: Is it possible to count remote key press?

You need to persist the state between events. "m" comes to mind.

function onKeyEvent(key as String, press as Boolean) as Boolean
handled = false

if press then
if m.key_cnt = invalid then m.key_cnt = {}
if m.key_cnt[key] = invalid then m.key_cnt[key] = 0
m.key_cnt[key] += 1

if key = "down" and key.key_cnt[key] = 3:
//do something on the 3rd press of Down 🙂
end if
end if

return handled
end function
0 Kudos
RENJITHVR4
Visitor

Re: Is it possible to count remote key press?

"EnTerr" wrote:
You need to persist the state between events. "m" comes to mind.

function onKeyEvent(key as String, press as Boolean) as Boolean
 handled = false

 if press then
   if m.key_cnt = invalid then m.key_cnt = {}
   if m.key_cnt[key] = invalid then m.key_cnt[key] = 0
   m.key_cnt[key] += 1

   if key = "down" and key.key_cnt[key] = 3:
     //do something on the 3rd press of Down 🙂
   end if
 end if

 return handled
end function


Thank you, bro. But I got an error - Syntax Error. (runtime error &h02),  I have a doubt , is it key.key_cnt[key] or m.key_cnt[key] ?. 
0 Kudos
EnTerr
Roku Guru

Re: Is it possible to count remote key press?

"RENJITHVR4" wrote:
Thank you, bro. But I got an error - Syntax Error. (runtime error &h02),  I have a doubt , is it  key.key_cnt[key] or m.key_cnt[key] ?

Right, you got it - should been `m.key_cnt[key]`. 
This is just something i typed off-hand just for you - not something i have used nor tested. There be typos. The important part is to get the idea. 
0 Kudos