support long press and touch
This commit is contained in:
1 parent
dca3e42c79
commit
d89b1c519d
3 files changed
+79
-14
No files matched your search
@@ -2,14 +2,24 @@ import { getIp } from "./Host";
|
||||
|
||||
export class ExternalControlProtocol {
|
||||
static async launchApp(appId) {
|
||||
console.log("Launching app", appId)
|
||||
return fetch(`http://${getIp()}:8060/launch/${appId}`, {
|
||||
method: 'POST',
|
||||
body: '',
|
||||
})
|
||||
}
|
||||
|
||||
static async pressKey(key) {
|
||||
return fetch(`http://${getIp()}:8060/keypress/${key}`, {
|
||||
static async pressKeyUp(key) {
|
||||
console.log("Pressing key up", key)
|
||||
return fetch(`http://${getIp()}:8060/keyup/${key}`, {
|
||||
method: 'POST',
|
||||
body: '',
|
||||
})
|
||||
}
|
||||
|
||||
static async pressKeyDown(key) {
|
||||
console.log("Pressing key down", key)
|
||||
return fetch(`http://${getIp()}:8060/keydown/${key}`, {
|
||||
method: 'POST',
|
||||
body: '',
|
||||
})
|
||||
|
||||
@@ -1,18 +1,59 @@
|
||||
<script lang="ts">
|
||||
import { ExternalControlProtocol } from "lib/Api/ExternalControlProtocol";
|
||||
import { onDestroy, onMount } from "svelte";
|
||||
|
||||
export let key: string;
|
||||
export let icon;
|
||||
export let small: boolean = false;
|
||||
|
||||
function pressKey() {
|
||||
console.log(`Sending key: ${key}`);
|
||||
ExternalControlProtocol.pressKey(key);
|
||||
let button;
|
||||
|
||||
onMount(() => {
|
||||
const isTouchDevice = "ontouchstart" in document.documentElement;
|
||||
if (isTouchDevice) {
|
||||
button.addEventListener("touchstart", pressKeyDown);
|
||||
button.addEventListener("touchend", pressKeyUp);
|
||||
button.addEventListener("touchcancel", onMouseLeave);
|
||||
} else {
|
||||
button.addEventListener("mousedown", pressKeyDown);
|
||||
button.addEventListener("mouseup", pressKeyUp);
|
||||
button.addEventListener("mouseleave", onMouseLeave);
|
||||
}
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
const isTouchDevice = "ontouchstart" in document.documentElement;
|
||||
if (isTouchDevice) {
|
||||
button.removeEventListener("touchstart", pressKeyDown);
|
||||
button.removeEventListener("touchend", pressKeyUp);
|
||||
button.removeEventListener("touchcancel", onMouseLeave);
|
||||
} else {
|
||||
button.removeEventListener("mousedown", pressKeyDown);
|
||||
button.removeEventListener("mouseup", pressKeyUp);
|
||||
button.removeEventListener("mouseleave", onMouseLeave);
|
||||
}
|
||||
});
|
||||
|
||||
let isDown = false;
|
||||
function pressKeyDown() {
|
||||
isDown = true;
|
||||
ExternalControlProtocol.pressKeyDown(key);
|
||||
}
|
||||
|
||||
function pressKeyUp() {
|
||||
isDown = false;
|
||||
ExternalControlProtocol.pressKeyUp(key);
|
||||
}
|
||||
|
||||
function onMouseLeave() {
|
||||
if (isDown) {
|
||||
pressKeyUp();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<button
|
||||
on:click={pressKey}
|
||||
bind:this={button}
|
||||
class="{small
|
||||
? 'w-16'
|
||||
: 'w-24'} h-11 btn-primary rounded-xl flex items-center justify-center"
|
||||
|
||||
@@ -56,13 +56,9 @@
|
||||
"*": BUTTONS.info,
|
||||
};
|
||||
|
||||
function pressKey(key) {
|
||||
console.log(`Sending key: ${key}`);
|
||||
ExternalControlProtocol.pressKey(key);
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
screen.addEventListener("keydown", onKeyDown);
|
||||
screen.addEventListener("keyup", onKeyUp);
|
||||
// TODO:P1 support navigator.mediaSession
|
||||
// media buttons, such as play/pause, next, and previous track.
|
||||
// metadata, like title, artist, album, artwork
|
||||
@@ -70,18 +66,36 @@
|
||||
|
||||
onDestroy(() => {
|
||||
screen.removeEventListener("keydown", onKeyDown);
|
||||
screen.removeEventListener("keyup", onKeyUp);
|
||||
});
|
||||
|
||||
function onKeyDown(event) {
|
||||
if (!visibility) {
|
||||
if (!visibility || event.repeat) {
|
||||
return;
|
||||
}
|
||||
if (event.key in KEYBOARD_BUTTONS) {
|
||||
event.preventDefault();
|
||||
pressKey(KEYBOARD_BUTTONS[event.key]);
|
||||
ExternalControlProtocol.pressKeyDown(KEYBOARD_BUTTONS[event.key]);
|
||||
} else {
|
||||
event.preventDefault();
|
||||
pressKey("Lit_" + encodeURIComponent(event.key));
|
||||
ExternalControlProtocol.pressKeyDown(
|
||||
"Lit_" + encodeURIComponent(event.key)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function onKeyUp(event) {
|
||||
if (!visibility || event.repeat) {
|
||||
return;
|
||||
}
|
||||
if (event.key in KEYBOARD_BUTTONS) {
|
||||
event.preventDefault();
|
||||
ExternalControlProtocol.pressKeyUp(KEYBOARD_BUTTONS[event.key]);
|
||||
} else {
|
||||
event.preventDefault();
|
||||
ExternalControlProtocol.pressKeyUp(
|
||||
"Lit_" + encodeURIComponent(event.key)
|
||||
);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user