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">
import { api } from "./api"
import HotkeyInput from "./HotkeyInput.svelte"
let { onDone }: { onDone: () => void } = $props()
let start = $state("")
let stop = $state("")
let pause = $state("")
let saving = $state(false)
let start: string | null = $state(null)
let stop: string | null = $state(null)
let pause: string | null = $state(null)
let saving: boolean = $state(false)
async function save() {
saving = true
try {
const cfg = await api.getConfig()
cfg.hotkeys.start = start.trim() || null
cfg.hotkeys.stop = stop.trim() || null
cfg.hotkeys.pause = pause.trim() || null
cfg.hotkeys.start = start
cfg.hotkeys.stop = stop
cfg.hotkeys.pause = pause
await api.saveConfig(cfg)
onDone()
} finally {
@@ -22,7 +23,7 @@
}
}
let valid = $derived(start.trim() && stop.trim() && pause.trim())
let valid = $derived(!!start && !!stop && !!pause)
</script>
<main>
@@ -37,15 +38,15 @@
<div class="form">
<label>
<span>start</span>
<input bind:value={start} placeholder="e.g. F9" />
<HotkeyInput bind:value={start} placeholder="e.g. F9" />
</label>
<label>
<span>stop</span>
<input bind:value={stop} placeholder="e.g. F9" />
<HotkeyInput bind:value={stop} placeholder="e.g. F9" />
</label>
<label>
<span>pause</span>
<input bind:value={pause} placeholder="e.g. F10" />
<HotkeyInput bind:value={pause} placeholder="e.g. F10" />
</label>
</div>
<div class="row">
@@ -54,8 +55,7 @@
</button>
</div>
<p class="hint">
Live key capture isn't wired up yet - for now type the combination as text
(F9, Ctrl+Shift+R, etc).
Click each field, then press your combo. Escape cancels.
</p>
</main>
@@ -90,18 +90,6 @@
color: #aaa;
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 {
display: flex;
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">
import { onMount } from "svelte"
import { api, type Config } from "./api"
import HotkeyInput from "./HotkeyInput.svelte"
let { onClose }: { onClose: () => void } = $props()
@@ -89,30 +90,15 @@
<h2>hotkeys</h2>
<label>
<span>start</span>
<input
type="text"
placeholder="F9"
value={cfg.hotkeys.start ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.start = (e.currentTarget as HTMLInputElement).value || null)}
/>
<HotkeyInput bind:value={cfg.hotkeys.start} placeholder="click to assign" />
</label>
<label>
<span>stop</span>
<input
type="text"
placeholder="F9"
value={cfg.hotkeys.stop ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.stop = (e.currentTarget as HTMLInputElement).value || null)}
/>
<HotkeyInput bind:value={cfg.hotkeys.stop} placeholder="click to assign" />
</label>
<label>
<span>pause</span>
<input
type="text"
placeholder="F10"
value={cfg.hotkeys.pause ?? ""}
oninput={(e) => cfg && (cfg.hotkeys.pause = (e.currentTarget as HTMLInputElement).value || null)}
/>
<HotkeyInput bind:value={cfg.hotkeys.pause} placeholder="click to assign" />
</label>
</section>
@@ -177,7 +163,7 @@
label.check {
grid-template-columns: 20px 1fr;
}
select, input[type="text"] {
select {
font: inherit;
padding: 0.4rem 0.6rem;
border: 1px solid #444;
@@ -185,7 +171,7 @@
color: inherit;
border-radius: 4px;
}
input[type="text"]:focus, select:focus {
select:focus {
outline: none;
border-color: #4a8;
}