[Firefox OS TV] How to get events from remote controller

Hi! I'm Miguel and I'm the proud developer of the Hubii Smart TV App for the new Panasonic TVs. I'm going to start a serie of articles about Firefox OS development. Some of them are going to be focused on Firefox OS for mobile devices and some about TV development.

Recently Panasonic announced a set of TVs with Firefox OS and, while that's great, there's not so many information out there about developing apps for this system. It's true that almost all the information and examples for mobile is compatible with the TV development but there's some concerns.

One of them is the, obvious, lack of keyboard and mouse to interact with the TV. The user will be interacting with the TV using the remote controller so we've to deal with it.

All of the management of the interaction will be done using Javascript listening the keyboard events (yeah, I know that sounds weird). The important thing that you need to know is the keycode for each button of the remote controller.

So, here you have how we're doing this in the Hubii TV App right now. Please, note that the next code is written using CoffeeScript and it compiles to Javascript. I hope it's easy enough to be understood by any user used to develop in JS.

document.onkeydown = ( e ) ->  
  # do some events depending on the keyCode pressed
  switch e.keyCode
    when 403 then redButtonPressed()
    when 404 then greenButtonPressed()
    when 405 then yellowButtonPressed()
    when 406 then blueButtonPressed()
    when 8 then backButtonPressed()
    when 615 then infoButtonPressed()
    when 38 then arrowUpPressed()
    when 40 then arrowDownPressed()
    when 37 then arrowLeftPressed()
    when 39 then arrowRightPressed()
    when 13 then okButtonPressed()
    else
      hA.log "keycode not controlled: #{e.keyCode}"
      return # to avoid fire the e.preventDefault
  e.preventDefault()