Handling Remote Control Events in Samsung SmartTV Application Development

Remote control key codes are mapped into JavaScript codes that switch focus between elements, by assigning an appropriate process to each of the registered key events. See the following list of registered key values for a full-screen application:

  • KEY_VOL_UP

  • KEY_VOL_DOWN

  • KEY_MUTE

  • KEY_TOOLS

  • KEY_INFO

  • KEY_EMODE

  • KEY_DMA

  • KEY_MENU

  • KEY_SOURCE

  • KEY_PRECH

  • KEY_FAVCH

  • KEY_CHLIST

  • KEY_DMA

  • KEY_TTX_MIX

  • KEY_GUIDE

  • KEY_SUBTITLE

  • KEY_ASPECT

  • KEY_DOLBY_SRR

  • KEY_MTS

  • KEY_PANEL_CH_UP

  • KEY_PANEL_CH_DOWN

  • KEY_PANEL_VOL_UP

  • KEY_PANEL_VOL_DOWN

  • KEY_PANEL_ENTER

  • KEY_PANEL_SOURCE

  • KEY_PANEL_MENU

  • KEY_1

  • KEY_2

  • KEY_3

  • KEY_4

  • KEY_5

  • KEY_6

  • KEY_7

  • KEY_8

  • KEY_9

  • KEY_0

  • KEY_WHEELDOWN

  • KEY_WHEELUP

  • KEY_RED

  • KEY_GREEN

  • KEY_YELLOW

  • KEY_BLUE

  • KEY_RW

  • KEY_PAUSE

  • KEY_FF

  • KEY_PLAY

  • KEY_STOP

  • KEY_ENTER

  • KEY_RETURN

  • KEY_EXIT

See the following list of registered key values for a single-wide application (ticker):

  • KEY_WHEELDOWN

  • KEY_WHEELUP

  • KEY_RED

  • KEY_GREEN

  • KEY_YELLOW

  • KEY_BLUE

  • KEY_RW

  • KEY_PAUSE

  • KEY_FF

  • KEY_PLAY

  • KEY_STOP

  • KEY_ENTER

  • KEY_RETURN

  • KEY_EXIT

If an application has a handler and an assigned function for a KEY_UP event, it will process efficiently when a user presses the Up key of the remote controller. But nothing will happen if the application is not programmed to handle the KEY_UP event.

The following example will handle the most commonly used four directional keys, back key, and OK key events. First, add an anchor element to handle the key events.

<!—Anchor element to handle four directional keys, back key, and ok key. →
<a href="javascript:void(0);" id="anchor" onkeydown="Main.keyDown();"></a>

The anchor element must have an href property, but any value assigned for this property will cause the browser to open a new page with the value as its address. That is an unnecessary function for the scene-based development method being used, and can be blocked by calling the void(0) function that always returns null value.

The currently focused anchor will use the onkeydown handler to process the event generated when a SmartTV user presses a remote control key. The handler calls the function if there is a registered function for the event.

The following example uses an event-handling function to process the four directional keys, the Back key, and the OK key, which are received by the previously defined anchor.

// Create a TVKeyValue instance of the Common Module API.
var tvKey = new Common.API.TVKeyValue();
 
// Event handling function registered on the anchor’s handler.
Main.keyDown = function () {
    var keyCode = event.keyCode;
    
    switch (keyCode) {
        case tvKey.KEY_LEFT:
            break;
        case tvKey.KEY_RIGHT:
            break;
        case tvKey.KEY_UP:
            break;
        case tvKey.KEY_DOWN:
            break;
        case tvKey.KEY_ENTER:
            break;
        case tvKey.KEY_RETURN:
            break;
    }
};

See the variable tvKey on the source code. This is an instance of the Common Module API library TVKeyValue.js. All remote control key code values are saved in this object as member properties.

Also, note the object type global variable event.keyCode, which stores the last pressed remote control key value. This value is passed to the local variable keyCode, then is used by a switch statement to process four directional keys, ok key, and return key inputs. The switch statement ignores any other keys.

While there are many key codes, memorizing them is not necessary because only a few common keys will be used by most applications.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com