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.

246 lines
10KB

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