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.

juce_KeyPressMappingSet.h 10KB

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