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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. Manages and edits a list of keypresses, which it uses to invoke the appropriate
  24. command in an ApplicationCommandManager.
  25. Normally, you won't actually create a KeyPressMappingSet directly, because
  26. each ApplicationCommandManager contains its own KeyPressMappingSet, so typically
  27. you'd create yourself an ApplicationCommandManager, and call its
  28. ApplicationCommandManager::getKeyMappings() method to get a pointer to its
  29. KeyPressMappingSet.
  30. For one of these to actually use keypresses, you'll need to add it as a KeyListener
  31. to the top-level component for which you want to handle keystrokes. So for example:
  32. @code
  33. class MyMainWindow : public Component
  34. {
  35. ApplicationCommandManager* myCommandManager;
  36. public:
  37. MyMainWindow()
  38. {
  39. myCommandManager = new ApplicationCommandManager();
  40. // first, make sure the command manager has registered all the commands that its
  41. // targets can perform..
  42. myCommandManager->registerAllCommandsForTarget (myCommandTarget1);
  43. myCommandManager->registerAllCommandsForTarget (myCommandTarget2);
  44. // this will use the command manager to initialise the KeyPressMappingSet with
  45. // the default keypresses that were specified when the targets added their commands
  46. // to the manager.
  47. myCommandManager->getKeyMappings()->resetToDefaultMappings();
  48. // having set up the default key-mappings, you might now want to load the last set
  49. // of mappings that the user configured.
  50. myCommandManager->getKeyMappings()->restoreFromXml (lastSavedKeyMappingsXML);
  51. // Now tell our top-level window to send any keypresses that arrive to the
  52. // KeyPressMappingSet, which will use them to invoke the appropriate commands.
  53. addKeyListener (myCommandManager->getKeyMappings());
  54. }
  55. ...
  56. }
  57. @endcode
  58. KeyPressMappingSet derives from ChangeBroadcaster so that interested parties can
  59. register to be told when a command or mapping is added, removed, etc.
  60. There's also a UI component called KeyMappingEditorComponent that can be used
  61. to easily edit the key mappings.
  62. @see Component::addKeyListener(), KeyMappingEditorComponent, ApplicationCommandManager
  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();
  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. The object that is returned must be deleted by the caller.
  148. @param saveDifferencesFromDefaultSet if this is false, then all keypresses
  149. will be saved into the XML. If it's true, then the XML will
  150. only store the differences between the current mappings and
  151. the default mappings you'd get from calling resetToDefaultMappings().
  152. The advantage of saving a set of differences from the default is that
  153. if you change the default mappings (in a new version of your app, for
  154. example), then these will be merged into a user's saved preferences.
  155. @see restoreFromXml
  156. */
  157. XmlElement* createXml (bool saveDifferencesFromDefaultSet) const;
  158. //==============================================================================
  159. /** @internal */
  160. bool keyPressed (const KeyPress&, Component*) override;
  161. /** @internal */
  162. bool keyStateChanged (bool isKeyDown, Component*) override;
  163. /** @internal */
  164. void globalFocusChanged (Component*) override;
  165. private:
  166. //==============================================================================
  167. ApplicationCommandManager& commandManager;
  168. struct CommandMapping
  169. {
  170. CommandID commandID;
  171. Array<KeyPress> keypresses;
  172. bool wantsKeyUpDownCallbacks;
  173. };
  174. OwnedArray<CommandMapping> mappings;
  175. struct KeyPressTime
  176. {
  177. KeyPress key;
  178. uint32 timeWhenPressed;
  179. };
  180. OwnedArray<KeyPressTime> keysDown;
  181. void invokeCommand (const CommandID, const KeyPress&, const bool isKeyDown,
  182. const int millisecsSinceKeyPressed, Component* originator) const;
  183. KeyPressMappingSet& operator= (const KeyPressMappingSet&);
  184. JUCE_LEAK_DETECTOR (KeyPressMappingSet)
  185. };
  186. } // namespace juce