feat: use keycodes on CCX

resolves #71
This commit is contained in:
2023-12-29 13:48:34 +01:00
parent c1b1068c4b
commit a3857843d6
5 changed files with 29 additions and 8 deletions

View File

@@ -66,6 +66,7 @@
.dynamic { .dynamic {
padding: 4px; padding: 4px;
border-radius: 1px; border-radius: 1px;
min-width: 8px;
background: var(--md-sys-color-surface-variant); background: var(--md-sys-color-surface-variant);
&.inline { &.inline {

View File

@@ -20,6 +20,12 @@ export const KEYMAP_CODES: Record<number, KeyInfo> = Object.fromEntries(
), ),
) )
export const KEYMAP_KEYCODES: Map<string, number> = new Map(
KEYMAP_CATEGORIES.flatMap(category =>
Object.entries(category.actions).map(([code, action]) => [action.keyCode!, Number(code)] as const),
).filter(([keyCode]) => keyCode !== undefined),
)
export const KEYMAP_IDS: Map<string, KeyInfo> = new Map( export const KEYMAP_IDS: Map<string, KeyInfo> = new Map(
KEYMAP_CATEGORIES.flatMap(category => KEYMAP_CATEGORIES.flatMap(category =>
Object.entries(category.actions).map( Object.entries(category.actions).map(

View File

@@ -1,11 +1,13 @@
<script lang="ts"> <script lang="ts">
import {KEYMAP_IDS} from "$lib/serial/keymap-codes"
import type {ChordInfo} from "$lib/undo-redo" import type {ChordInfo} from "$lib/undo-redo"
import {changes, ChangeType} from "$lib/undo-redo" import {changes, ChangeType} from "$lib/undo-redo"
import {createEventDispatcher} from "svelte" import {createEventDispatcher} from "svelte"
import LL from "../../../i18n/i18n-svelte" import LL from "../../../i18n/i18n-svelte"
import ActionString from "$lib/components/ActionString.svelte" import ActionString from "$lib/components/ActionString.svelte"
import {selectAction} from "./action-selector" import {selectAction} from "./action-selector"
import {serialPort} from "$lib/serial/connection"
import {get} from "svelte/store"
import {inputToAction} from "./input-converter"
export let chord: ChordInfo | undefined = undefined export let chord: ChordInfo | undefined = undefined
@@ -26,7 +28,7 @@
function keydown(event: KeyboardEvent) { function keydown(event: KeyboardEvent) {
if (!editing) return if (!editing) return
event.preventDefault() event.preventDefault()
pressedKeys.add(KEYMAP_IDS.get(event.key)!.code) pressedKeys.add(inputToAction(event, get(serialPort)?.device === "X")!)
pressedKeys = pressedKeys pressedKeys = pressedKeys
} }

View File

@@ -7,6 +7,9 @@
import {scale} from "svelte/transition" import {scale} from "svelte/transition"
import ActionString from "$lib/components/ActionString.svelte" import ActionString from "$lib/components/ActionString.svelte"
import {selectAction} from "./action-selector" import {selectAction} from "./action-selector"
import {inputToAction} from "./input-converter"
import {serialPort} from "$lib/serial/connection"
import {get} from "svelte/store"
export let chord: ChordInfo export let chord: ChordInfo
@@ -22,14 +25,14 @@
moveCursor(cursorPosition - 1) moveCursor(cursorPosition - 1)
} else if (event.key === "Delete") { } else if (event.key === "Delete") {
deleteAction(cursorPosition) deleteAction(cursorPosition)
} else if (KEYMAP_IDS.has(event.key)) { } else {
insertAction(cursorPosition, KEYMAP_IDS.get(event.key)!.code) const action = inputToAction(event, get(serialPort)?.device === "X")
tick().then(() => moveCursor(cursorPosition + 1)) if (action !== undefined) {
} else if (specialKeycodes.has(event.key)) { insertAction(cursorPosition, action)
insertAction(cursorPosition, specialKeycodes.get(event.key)!)
tick().then(() => moveCursor(cursorPosition + 1)) tick().then(() => moveCursor(cursorPosition + 1))
} }
} }
}
function moveCursor(to: number) { function moveCursor(to: number) {
cursorPosition = Math.max(0, Math.min(to, chord.phrase.length)) cursorPosition = Math.max(0, Math.min(to, chord.phrase.length))

View File

@@ -0,0 +1,9 @@
import {KEYMAP_IDS, KEYMAP_KEYCODES, specialKeycodes} from "$lib/serial/keymap-codes"
export function inputToAction(event: KeyboardEvent, useKeycodes?: boolean): number | undefined {
if (useKeycodes) {
return KEYMAP_KEYCODES.get(event.code)
} else {
return KEYMAP_IDS.get(event.key)?.code ?? specialKeycodes.get(event.key)
}
}