Example: Keycodes

This example includes an external javascript file with the following:

<script type="text/ecmascript" xlink:href="keycodes.js" ></script>

It also states that the functions onLoad() and onUnload2() shall be used when the document loads and unloads:

<svg onload="onLoad()"
        onunload="onUnload2()"
        ...

These functions are defined in the keycodes.js file. Open it now. It begins with some definitions of variables, followed by the onLoad() function.

DOM events

A standard DOM key event is generated when the user presses a button on the remote control or keyboard. The keyIdentifier parameter in this event lets the portal programmer know what button was pressed. These keyIdentifiers conform as much as possible to those defined by the W3C. See W3C keyboard event definition for more info. The KreaTV IP-STB adds some extra identifiers for buttons specific to our remote controls and keyboards.

The example begins by adding an event listener for keydown messages.

document.addEventListener("keydown", handleRawKeyEvent, false);

Each time an event is generated, the handleRawKeyEvent() function will be called, and passed a keyboard event object. The example prints the values of the members in this event to the TV screen and log for you to study.

alert("keyIdentifier is " + ev.keyIdentifier);
alert("keyLocation is " + ev.keyLocation);
alert("ctrlKey is " + ev.ctrlKey);
alert("shiftKey is " + ev.shiftKey);
alert("altKey is " + ev.altKey);
alert("metaKey is " + ev.metaKey + "\n");

The event.keyIdentifier is the most important value, which identifies most of the common buttons in use. For numbers, a string such as "U+0030" is received. The example creates easier to use definitions for these:

var KEY_0     = "U+0030";
var KEY_1     = "U+0031";
var KEY_2     = "U+0032";
var KEY_3     = "U+0033";

Dealing with modifiers

The example also creates some special defintions for the scroll keys, which are not part of the W3C list. The keyIdentifiers "Up", "Down", "Left" and "Right" are sent, but with the ctrlKey flag set to true. The example uses a switch statement to convert these events into a unique keyIdentifier, so only keyIdentifiers need be used elsewhere in the portal.

// convert event into just unique keyIdentifiers
if (ev.ctrlKey) {
  switch (key) {
    case "Up":
    key = KEY_SCROLL_UP;
    break;
  case "Down":
    key = KEY_SCROLL_DOWN;
    break;

Note that this is performed for convenience, it is not a necessary step. Next, the keyIdentifier only is passed to the onKeyDown() function which looks at the keyIdentifier and performs various actions.

Standby

The standby key is a special case. It is not possible to receive the standby keypress like regular keypresses. Instead, if you want to perform an action when the IP-STB goes into or leaves standby you need to subscribe to the system object, var.io.state. When the value of this object changes, the portal receives a callback, so you know a change to the standby state has just taken place. See the example on Information Service for more information on how this subscription is done.