KeyMap.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. // LYNX's key map: every keyboard shortcut lives in one remappable table (docs/WORKFLOW_FL.md,
  3. // P1 item 14). Platform-neutral (no JUCE): the UI converts its key events into a Chord, asks
  4. // the map which action it triggers in the focused panel, and runs it.
  5. //
  6. // Typing piano: while it is on, the unmodified keys of the piano layout play notes and win over any
  7. // shortcut bound to them; other plain printable keys only work for actions marked `whilePiano`.
  8. // Modified chords (Ctrl/Alt), F-keys and the non-printable keys (Space, Enter, arrows, Delete...)
  9. // always work.
  10. #include <cstdint>
  11. #include <optional>
  12. #include <string>
  13. #include <vector>
  14. #ifndef TR_NOOP
  15. #define TR_NOOP(text) text // marks a literal for translation (tools/i18n_keys.py scans core too)
  16. #endif
  17. namespace lynx::keys
  18. {
  19. // Key codes: printable keys use their upper-case ASCII code ('A', '1', '+'); the others these values.
  20. enum Key : int
  21. {
  22. space = ' ',
  23. f1 = 0x10001, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12,
  24. up = 0x10020, down, left, right, pageUp, pageDown, home, end,
  25. enter = 0x10030, escape, tab, backspace, del, insert,
  26. numpadPlus = 0x10040, numpadMinus
  27. };
  28. enum Modifier : std::uint8_t
  29. {
  30. none = 0,
  31. ctrl = 1,
  32. shift = 2,
  33. alt = 4
  34. };
  35. struct Chord
  36. {
  37. int key = 0;
  38. std::uint8_t mods = none;
  39. bool operator== (const Chord&) const = default;
  40. bool isPrintable() const noexcept; // a plain character key (letter, digit, punctuation), Space excluded
  41. bool isValid() const noexcept { return key != 0; }
  42. };
  43. // "Ctrl+Shift+F4", "Alt+Up", "Numpad +", "Space"... (English key names, used in the JSON file and menus)
  44. std::string toString (const Chord& chord);
  45. std::optional<Chord> parseChord (const std::string& text);
  46. // Where a binding applies. Global bindings work everywhere; the others only while that panel has focus
  47. // (and win over a global binding of the same chord).
  48. enum class Context : std::uint8_t { global, channelRack, pianoRoll, playlist, mixer, browser };
  49. struct Action
  50. {
  51. const char* id; // stable identifier ("pattern.next"), the key of the JSON file
  52. const char* description; // English, translated by the UI
  53. Context context;
  54. std::vector<Chord> defaults;
  55. bool whilePiano = false; // a plain printable key that keeps working while the typing piano is on
  56. };
  57. // The built-in actions with LYNX's default chords.
  58. const std::vector<Action>& actions();
  59. const Action* findAction (const std::string& id);
  60. // 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
  61. // 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.
  62. int typingPianoNote (int key) noexcept;
  63. class KeyMap
  64. {
  65. public:
  66. KeyMap(); // the defaults
  67. // Action triggered by `chord` with `focus` focused; "" for none. `typingPiano`: the piano is on.
  68. std::string find (const Chord& chord, Context focus, bool typingPiano) const;
  69. // True when the chord plays a note (piano on, unmodified, mapped key).
  70. static bool playsNote (const Chord& chord, bool typingPiano) noexcept;
  71. const std::vector<Chord>& chordsFor (const std::string& actionId) const;
  72. void setChords (const std::string& actionId, std::vector<Chord> chords); // [] = unbound
  73. void resetToDefaults();
  74. // Overrides from a JSON object {"action.id": "Ctrl+L", "other.id": ["F4", "Shift+F4"], "x": ""}.
  75. // Unknown ids and unreadable chords are reported in `errors` and skipped. Returns false if the
  76. // text is not a JSON object.
  77. bool loadJson (const std::string& json, std::vector<std::string>* errors = nullptr);
  78. std::string toJson() const; // every action with its current chords
  79. // Pairs of actions bound to the same chord in overlapping contexts (for the help sheet / tests).
  80. std::vector<std::pair<std::string, std::string>> conflicts() const;
  81. private:
  82. std::vector<std::vector<Chord>> chords; // parallel to actions()
  83. };
  84. } // namespace lynx::keys