Add computer-keyboard control mapping
Bind letter/digit keys (with Shift/Control/Option; Command reserved for menu shortcuts) to the same rig toggle Actions a MIDI Program-Change footswitch drives. Keys reuse MIDIController's perform(_:) dispatch via a new .key learn target and ingestKey(_:modifiers:). Key capture is platform-specific: macOS installs an AppKit NSEvent.addLocalMonitorForEvents(.keyDown) monitor (SwiftUI .onKeyPress only delivers to the focused view); iOS uses .onKeyPress with the root made focusable/focused, reclaiming focus when a text-field modal closes. Both gate on the first responder not being a text field so typing is never hijacked, filter key-repeat, and reserve Command for the menu. MIDIMap gains keys:[KeyBinding] and a KeyModifiers OptionSet, both with tolerant Codable decode so old rigs (no keys / no modifiers) still load with switches/ccs intact. The mapping sheet is unified: each switch row shows its footswitch (PC) and key bindings with per-source learn/clear, and the sheet is retitled "MIDI / Control Mapping". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
# Keyboard control mapping + unified mapping sheet
|
||||
|
||||
Bind computer letter keys to the same rig actions MIDI footswitches drive, and rework the
|
||||
MIDI mapping sheet so each rig action shows **all** of its trigger sources (footswitch,
|
||||
expression pedal, letter key) on one row.
|
||||
|
||||
## Goals
|
||||
|
||||
- A letter key (e.g. `b`) can toggle rig bypass / mute, a board solo/bypass, a pedal bypass,
|
||||
a looper transport action, the tuner, or tap tempo — exactly the toggle actions a
|
||||
Program-Change footswitch already drives.
|
||||
- Keys fire **only** when no text field is focused (rig rename, board label). Typing a rig
|
||||
name must never trip a bypass.
|
||||
- One mapping sheet row per action, showing its PC chip, KEY chip, and per-source learn.
|
||||
- Keys persist with the rig (travel in `ShoeGazeState`), like MIDI bindings.
|
||||
- macOS + iOS hardware keyboard.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- Keys driving continuous CC targets (knobs / levels). Keys are momentary → toggle actions
|
||||
only. CC rows in the sheet are unchanged.
|
||||
- `⌘`-modified bindings. Shift / Control / Option qualify a key binding (`a` vs `⇧a` vs `⌃a`),
|
||||
but Command is reserved for the existing menu `⌘`-shortcuts and is never bindable.
|
||||
|
||||
## Design
|
||||
|
||||
Reuse the existing controller. `MIDIController` already owns the shared `perform(_:)` action
|
||||
dispatch, the `learnTarget` state, the weak `shoeGaze`, and `onMapChanged`. Key learn and key
|
||||
apply reuse all of it — a separate controller would duplicate that. Key **event capture** is
|
||||
the only platform piece and lives in the UI module (SwiftUI `.onKeyPress`), per the module
|
||||
layering rule; the controller exposes a pure `ingestKey` entry point that the UI calls.
|
||||
|
||||
### Model — `MIDIMap`
|
||||
|
||||
Add a third binding list beside `switches` / `ccs`:
|
||||
|
||||
```swift
|
||||
public var keys: [KeyBinding]
|
||||
|
||||
public struct KeyBinding: Codable, Equatable, Sendable {
|
||||
public var key: String // single lowercased character
|
||||
public var action: Action
|
||||
public var displayNameHint: String?
|
||||
}
|
||||
```
|
||||
|
||||
`MIDIMap` gains a custom `init(from:)` that decodes `keys` with `decodeIfPresent ?? []`, so a
|
||||
rig saved before this feature (a `midiMap` with `switches`/`ccs` but no `keys`) still decodes
|
||||
— without it, synthesized Codable would fail the whole map and `ShoeGazeState` would silently
|
||||
drop existing MIDI bindings. `encode(to:)` stays synthesized.
|
||||
|
||||
### Controller — `MIDIController`
|
||||
|
||||
- `LearnTarget` gains `.key(MIDIMap.Action)`.
|
||||
- `armKeyLearn(_ action:)`, `keyBinding(for:)`, `clearKeyBinding(for:)` — mirror the switch API.
|
||||
- `ingestKey(_ key: String) -> Bool` — if armed for key learn, capture + clear learn; else run
|
||||
every action bound to that key via the shared `perform(_:)`. Returns `true` when the key was
|
||||
consumed (learned or matched a binding) so the UI can swallow the event; `false` lets the key
|
||||
fall through. Normalizes to a single lowercased letter/digit (non-printing keys rejected).
|
||||
`ingestKey` is `public` (no DEBUG gate needed — unlike MIDI messages there is no background-queue
|
||||
hop to bypass, so tests call it directly).
|
||||
|
||||
### UI capture — `ShoeGazeBuilderView`
|
||||
|
||||
Attach a focus-gated `.onKeyPress(phases: .down)` on the builder root. Handler:
|
||||
- Ignores repeats (hold = one toggle) and anything with command/control/option modifiers.
|
||||
- Extracts the single character, calls `controller.ingestKey`.
|
||||
- Returns `.handled` when consumed, else `.ignored`.
|
||||
|
||||
Focus gate: a focused `TextField` consumes keys first via the responder chain, so a root
|
||||
`.onKeyPress` never sees them while the user types. Sheets (rig rename, board editor) present
|
||||
their own text fields and take focus. No extra `@FocusState` plumbing needed for v1.
|
||||
|
||||
### UI sheet — `MIDIMappingSheet`
|
||||
|
||||
Switch rows gain a KEY chip (showing the bound letter, or nothing) and a keyboard-learn button
|
||||
beside the existing footswitch learn. Armed key-learn shows "Listening — press a key…". CC rows
|
||||
unchanged. Learn buttons: piano icon = MIDI learn, keyboard icon = key learn.
|
||||
|
||||
## Files
|
||||
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `Sources/LyrebirdShoeGaze/MIDI/MIDIMap.swift` | `KeyBinding` type, `keys` array, custom `init(from:)`. |
|
||||
| `Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift` | `.key` learn target, key learn/apply API, `ingestKey`. |
|
||||
| `Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift` | focus-gated `.onKeyPress` root modifier. |
|
||||
| `Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift` | KEY chip + keyboard-learn on switch rows. |
|
||||
| `Tests/LyrebirdShoeGazeTests/MIDIMapCodableTests.swift` | `keys` round-trip + old-rig (no `keys`) decode. |
|
||||
| `Tests/LyrebirdShoeGazeTests/MIDIControllerKeyTests.swift` | key learn / apply / clear / latch. |
|
||||
|
||||
## Test plan
|
||||
|
||||
- `MIDIMap` with `keys` encodes + decodes equal; a `midiMap` JSON lacking `keys` decodes with
|
||||
`keys == []` and its `switches`/`ccs` intact.
|
||||
- `armKeyLearn` + `ingestKey("b")` captures a `KeyBinding`, clears learn, fires `onMapChanged`,
|
||||
does not perform the action on the learn press.
|
||||
- A subsequent `ingestKey("b")` latches the action (e.g. rig bypass on/off).
|
||||
- `ingestKey` returns `false` for an unbound key; `clearKeyBinding` removes it.
|
||||
- Case/whitespace normalization: `ingestKey("B")` matches a binding learned as `b`.
|
||||
Reference in New Issue
Block a user