2020-01-17 12:05:56 +03:00
|
|
|
define(["inputManager", "layoutManager"], function (inputManager, layoutManager) {
|
|
|
|
"use strict";
|
2019-05-08 07:03:59 +02:00
|
|
|
|
|
|
|
console.log("keyboardnavigation");
|
|
|
|
|
2020-01-17 12:05:56 +03:00
|
|
|
/**
|
|
|
|
* Key name mapping.
|
|
|
|
*/
|
|
|
|
// Add more to support old browsers
|
|
|
|
var KeyNames = {
|
|
|
|
13: "Enter",
|
|
|
|
19: "Pause",
|
|
|
|
27: "Escape",
|
|
|
|
32: "Space",
|
|
|
|
37: "ArrowLeft",
|
|
|
|
38: "ArrowUp",
|
|
|
|
39: "ArrowRight",
|
|
|
|
40: "ArrowDown",
|
2020-01-24 16:44:45 +03:00
|
|
|
// MediaRewind (Tizen/WebOS)
|
|
|
|
412: "MediaRewind",
|
|
|
|
// MediaStop (Tizen/WebOS)
|
|
|
|
413: "MediaStop",
|
|
|
|
// MediaPlay (Tizen/WebOS)
|
|
|
|
415: "MediaPlay",
|
|
|
|
// MediaFastForward (Tizen/WebOS)
|
|
|
|
417: "MediaFastForward",
|
|
|
|
// Back (WebOS)
|
|
|
|
461: "Back",
|
|
|
|
// Back (Tizen)
|
|
|
|
10009: "Back",
|
|
|
|
// MediaTrackPrevious (Tizen)
|
|
|
|
10232: "MediaTrackPrevious",
|
|
|
|
// MediaTrackNext (Tizen)
|
|
|
|
10233: "MediaTrackNext",
|
|
|
|
// MediaPlayPause (Tizen)
|
|
|
|
10252: "MediaPlayPause"
|
2020-01-17 12:05:56 +03:00
|
|
|
};
|
|
|
|
|
2020-01-19 01:09:42 +03:00
|
|
|
var hasFieldKey = false;
|
|
|
|
try {
|
|
|
|
hasFieldKey = "key" in new KeyboardEvent("keydown");
|
|
|
|
} catch (e) {
|
|
|
|
console.log("error checking 'key' field");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasFieldKey) {
|
|
|
|
// Add [a..z]
|
|
|
|
for (var i = 65; i <= 90; i++) {
|
|
|
|
KeyNames[i] = String.fromCharCode(i).toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:56 +03:00
|
|
|
/**
|
|
|
|
* Returns key name from event.
|
|
|
|
*
|
|
|
|
* @param {KeyboardEvent} keyboard event
|
|
|
|
* @return {string} key name
|
|
|
|
*/
|
|
|
|
function getKeyName(event) {
|
|
|
|
return KeyNames[event.keyCode] || event.key;
|
|
|
|
}
|
|
|
|
|
2019-05-08 07:03:59 +02:00
|
|
|
function enable() {
|
2020-01-17 12:05:56 +03:00
|
|
|
document.addEventListener("keydown", function (e) {
|
2019-05-08 07:03:59 +02:00
|
|
|
var capture = true;
|
|
|
|
|
2020-01-17 12:05:56 +03:00
|
|
|
switch (getKeyName(e)) {
|
|
|
|
case "ArrowLeft":
|
|
|
|
inputManager.handle("left");
|
|
|
|
break;
|
|
|
|
case "ArrowUp":
|
|
|
|
inputManager.handle("up");
|
|
|
|
break;
|
|
|
|
case "ArrowRight":
|
|
|
|
inputManager.handle("right");
|
2019-11-23 00:29:38 +09:00
|
|
|
break;
|
2020-01-17 12:05:56 +03:00
|
|
|
case "ArrowDown":
|
|
|
|
inputManager.handle("down");
|
2019-11-23 00:29:38 +09:00
|
|
|
break;
|
2020-01-17 12:05:56 +03:00
|
|
|
|
|
|
|
case "Back":
|
|
|
|
inputManager.handle("back");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Escape":
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
inputManager.handle("back");
|
|
|
|
} else {
|
|
|
|
capture = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "MediaPlay":
|
|
|
|
inputManager.handle("play");
|
2019-11-23 00:29:38 +09:00
|
|
|
break;
|
2020-01-17 12:05:56 +03:00
|
|
|
case "Pause":
|
|
|
|
inputManager.handle("pause");
|
2019-11-23 00:29:38 +09:00
|
|
|
break;
|
2020-01-17 12:05:56 +03:00
|
|
|
case "MediaPlayPause":
|
|
|
|
inputManager.handle("playpause");
|
|
|
|
break;
|
|
|
|
case "MediaRewind":
|
|
|
|
inputManager.handle("rewind");
|
|
|
|
break;
|
|
|
|
case "MediaFastForward":
|
|
|
|
inputManager.handle("fastforward");
|
|
|
|
break;
|
|
|
|
case "MediaStop":
|
|
|
|
inputManager.handle("stop");
|
|
|
|
break;
|
|
|
|
case "MediaTrackPrevious":
|
|
|
|
inputManager.handle("previoustrack");
|
|
|
|
break;
|
|
|
|
case "MediaTrackNext":
|
|
|
|
inputManager.handle("nexttrack");
|
|
|
|
break;
|
|
|
|
|
2019-11-23 00:29:38 +09:00
|
|
|
default:
|
|
|
|
capture = false;
|
2019-05-08 07:03:59 +02:00
|
|
|
}
|
2020-01-17 12:05:56 +03:00
|
|
|
|
2019-05-08 07:03:59 +02:00
|
|
|
if (capture) {
|
|
|
|
console.log("Disabling default event handling");
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-01-17 12:05:56 +03:00
|
|
|
|
2019-05-08 07:03:59 +02:00
|
|
|
return {
|
2020-01-19 01:09:42 +03:00
|
|
|
enable: enable,
|
|
|
|
getKeyName: getKeyName
|
2019-05-08 07:03:59 +02:00
|
|
|
};
|
|
|
|
});
|