Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Manages and edits a list of keypresses, which it uses to invoke the appropriate
  18. command in an ApplicationCommandManager.
  19. Normally, you won't actually create a KeyPressMappingSet directly, because
  20. each ApplicationCommandManager contains its own KeyPressMappingSet, so typically
  21. you'd create yourself an ApplicationCommandManager, and call its
  22. ApplicationCommandManager::getKeyMappings() method to get a pointer to its
  23. KeyPressMappingSet.
  24. For one of these to actually use keypresses, you'll need to add it as a KeyListener
  25. to the top-level component for which you want to handle keystrokes. So for example:
  26. @code
  27. class MyMainWindow : public Component
  28. {
  29. ApplicationCommandManager* myCommandManager;
  30. public:
  31. MyMainWindow()
  32. {
  33. myCommandManager = new ApplicationCommandManager();
  34. // first, make sure the command manager has registered all the commands that its
  35. // targets can perform..
  36. myCommandManager->registerAllCommandsForTarget (myCommandTarget1);
  37. myCommandManager->registerAllCommandsForTarget (myCommandTarget2);
  38. // this will use the command manager to initialise the KeyPressMappingSet with
  39. // the default keypresses that were specified when the targets added their commands
  40. // to the manager.
  41. myCommandManager->getKeyMappings()->resetToDefaultMappings();
  42. // having set up the default key-mappings, you might now want to load the last set
  43. // of mappings that the user configured.
  44. myCommandManager->getKeyMappings()->restoreFromXml (lastSavedKeyMappingsXML);
  45. // Now tell our top-level window to send any keypresses that arrive to the
  46. // KeyPressMappingSet, which will use them to invoke the appropriate commands.
  47. addKeyListener (myCommandManager->getKeyMappings());
  48. }
  49. ...
  50. }
  51. @endcode
  52. KeyPressMappingSet derives from ChangeBroadcaster so that interested parties can
  53. register to be told when a command or mapping is added, removed, etc.
  54. There's also a UI component called KeyMappingEditorComponent that can be used
  55. to easily edit the key mappings.
  56. @see Component::addKeyListener(), KeyMappingEditorComponent, ApplicationCommandManager
  57. @tags{GUI}
  58. */
  59. class JUCE_API KeyPressMappingSet : public KeyListener,
  60. public ChangeBroadcaster,
  61. private FocusChangeListener
  62. {
  63. public:
  64. //==============================================================================
  65. /** Creates a KeyPressMappingSet for a given command manager.
  66. Normally, you won't actually create a KeyPressMappingSet directly, because
  67. each ApplicationCommandManager contains its own KeyPressMappingSet, so the
  68. best thing to do is to create your ApplicationCommandManager, and use the
  69. ApplicationCommandManager::getKeyMappings() method to access its mappings.
  70. When a suitable keypress happens, the manager's invoke() method will be
  71. used to invoke the appropriate command.
  72. @see ApplicationCommandManager
  73. */
  74. explicit KeyPressMappingSet (ApplicationCommandManager&);
  75. /** Creates an copy of a KeyPressMappingSet. */
  76. KeyPressMappingSet (const KeyPressMappingSet&);
  77. /** Destructor. */
  78. ~KeyPressMappingSet() override;
  79. //==============================================================================
  80. ApplicationCommandManager& getCommandManager() const noexcept { return commandManager; }
  81. //==============================================================================
  82. /** Returns a list of keypresses that are assigned to a particular command.
  83. @param commandID the command's ID
  84. */
  85. Array<KeyPress> getKeyPressesAssignedToCommand (CommandID commandID) const;
  86. /** Assigns a keypress to a command.
  87. If the keypress is already assigned to a different command, it will first be
  88. removed from that command, to avoid it triggering multiple functions.
  89. @param commandID the ID of the command that you want to add a keypress to. If
  90. this is 0, the keypress will be removed from anything that it
  91. was previously assigned to, but not re-assigned
  92. @param newKeyPress the new key-press
  93. @param insertIndex if this is less than zero, the key will be appended to the
  94. end of the list of keypresses; otherwise the new keypress will
  95. be inserted into the existing list at this index
  96. */
  97. void addKeyPress (CommandID commandID,
  98. const KeyPress& newKeyPress,
  99. int insertIndex = -1);
  100. /** Reset all mappings to the defaults, as dictated by the ApplicationCommandManager.
  101. @see resetToDefaultMapping
  102. */
  103. void resetToDefaultMappings();
  104. /** Resets all key-mappings to the defaults for a particular command.
  105. @see resetToDefaultMappings
  106. */
  107. void resetToDefaultMapping (CommandID commandID);
  108. /** Removes all keypresses that are assigned to any commands. */
  109. void clearAllKeyPresses();
  110. /** Removes all keypresses that are assigned to a particular command. */
  111. void clearAllKeyPresses (CommandID commandID);
  112. /** Removes one of the keypresses that are assigned to a command.
  113. See the getKeyPressesAssignedToCommand() for the list of keypresses to
  114. which the keyPressIndex refers.
  115. */
  116. void removeKeyPress (CommandID commandID, int keyPressIndex);
  117. /** Removes a keypress from any command that it may be assigned to. */
  118. void removeKeyPress (const KeyPress& keypress);
  119. /** Returns true if the given command is linked to this key. */
  120. bool containsMapping (CommandID commandID, const KeyPress& keyPress) const noexcept;
  121. //==============================================================================
  122. /** Looks for a command that corresponds to a keypress.
  123. @returns the UID of the command or 0 if none was found
  124. */
  125. CommandID findCommandForKeyPress (const KeyPress& keyPress) const noexcept;
  126. //==============================================================================
  127. /** Tries to recreate the mappings from a previously stored state.
  128. The XML passed in must have been created by the createXml() method.
  129. If the stored state makes any reference to commands that aren't
  130. currently available, these will be ignored.
  131. If the set of mappings being loaded was a set of differences (using createXml (true)),
  132. then this will call resetToDefaultMappings() and then merge the saved mappings
  133. on top. If the saved set was created with createXml (false), then this method
  134. will first clear all existing mappings and load the saved ones as a complete set.
  135. @returns true if it manages to load the XML correctly
  136. @see createXml
  137. */
  138. bool restoreFromXml (const XmlElement& xmlVersion);
  139. /** Creates an XML representation of the current mappings.
  140. This will produce a lump of XML that can be later reloaded using
  141. restoreFromXml() to recreate the current mapping state.
  142. @param saveDifferencesFromDefaultSet if this is false, then all keypresses
  143. will be saved into the XML. If it's true, then the XML will
  144. only store the differences between the current mappings and
  145. the default mappings you'd get from calling resetToDefaultMappings().
  146. The advantage of saving a set of differences from the default is that
  147. if you change the default mappings (in a new version of your app, for
  148. example), then these will be merged into a user's saved preferences.
  149. @see restoreFromXml
  150. */
  151. std::unique_ptr<XmlElement> createXml (bool saveDifferencesFromDefaultSet) const;
  152. //==============================================================================
  153. /** @internal */
  154. bool keyPressed (const KeyPress&, Component*) override;
  155. /** @internal */
  156. bool keyStateChanged (bool isKeyDown, Component*) override;
  157. /** @internal */
  158. void globalFocusChanged (Component*) override;
  159. private:
  160. //==============================================================================
  161. ApplicationCommandManager& commandManager;
  162. struct CommandMapping
  163. {
  164. CommandID commandID;
  165. Array<KeyPress> keypresses;
  166. bool wantsKeyUpDownCallbacks;
  167. };
  168. OwnedArray<CommandMapping> mappings;
  169. struct KeyPressTime
  170. {
  171. KeyPress key;
  172. uint32 timeWhenPressed;
  173. };
  174. OwnedArray<KeyPressTime> keysDown;
  175. void invokeCommand (const CommandID, const KeyPress&, const bool isKeyDown,
  176. const int millisecsSinceKeyPressed, Component* originator) const;
  177. KeyPressMappingSet& operator= (const KeyPressMappingSet&);
  178. JUCE_LEAK_DETECTOR (KeyPressMappingSet)
  179. };
  180. } // namespace juce