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: 
TechTalk01
Binge Watcher

Onkey event in Roku for back button funtionality

I am working with Scenegraph Developer Extension (SGDEX) for developing one login flow.
Now when I press Ok button in textBox the keyboard is appearing ,

But what I want is when I press backButton (once) the keyboard should disapper and if I press backButton again my screen should navigate to previous screen,

function onKeyEvent(key as string, press as boolean) as boolean
  handled = false
  if press then 
    if(key = "back")
      if (m.keyboard.visible)
        m.keyboard.visible = false
      end if
      handled = true
    end if

  end if
  return handled
end function
 
Now the problem is if I define onKey back event in my component then my SGDEX default screen stack navigation will not work, if I don't give onkey in my component, then on pressing back button keyboard will not close,

Note:- I want keyboard to get closed using back button only.

 

0 Kudos
2 REPLIES 2
makaiguy
Community Streaming Expert

Re: Onkey event in Roku for back button funtionality

@TechTalk01-

You've posted to a forum for Roku users, where few, if any, will be able to help. The Developers forums are here: https://community.roku.com/t5/Developers/ct-p/channel-developers

Roku Community Streaming Expert
I am not a Roku employee, only a user like you.  Please, no support questions via private message -- post them publicly to the Community where others may benefit as well.
If this post solves your problem please help others find this answer by clicking "Accept as Solution.".

Ultra 4800 | Streaming Stick 4K+ 3821 | TCL Roku TV 43S245/C107X
0 Kudos
speechles
Roku Guru

Re: Onkey event in Roku for back button funtionality

 

function onKeyEvent(key as string, press as boolean) as boolean
  handled = false
  if press then 
    if(key = "back")
      if (m.keyboard.visible)
        m.keyboard.visible = false
         ' only return true when you want to swallow the keypress
         ' we want to swallow the keypress only when keyboard is visible and back is pressed
        handled = true
      end if
    end if
  end if
  return handled
end function

... or even shorter version ...

function onKeyEvent(key as string, press as boolean) as boolean
  handled = false
  if press and (key = "back") and (m.keyboard.visible)
    m.keyboard.visible = false
    ' only return true when you want to swallow the keypress
    ' we want to swallow the keypress only when keyboard is visible and back is pressed
    handled = true
  end if
  return handled
end function

 

You almost had it. You just forgot to only swallow the keypress at the right time part. You don't want to return true every time the back button is pressed which is what your code block did. You only want to swallow the keypress (aka return true) when you have a visible keyboard.

When the OnKeyEvent returns false it allows the keypress to pass upwards to the other components in the extend/scene.

0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.