top

Handling Remote Control Key Events

Published 2014-10-27 | (Compatible with SDK 5.0,5.1 and 2014 models)

Learn how to catch remote control key events and how to handle them.

If a TV user presses any button on the remote control a keyDown event occurs. This event is passed to an element that is currently focused and if the element has a registered function in the onkeydown property, that function is executed. The function is registered using event.keyCode.

All the remote control keys must be defined by member variables of the tvKeyValue object as follows:

  1. Insert an element that will have focus in the index.html file:

    <a href="javascript:void(0);" id="anchor" onkeydown="Main.keyDown();"></a>
    
  2. Create a function to be registered in the onkeydown property:

    Main.keyDown = function () {
        var keyCode = event.keyCode;
        alert("Main Key code : " + keyCode);
    
        switch (keyCode) {
            case tvKey.KEY_LEFT:
                // Code for Left key event
                break;
    
            case tvKey.KEY_RIGHT:
                // Code for Right key event
                break;
    
            case tvKey.KEY_UP:
                // Code for Up key event
                break;
    
            case tvKey.KEY_DOWN:
                // Code for Down key event
                break;
    
            case tvKey.KEY_ENTER:
                // Code for Enter key event
                break;
    
            case tvKey.KEY_RETURN:
                // Code for Return key event
                break;
        }
    }
    

Remote control keys for applications

If you access an application, the Application Manager registers basic keys relevant to it. If a key event registered in an application occurs, it is loaded to the application. However, if the key is not registered in the application, the event is not loaded to the application. If an unregistered key is executed, the TV system closes the application and TV functions become available. For example, when an application which has no registered volume key is running and the volume key on remote control is pressed, the working application is forcibly closed and volume banner appears on TV screen.

Users can use it as this caph.platform.Key when register a key.

Enumeration
Enums

 

UP:
up key ( 29460 )
DOWN:
down key ( 29461 )
LEFT:
left key ( 4 )
RIGHT:
right key ( 5 )
ENTER:
enter key ( 29443 )
TOOLS:
tools key ( 75 )
RETURN:
return key ( 88 )
EXIT:
exit key (45 )
INFO:
info key ( 31 )
RED:
red key ( 108 )
GREEN:
green key ( 20 )
YELLOW:
yellow key ( 21 )
BLUE:
blue key ( 22 )
VOL_UP:
volume up ( 7 )
VOL_DOWN:
volume down ( 11 )
MUTE:
volume mute ( 27 )
CH_UP:
channel up ( 68 )
CH_DOWN:
channel down ( 65 )
 
N0:
number key 0 ( 17 )
N1:
number key 1 ( 101 )
N2:
number key 2 ( 98 )
N3:
number key 3 ( 6 )
N4:
number key 4 ( 8 )
N5:
number key 5 ( 9 )
N6:
number key 6 ( 10 )
N7:
number key 7 ( 12 )
N8:
number key 8 ( 13 )
N9:
number key 9 ( 14 )
PRECH:
PRE-CH key ( 259 )
REW:
rewind key ( 69 )
PAUSE:
pause key ( 74 )
FF:
fast forward key ( 72 )
REC:
record key ( 192 )
PLAY:
play key ( 71 )
STOP:
stop key ( 70 )

Key registration/unregistration should be done after the window.onshow event. The keys registered/unregistered after the window.onshow event are processed normally, but those before the window.onshow event may be ignored.

Register a function executed in window.onshow before calling this method because window.onshow event happens after calling the sendReadyEvent() method of the Widget common module.

An example is shown below:

var WIDGET = new Common.API.Widget();        // For sendReadyEvent()
var TVKEY  = new Common.API.TVKeyValue();    // Remote controller key value object
var PL_NNAVI_STATE_BANNER_VOL = 1;


Main.onLoad = function () {    
    this.enableKeys();
    WIDGET.sendReadyEvent();
}

window.onShow = function () {    
    var NNaviPlugin = caph.platform.dtv.Device.plugin('NNAVI');
    NNaviPlugin.SetBannerState(PL_NNAVI_STATE_BANNER_VOL);
    caph.platform.dtv.Device.unRegisterKey(caph.platform.Key.VOL_UP);
    caph.platform.dtv.Device.unRegisterKey(caph.platform.Key.VOL_DOWN);
    caph.platform.dtv.Device.unRegisterKey(caph.platform.Key.MUTE);

    /**
     * code
     */
}

The time of key registration/unregistration after the window.onshow event does not matter.