Search code examples
javascripthtmlamazon-fire-tv

How to prevent default back behavior in Amazon Fire TV HTML5 web app?


I'm trying to override the default back behavior in certain cases, and update my application state rather than fire a history popstate event.

Is there some other event I should hook in to to prevent default behavior? I found the remote back button fires a keydown event with e.keyCode === 27

const FIRE_REMOTE_BACK = 27;
handleBackButton = (e) => {
  e.stopImmediatePropagation();
  e.stopPropagation();
  e.preventDefault();
  // update app state
}
handleKeyDown = (e) => {
  const {keyCode} = e;
  
  if (keyCode === FIRE_REMOTE_BACK) {
    return handleBackButton(e);
  }
}

I see my application state updated briefly, but then back event completes.


Solution

  • Answered my own question. Listen for the 'keyup' (not keydown) event and preventDefault()