diff --git a/CHANGELOG.md b/CHANGELOG.md index a02d1d15..ec2d096d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Playlet logo in web app is clickable, and launches app if not already on - Remote control in web app + - Clickable buttons and keyboard support ## [0.16.0] - 2023-11-13 diff --git a/playlet-web/src/lib/Screens/RemoteControlScreen.svelte b/playlet-web/src/lib/Screens/RemoteControlScreen.svelte index 03c9a6f2..c89999ab 100644 --- a/playlet-web/src/lib/Screens/RemoteControlScreen.svelte +++ b/playlet-web/src/lib/Screens/RemoteControlScreen.svelte @@ -18,8 +18,10 @@ import VolumeUpIcon from "assets/remote-control/volume-up.svg.svelte"; import RemoteButton from "./RemoteControl/RemoteButton.svelte"; + import { onDestroy, onMount } from "svelte"; export let visibility: boolean; + let screen; const BUTTONS = { home: "Home", @@ -42,9 +44,49 @@ volumeUp: "VolumeUp", powerOff: "PowerOff", }; + + const KEYBOARD_BUTTONS = { + ArrowRight: BUTTONS.right, + ArrowLeft: BUTTONS.left, + ArrowUp: BUTTONS.up, + ArrowDown: BUTTONS.down, + Enter: BUTTONS.select, + Backspace: BUTTONS.backspace, + Escape: BUTTONS.back, + "*": BUTTONS.info, + }; + + function pressKey(key) { + console.log(`Sending key: ${key}`); + ExternalControlProtocol.pressKey(key); + } + + onMount(() => { + screen.addEventListener("keydown", onKeyDown); + // TODO:P1 support navigator.mediaSession + // media buttons, such as play/pause, next, and previous track. + // metadata, like title, artist, album, artwork + }); + + onDestroy(() => { + screen.removeEventListener("keydown", onKeyDown); + }); + + function onKeyDown(event) { + if (!visibility) { + return; + } + if (event.key in KEYBOARD_BUTTONS) { + event.preventDefault(); + pressKey(KEYBOARD_BUTTONS[event.key]); + } else { + event.preventDefault(); + pressKey("Lit_" + encodeURIComponent(event.key)); + } + } -