live hotkey capture

This commit is contained in:
2026-05-12 00:08:24 +03:00
parent 14d02cdad4
commit 7a821f54fc
3 changed files with 144 additions and 45 deletions
+13 -25
View File
@@ -1,20 +1,21 @@
<script lang="ts"> <script lang="ts">
import { api } from "./api" import { api } from "./api"
import HotkeyInput from "./HotkeyInput.svelte"
let { onDone }: { onDone: () => void } = $props() let { onDone }: { onDone: () => void } = $props()
let start = $state("") let start: string | null = $state(null)
let stop = $state("") let stop: string | null = $state(null)
let pause = $state("") let pause: string | null = $state(null)
let saving = $state(false) let saving: boolean = $state(false)
async function save() { async function save() {
saving = true saving = true
try { try {
const cfg = await api.getConfig() const cfg = await api.getConfig()
cfg.hotkeys.start = start.trim() || null cfg.hotkeys.start = start
cfg.hotkeys.stop = stop.trim() || null cfg.hotkeys.stop = stop
cfg.hotkeys.pause = pause.trim() || null cfg.hotkeys.pause = pause
await api.saveConfig(cfg) await api.saveConfig(cfg)
onDone() onDone()
} finally { } finally {
@@ -22,7 +23,7 @@
} }
} }
let valid = $derived(start.trim() && stop.trim() && pause.trim()) let valid = $derived(!!start && !!stop && !!pause)
</script> </script>
<main> <main>
@@ -37,15 +38,15 @@
<div class="form"> <div class="form">
<label> <label>
<span>start</span> <span>start</span>
<input bind:value={start} placeholder="e.g. F9" /> <HotkeyInput bind:value={start} placeholder="e.g. F9" />
</label> </label>
<label> <label>
<span>stop</span> <span>stop</span>
<input bind:value={stop} placeholder="e.g. F9" /> <HotkeyInput bind:value={stop} placeholder="e.g. F9" />
</label> </label>
<label> <label>
<span>pause</span> <span>pause</span>
<input bind:value={pause} placeholder="e.g. F10" /> <HotkeyInput bind:value={pause} placeholder="e.g. F10" />
</label> </label>
</div> </div>
<div class="row"> <div class="row">
@@ -54,8 +55,7 @@
</button> </button>
</div> </div>
<p class="hint"> <p class="hint">
Live key capture isn't wired up yet - for now type the combination as text Click each field, then press your combo. Escape cancels.
(F9, Ctrl+Shift+R, etc).
</p> </p>
</main> </main>
@@ -90,18 +90,6 @@
color: #aaa; color: #aaa;
font-size: 0.875rem; font-size: 0.875rem;
} }
input {
font: inherit;
padding: 0.45rem 0.6rem;
border: 1px solid #444;
background: #222;
color: inherit;
border-radius: 4px;
}
input:focus {
outline: none;
border-color: #4a8;
}
.row { .row {
display: flex; display: flex;
gap: 0.5rem; gap: 0.5rem;
+125
View File
@@ -0,0 +1,125 @@
<script lang="ts">
let {
value = $bindable(),
placeholder = "click to assign",
}: {
value: string | null
placeholder?: string
} = $props()
let capturing: boolean = $state(false)
let buttonEl: HTMLButtonElement | null = $state(null)
const MODIFIER_KEYS = new Set(["Control", "Shift", "Alt", "Meta", "ControlLeft", "ControlRight"])
function startCapture() {
capturing = true
buttonEl?.focus()
}
function handleKeydown(e: KeyboardEvent) {
if (!capturing) return
e.preventDefault()
e.stopPropagation()
if (e.key === "Escape") {
capturing = false
buttonEl?.blur()
return
}
if (MODIFIER_KEYS.has(e.key)) {
return
}
const parts: string[] = []
if (e.ctrlKey) parts.push("Ctrl")
if (e.altKey) parts.push("Alt")
if (e.shiftKey) parts.push("Shift")
if (e.metaKey) parts.push("Meta")
let key = e.key
if (key === " ") key = "Space"
else if (key.length === 1) key = key.toUpperCase()
parts.push(key)
value = parts.join("+")
capturing = false
buttonEl?.blur()
}
function handleBlur() {
capturing = false
}
function clear(e: MouseEvent) {
e.stopPropagation()
value = null
capturing = false
}
</script>
<div class="row">
<button
bind:this={buttonEl}
class:capturing
onclick={startCapture}
onkeydown={handleKeydown}
onblur={handleBlur}
type="button"
>
{#if capturing}
press a combo (Esc to cancel)
{:else if value}
{value}
{:else}
{placeholder}
{/if}
</button>
{#if value && !capturing}
<button class="clear" onclick={clear} type="button" title="clear">×</button>
{/if}
</div>
<style>
.row {
display: flex;
gap: 0.4rem;
align-items: stretch;
}
button {
font: inherit;
padding: 0.45rem 0.7rem;
border: 1px solid #444;
background: #222;
color: inherit;
border-radius: 4px;
cursor: pointer;
text-align: left;
flex: 1;
}
button:hover {
border-color: #888;
}
button.capturing {
border-color: #4a8;
background: #1a2a24;
color: #4a8;
outline: none;
}
button:focus {
outline: none;
border-color: #4a8;
}
button.clear {
flex: 0 0 auto;
padding: 0.45rem 0.7rem;
color: #888;
background: transparent;
border-color: #2a2a2a;
}
button.clear:hover {
color: #ec9a9a;
border-color: #ec9a9a;
}
</style>
+6 -20
View File
@@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte" import { onMount } from "svelte"
import { api, type Config } from "./api" import { api, type Config } from "./api"
import HotkeyInput from "./HotkeyInput.svelte"
let { onClose }: { onClose: () => void } = $props() let { onClose }: { onClose: () => void } = $props()
@@ -89,30 +90,15 @@
<h2>hotkeys</h2> <h2>hotkeys</h2>
<label> <label>
<span>start</span> <span>start</span>
<input <HotkeyInput bind:value={cfg.hotkeys.start} placeholder="click to assign" />
type="text"
placeholder="F9"
value={cfg.hotkeys.start ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.start = (e.currentTarget as HTMLInputElement).value || null)}
/>
</label> </label>
<label> <label>
<span>stop</span> <span>stop</span>
<input <HotkeyInput bind:value={cfg.hotkeys.stop} placeholder="click to assign" />
type="text"
placeholder="F9"
value={cfg.hotkeys.stop ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.stop = (e.currentTarget as HTMLInputElement).value || null)}
/>
</label> </label>
<label> <label>
<span>pause</span> <span>pause</span>
<input <HotkeyInput bind:value={cfg.hotkeys.pause} placeholder="click to assign" />
type="text"
placeholder="F10"
value={cfg.hotkeys.pause ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.pause = (e.currentTarget as HTMLInputElement).value || null)}
/>
</label> </label>
</section> </section>
@@ -177,7 +163,7 @@
label.check { label.check {
grid-template-columns: 20px 1fr; grid-template-columns: 20px 1fr;
} }
select, input[type="text"] { select {
font: inherit; font: inherit;
padding: 0.4rem 0.6rem; padding: 0.4rem 0.6rem;
border: 1px solid #444; border: 1px solid #444;
@@ -185,7 +171,7 @@
color: inherit; color: inherit;
border-radius: 4px; border-radius: 4px;
} }
input[type="text"]:focus, select:focus { select:focus {
outline: none; outline: none;
border-color: #4a8; border-color: #4a8;
} }