| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #pragma once
- // LYNX's key map: every keyboard shortcut lives in one remappable table (docs/WORKFLOW_FL.md,
- // P1 item 14). Platform-neutral (no JUCE): the UI converts its key events into a Chord, asks
- // the map which action it triggers in the focused panel, and runs it.
- //
- // Typing piano: while it is on, the unmodified keys of the piano layout play notes and win over any
- // shortcut bound to them; other plain printable keys only work for actions marked `whilePiano`.
- // Modified chords (Ctrl/Alt), F-keys and the non-printable keys (Space, Enter, arrows, Delete...)
- // always work.
- #include <cstdint>
- #include <optional>
- #include <string>
- #include <vector>
- #ifndef TR_NOOP
- #define TR_NOOP(text) text // marks a literal for translation (tools/i18n_keys.py scans core too)
- #endif
- namespace lynx::keys
- {
- // Key codes: printable keys use their upper-case ASCII code ('A', '1', '+'); the others these values.
- enum Key : int
- {
- space = ' ',
- f1 = 0x10001, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
- up = 0x10020, down, left, right, pageUp, pageDown, home, end,
- enter = 0x10030, escape, tab, backspace, del, insert,
- numpadPlus = 0x10040, numpadMinus
- };
- enum Modifier : std::uint8_t
- {
- none = 0,
- ctrl = 1,
- shift = 2,
- alt = 4
- };
- struct Chord
- {
- int key = 0;
- std::uint8_t mods = none;
- bool operator== (const Chord&) const = default;
- bool isPrintable() const noexcept; // a plain character key (letter, digit, punctuation), Space excluded
- bool isValid() const noexcept { return key != 0; }
- };
- // "Ctrl+Shift+F4", "Alt+Up", "Numpad +", "Space"... (English key names, used in the JSON file and menus)
- std::string toString (const Chord& chord);
- std::optional<Chord> parseChord (const std::string& text);
- // Where a binding applies. Global bindings work everywhere; the others only while that panel has focus
- // (and win over a global binding of the same chord).
- enum class Context : std::uint8_t { global, channelRack, pianoRoll, playlist, mixer, browser };
- struct Action
- {
- const char* id; // stable identifier ("pattern.next"), the key of the JSON file
- const char* description; // English, translated by the UI
- Context context;
- std::vector<Chord> defaults;
- bool whilePiano = false; // a plain printable key that keeps working while the typing piano is on
- };
- // The built-in actions with LYNX's default chords.
- const std::vector<Action>& actions();
- const Action* findAction (const std::string& id);
- // PC keyboard as a piano: MIDI note of a key (upper-case code), or -1. Two rows from C5 (Z S X D C V G
- // B H N J M) and from C6 (Q 2 W 3 E R 5 T 6 Y 7 U I 9 O 0 P), with C5 = MIDI 60.
- int typingPianoNote (int key) noexcept;
- class KeyMap
- {
- public:
- KeyMap(); // the defaults
- // Action triggered by `chord` with `focus` focused; "" for none. `typingPiano`: the piano is on.
- std::string find (const Chord& chord, Context focus, bool typingPiano) const;
- // True when the chord plays a note (piano on, unmodified, mapped key).
- static bool playsNote (const Chord& chord, bool typingPiano) noexcept;
- const std::vector<Chord>& chordsFor (const std::string& actionId) const;
- void setChords (const std::string& actionId, std::vector<Chord> chords); // [] = unbound
- void resetToDefaults();
- // Overrides from a JSON object {"action.id": "Ctrl+L", "other.id": ["F4", "Shift+F4"], "x": ""}.
- // Unknown ids and unreadable chords are reported in `errors` and skipped. Returns false if the
- // text is not a JSON object.
- bool loadJson (const std::string& json, std::vector<std::string>* errors = nullptr);
- std::string toJson() const; // every action with its current chords
- // Pairs of actions bound to the same chord in overlapping contexts (for the help sheet / tests).
- std::vector<std::pair<std::string, std::string>> conflicts() const;
- private:
- std::vector<std::vector<Chord>> chords; // parallel to actions()
- };
- } // namespace lynx::keys
|