Skip to content

Commit e7934e3

Browse files
committed
メインメニューもキーボード操作できるようにした
1 parent 2747618 commit e7934e3

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

routes/+page.svelte

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { onDestroy, onMount } from "svelte";
3+
import "@/ui-components/menu/menu.css";
34
45
onMount(() => {
56
const userAgent = navigator.userAgent.toLowerCase();
@@ -14,16 +15,48 @@ onMount(() => {
1415
onDestroy(() => {
1516
// document.body.style.backgroundColor = "black";
1617
});
18+
19+
let btn1: HTMLElement;
20+
let btn2: HTMLElement;
21+
function handleKeyDown(e: KeyboardEvent) {
22+
const buttons = [btn1, btn2];
23+
const currentIndex = buttons.indexOf(document.activeElement as HTMLElement);
24+
if (e.key === "ArrowUp") {
25+
if (currentIndex > 0) {
26+
buttons[currentIndex - 1].focus();
27+
} else {
28+
buttons[buttons.length - 1].focus();
29+
}
30+
e.preventDefault();
31+
} else if (e.key === "ArrowDown") {
32+
if (currentIndex < buttons.length - 1) {
33+
buttons[currentIndex + 1].focus();
34+
} else {
35+
buttons[0].focus();
36+
}
37+
e.preventDefault();
38+
} else if (e.key === " ") {
39+
(document.activeElement as HTMLElement)?.click?.();
40+
e.preventDefault();
41+
}
42+
}
43+
$effect(() => {
44+
document.addEventListener("keydown", handleKeyDown);
45+
return () => {
46+
document.removeEventListener("keydown", handleKeyDown);
47+
};
48+
});
1749
</script>
1850

1951
<div id="container" class="fixed inset-0">
2052
<div class="fixed inset-0 backdrop-blur-xs">
2153
<div class="flex flex-col gap-5 mt-30 m-10 p-5 bg-yellow-400/85 rounded-lg">
22-
<h1 class="text-center mt-6 mb-2">Shortcut Game</h1> <!--TODO: 題名-->
23-
<a class="btn modal-btn text-2xl" href="/game?stage=1-1"> Start </a>
24-
<a class="btn modal-btn text-2xl" href="/stage-select"> Stage Select </a>
54+
<h1 class="text-center mt-6 mb-2">Shortcut Game</h1>
55+
<!--TODO: 題名-->
56+
<a class="btn modal-btn text-2xl" href="/game?stage=1-1" bind:this={btn1} tabindex="1"> Start </a>
57+
<a class="btn modal-btn text-2xl" href="/stage-select" bind:this={btn2} tabindex="2"> Stage Select </a>
2558
<p class="bg-white/90 p-2 rounded-lg">
26-
Move: &#x21E6; &#x21E8; or A D<br />
59+
Move: &#x21E6; &#x21E8; or A D<br />
2760
Jump: &#x21E7; or W or Space<br />
2861
Ability: Ctrl + ?
2962
</p>
@@ -34,10 +67,13 @@ onDestroy(() => {
3467
<style>
3568
#container {
3669
background-color: black;
37-
background-image: url('/assets/home.png'), url('/assets/home-block.png');
70+
background-image: url("/assets/home.png"), url("/assets/home-block.png");
3871
background-size: 100%, 100%;
3972
background-repeat: no-repeat, repeat-y;
40-
background-position: left top, left, top;
73+
background-position:
74+
left top,
75+
left,
76+
top;
4177
backdrop-filter: blur(10px);
4278
}
4379
</style>

0 commit comments

Comments
 (0)