refactor: simplify key capture switch statement
- commit
- 677e3c0376ec2346e50e27ef87ee4ee3da5a4d04
- author
- brice · · 2026-01-24 16:34 UTC
- parent
- 026a7ae
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +12 -35
M webui/src/pages/config/KeyCaptureModal.svelte +12 -35
| @@ -46,43 +46,20 @@ | |||
| 46 | 46 | if (e.ctrlKey) parts.push("ctrl"); | |
| 47 | 47 | if (e.altKey) parts.push("alt"); | |
| 48 | 48 | ||
| 49 | - | let key = e.key.toLowerCase(); | |
| 50 | - | switch (key) { | |
| 51 | - | case "control": | |
| 52 | - | case "alt": | |
| 53 | - | case "shift": | |
| 54 | - | case "meta": | |
| 55 | - | return; // Modifier only, wait for actual key | |
| 56 | - | case "arrowleft": | |
| 57 | - | key = "arrowleft"; | |
| 58 | - | break; | |
| 59 | - | case "arrowright": | |
| 60 | - | key = "arrowright"; | |
| 61 | - | break; | |
| 62 | - | case "arrowup": | |
| 63 | - | key = "arrowup"; | |
| 64 | - | break; | |
| 65 | - | case "arrowdown": | |
| 66 | - | key = "arrowdown"; | |
| 67 | - | break; | |
| 68 | - | case "escape": | |
| 69 | - | // Escape closes the modal entirely | |
| 70 | - | onCancel(); | |
| 71 | - | return; | |
| 72 | - | case " ": | |
| 73 | - | key = "space"; | |
| 74 | - | break; | |
| 75 | - | case "enter": | |
| 76 | - | key = "enter"; | |
| 77 | - | break; | |
| 78 | - | case "tab": | |
| 79 | - | key = "tab"; | |
| 80 | - | break; | |
| 81 | - | case "backspace": | |
| 82 | - | key = "backspace"; | |
| 83 | - | break; | |
| 49 | + | // Ignore modifier-only presses | |
| 50 | + | if (["control", "alt", "shift", "meta"].includes(e.key.toLowerCase())) { | |
| 51 | + | return; | |
| 84 | 52 | } | |
| 85 | 53 | ||
| 54 | + | // Escape closes the modal | |
| 55 | + | if (e.key === "Escape") { | |
| 56 | + | onCancel(); | |
| 57 | + | return; | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | // Normalize key name | |
| 61 | + | const key = e.key === " " ? "space" : e.key.toLowerCase(); | |
| 62 | + | ||
| 86 | 63 | // Only add shift modifier for letter keys and non-symbol keys | |
| 87 | 64 | // Symbol keys like { } + etc. are already the shifted result | |
| 88 | 65 | if (e.shiftKey && !SHIFTED_SYMBOLS.has(e.key)) { | |
| @@ -46,43 +46,20 @@ | |||||
| 46 | if (e.ctrlKey) parts.push("ctrl"); | 46 | if (e.ctrlKey) parts.push("ctrl"); | ||
| 47 | if (e.altKey) parts.push("alt"); | 47 | if (e.altKey) parts.push("alt"); | ||
| 48 | 48 | ||||
| 49 | - | let key = e.key.toLowerCase(); | 49 | + | // Ignore modifier-only presses |
| 50 | - | switch (key) { | 50 | + | if (["control", "alt", "shift", "meta"].includes(e.key.toLowerCase())) { |
| 51 | - | case "control": | 51 | + | return; |
| 52 | - | case "alt": | |||
| 53 | - | case "shift": | |||
| 54 | - | case "meta": | |||
| 55 | - | return; // Modifier only, wait for actual key | |||
| 56 | - | case "arrowleft": | |||
| 57 | - | key = "arrowleft"; | |||
| 58 | - | break; | |||
| 59 | - | case "arrowright": | |||
| 60 | - | key = "arrowright"; | |||
| 61 | - | break; | |||
| 62 | - | case "arrowup": | |||
| 63 | - | key = "arrowup"; | |||
| 64 | - | break; | |||
| 65 | - | case "arrowdown": | |||
| 66 | - | key = "arrowdown"; | |||
| 67 | - | break; | |||
| 68 | - | case "escape": | |||
| 69 | - | // Escape closes the modal entirely | |||
| 70 | - | onCancel(); | |||
| 71 | - | return; | |||
| 72 | - | case " ": | |||
| 73 | - | key = "space"; | |||
| 74 | - | break; | |||
| 75 | - | case "enter": | |||
| 76 | - | key = "enter"; | |||
| 77 | - | break; | |||
| 78 | - | case "tab": | |||
| 79 | - | key = "tab"; | |||
| 80 | - | break; | |||
| 81 | - | case "backspace": | |||
| 82 | - | key = "backspace"; | |||
| 83 | - | break; | |||
| 84 | } | 52 | } | ||
| 85 | 53 | ||||
| 54 | + | // Escape closes the modal | |||
| 55 | + | if (e.key === "Escape") { | |||
| 56 | + | onCancel(); | |||
| 57 | + | return; | |||
| 58 | + | } | |||
| 59 | + | ||||
| 60 | + | // Normalize key name | |||
| 61 | + | const key = e.key === " " ? "space" : e.key.toLowerCase(); | |||
| 62 | + | ||||
| 86 | // Only add shift modifier for letter keys and non-symbol keys | 63 | // Only add shift modifier for letter keys and non-symbol keys | ||
| 87 | // Symbol keys like { } + etc. are already the shifted result | 64 | // Symbol keys like { } + etc. are already the shifted result | ||
| 88 | if (e.shiftKey && !SHIFTED_SYMBOLS.has(e.key)) { | 65 | if (e.shiftKey && !SHIFTED_SYMBOLS.has(e.key)) { | ||