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 {
|
export class ExternalControlProtocol {
|
||||||
static async launchApp(appId) {
|
static async launchApp(appId) {
|
||||||
|
console.log("Launching app", appId)
|
||||||
return fetch(`http://${getIp()}:8060/launch/${appId}`, {
|
return fetch(`http://${getIp()}:8060/launch/${appId}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: '',
|
body: '',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
static async pressKey(key) {
|
static async pressKeyUp(key) {
|
||||||
return fetch(`http://${getIp()}:8060/keypress/${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',
|
method: 'POST',
|
||||||
body: '',
|
body: '',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,18 +1,59 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ExternalControlProtocol } from "lib/Api/ExternalControlProtocol";
|
import { ExternalControlProtocol } from "lib/Api/ExternalControlProtocol";
|
||||||
|
import { onDestroy, onMount } from "svelte";
|
||||||
|
|
||||||
export let key: string;
|
export let key: string;
|
||||||
export let icon;
|
export let icon;
|
||||||
export let small: boolean = false;
|
export let small: boolean = false;
|
||||||
|
|
||||||
function pressKey() {
|
let button;
|
||||||
console.log(`Sending key: ${key}`);
|
|
||||||
ExternalControlProtocol.pressKey(key);
|
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>
|
</script>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
on:click={pressKey}
|
bind:this={button}
|
||||||
class="{small
|
class="{small
|
||||||
? 'w-16'
|
? 'w-16'
|
||||||
: 'w-24'} h-11 btn-primary rounded-xl flex items-center justify-center"
|
: 'w-24'} h-11 btn-primary rounded-xl flex items-center justify-center"
|
||||||
|
|||||||
@@ -56,13 +56,9 @@
|
|||||||
"*": BUTTONS.info,
|
"*": BUTTONS.info,
|
||||||
};
|
};
|
||||||
|
|
||||||
function pressKey(key) {
|
|
||||||
console.log(`Sending key: ${key}`);
|
|
||||||
ExternalControlProtocol.pressKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
screen.addEventListener("keydown", onKeyDown);
|
screen.addEventListener("keydown", onKeyDown);
|
||||||
|
screen.addEventListener("keyup", onKeyUp);
|
||||||
// TODO:P1 support navigator.mediaSession
|
// TODO:P1 support navigator.mediaSession
|
||||||
// media buttons, such as play/pause, next, and previous track.
|
// media buttons, such as play/pause, next, and previous track.
|
||||||
// metadata, like title, artist, album, artwork
|
// metadata, like title, artist, album, artwork
|
||||||
@@ -70,18 +66,36 @@
|
|||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
screen.removeEventListener("keydown", onKeyDown);
|
screen.removeEventListener("keydown", onKeyDown);
|
||||||
|
screen.removeEventListener("keyup", onKeyUp);
|
||||||
});
|
});
|
||||||
|
|
||||||
function onKeyDown(event) {
|
function onKeyDown(event) {
|
||||||
if (!visibility) {
|
if (!visibility || event.repeat) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.key in KEYBOARD_BUTTONS) {
|
if (event.key in KEYBOARD_BUTTONS) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
pressKey(KEYBOARD_BUTTONS[event.key]);
|
ExternalControlProtocol.pressKeyDown(KEYBOARD_BUTTONS[event.key]);
|
||||||
} else {
|
} else {
|
||||||
event.preventDefault();
|
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>
|
</script>
|
||||||
|
|||||||
Reference in new issue
Block a user