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.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. KeyPressMappingSet::KeyPressMappingSet (ApplicationCommandManager& cm)
  16. : commandManager (cm)
  17. {
  18. Desktop::getInstance().addFocusChangeListener (this);
  19. }
  20. KeyPressMappingSet::KeyPressMappingSet (const KeyPressMappingSet& other)
  21. : KeyListener(), ChangeBroadcaster(), FocusChangeListener(), commandManager (other.commandManager)
  22. {
  23. Desktop::getInstance().addFocusChangeListener (this);
  24. }
  25. KeyPressMappingSet::~KeyPressMappingSet()
  26. {
  27. Desktop::getInstance().removeFocusChangeListener (this);
  28. }
  29. //==============================================================================
  30. Array<KeyPress> KeyPressMappingSet::getKeyPressesAssignedToCommand (const CommandID commandID) const
  31. {
  32. for (int i = 0; i < mappings.size(); ++i)
  33. if (mappings.getUnchecked(i)->commandID == commandID)
  34. return mappings.getUnchecked (i)->keypresses;
  35. return {};
  36. }
  37. void KeyPressMappingSet::addKeyPress (const CommandID commandID, const KeyPress& newKeyPress, int insertIndex)
  38. {
  39. // If you specify an upper-case letter but no shift key, how is the user supposed to press it!?
  40. // Stick to lower-case letters when defining a keypress, to avoid ambiguity.
  41. jassert (! (CharacterFunctions::isUpperCase (newKeyPress.getTextCharacter())
  42. && ! newKeyPress.getModifiers().isShiftDown()));
  43. if (findCommandForKeyPress (newKeyPress) != commandID)
  44. {
  45. if (newKeyPress.isValid())
  46. {
  47. for (int i = mappings.size(); --i >= 0;)
  48. {
  49. if (mappings.getUnchecked(i)->commandID == commandID)
  50. {
  51. mappings.getUnchecked(i)->keypresses.insert (insertIndex, newKeyPress);
  52. sendChangeMessage();
  53. return;
  54. }
  55. }
  56. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID))
  57. {
  58. CommandMapping* const cm = new CommandMapping();
  59. cm->commandID = commandID;
  60. cm->keypresses.add (newKeyPress);
  61. cm->wantsKeyUpDownCallbacks = (ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) != 0;
  62. mappings.add (cm);
  63. sendChangeMessage();
  64. }
  65. else
  66. {
  67. // If you hit this, you're trying to attach a keypress to a command ID that
  68. // doesn't exist, so the key is not being attached.
  69. jassertfalse;
  70. }
  71. }
  72. }
  73. }
  74. static void addKeyPresses (KeyPressMappingSet& set, const ApplicationCommandInfo* const ci)
  75. {
  76. for (int j = 0; j < ci->defaultKeypresses.size(); ++j)
  77. set.addKeyPress (ci->commandID, ci->defaultKeypresses.getReference (j));
  78. }
  79. void KeyPressMappingSet::resetToDefaultMappings()
  80. {
  81. mappings.clear();
  82. for (int i = 0; i < commandManager.getNumCommands(); ++i)
  83. addKeyPresses (*this, commandManager.getCommandForIndex (i));
  84. sendChangeMessage();
  85. }
  86. void KeyPressMappingSet::resetToDefaultMapping (const CommandID commandID)
  87. {
  88. clearAllKeyPresses (commandID);
  89. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (commandID))
  90. addKeyPresses (*this, ci);
  91. }
  92. void KeyPressMappingSet::clearAllKeyPresses()
  93. {
  94. if (mappings.size() > 0)
  95. {
  96. sendChangeMessage();
  97. mappings.clear();
  98. }
  99. }
  100. void KeyPressMappingSet::clearAllKeyPresses (const CommandID commandID)
  101. {
  102. for (int i = mappings.size(); --i >= 0;)
  103. {
  104. if (mappings.getUnchecked(i)->commandID == commandID)
  105. {
  106. mappings.remove (i);
  107. sendChangeMessage();
  108. }
  109. }
  110. }
  111. void KeyPressMappingSet::removeKeyPress (const KeyPress& keypress)
  112. {
  113. if (keypress.isValid())
  114. {
  115. for (int i = mappings.size(); --i >= 0;)
  116. {
  117. CommandMapping& cm = *mappings.getUnchecked(i);
  118. for (int j = cm.keypresses.size(); --j >= 0;)
  119. {
  120. if (keypress == cm.keypresses [j])
  121. {
  122. cm.keypresses.remove (j);
  123. sendChangeMessage();
  124. }
  125. }
  126. }
  127. }
  128. }
  129. void KeyPressMappingSet::removeKeyPress (const CommandID commandID, const int keyPressIndex)
  130. {
  131. for (int i = mappings.size(); --i >= 0;)
  132. {
  133. if (mappings.getUnchecked(i)->commandID == commandID)
  134. {
  135. mappings.getUnchecked(i)->keypresses.remove (keyPressIndex);
  136. sendChangeMessage();
  137. break;
  138. }
  139. }
  140. }
  141. //==============================================================================
  142. CommandID KeyPressMappingSet::findCommandForKeyPress (const KeyPress& keyPress) const noexcept
  143. {
  144. for (int i = 0; i < mappings.size(); ++i)
  145. if (mappings.getUnchecked(i)->keypresses.contains (keyPress))
  146. return mappings.getUnchecked(i)->commandID;
  147. return 0;
  148. }
  149. bool KeyPressMappingSet::containsMapping (const CommandID commandID, const KeyPress& keyPress) const noexcept
  150. {
  151. for (int i = mappings.size(); --i >= 0;)
  152. if (mappings.getUnchecked(i)->commandID == commandID)
  153. return mappings.getUnchecked(i)->keypresses.contains (keyPress);
  154. return false;
  155. }
  156. void KeyPressMappingSet::invokeCommand (const CommandID commandID,
  157. const KeyPress& key,
  158. const bool isKeyDown,
  159. const int millisecsSinceKeyPressed,
  160. Component* const originatingComponent) const
  161. {
  162. ApplicationCommandTarget::InvocationInfo info (commandID);
  163. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromKeyPress;
  164. info.isKeyDown = isKeyDown;
  165. info.keyPress = key;
  166. info.millisecsSinceKeyPressed = millisecsSinceKeyPressed;
  167. info.originatingComponent = originatingComponent;
  168. commandManager.invoke (info, false);
  169. }
  170. //==============================================================================
  171. bool KeyPressMappingSet::restoreFromXml (const XmlElement& xmlVersion)
  172. {
  173. if (xmlVersion.hasTagName ("KEYMAPPINGS"))
  174. {
  175. if (xmlVersion.getBoolAttribute ("basedOnDefaults", true))
  176. {
  177. // if the XML was created as a set of differences from the default mappings,
  178. // (i.e. by calling createXml (true)), then we need to first restore the defaults.
  179. resetToDefaultMappings();
  180. }
  181. else
  182. {
  183. // if the XML was created calling createXml (false), then we need to clear all
  184. // the keys and treat the xml as describing the entire set of mappings.
  185. clearAllKeyPresses();
  186. }
  187. forEachXmlChildElement (xmlVersion, map)
  188. {
  189. const CommandID commandId = map->getStringAttribute ("commandId").getHexValue32();
  190. if (commandId != 0)
  191. {
  192. auto key = KeyPress::createFromDescription (map->getStringAttribute ("key"));
  193. if (map->hasTagName ("MAPPING"))
  194. {
  195. addKeyPress (commandId, key);
  196. }
  197. else if (map->hasTagName ("UNMAPPING"))
  198. {
  199. for (auto& m : mappings)
  200. if (m->commandID == commandId)
  201. m->keypresses.removeAllInstancesOf (key);
  202. }
  203. }
  204. }
  205. return true;
  206. }
  207. return false;
  208. }
  209. std::unique_ptr<XmlElement> KeyPressMappingSet::createXml (const bool saveDifferencesFromDefaultSet) const
  210. {
  211. std::unique_ptr<KeyPressMappingSet> defaultSet;
  212. if (saveDifferencesFromDefaultSet)
  213. {
  214. defaultSet = std::make_unique<KeyPressMappingSet> (commandManager);
  215. defaultSet->resetToDefaultMappings();
  216. }
  217. auto doc = std::make_unique<XmlElement> ("KEYMAPPINGS");
  218. doc->setAttribute ("basedOnDefaults", saveDifferencesFromDefaultSet);
  219. for (int i = 0; i < mappings.size(); ++i)
  220. {
  221. auto& cm = *mappings.getUnchecked(i);
  222. for (int j = 0; j < cm.keypresses.size(); ++j)
  223. {
  224. if (defaultSet == nullptr
  225. || ! defaultSet->containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  226. {
  227. auto map = doc->createNewChildElement ("MAPPING");
  228. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  229. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  230. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  231. }
  232. }
  233. }
  234. if (defaultSet != nullptr)
  235. {
  236. for (int i = 0; i < defaultSet->mappings.size(); ++i)
  237. {
  238. auto& cm = *defaultSet->mappings.getUnchecked(i);
  239. for (int j = 0; j < cm.keypresses.size(); ++j)
  240. {
  241. if (! containsMapping (cm.commandID, cm.keypresses.getReference (j)))
  242. {
  243. auto map = doc->createNewChildElement ("UNMAPPING");
  244. map->setAttribute ("commandId", String::toHexString ((int) cm.commandID));
  245. map->setAttribute ("description", commandManager.getDescriptionOfCommand (cm.commandID));
  246. map->setAttribute ("key", cm.keypresses.getReference (j).getTextDescription());
  247. }
  248. }
  249. }
  250. }
  251. return doc;
  252. }
  253. //==============================================================================
  254. bool KeyPressMappingSet::keyPressed (const KeyPress& key, Component* const originatingComponent)
  255. {
  256. bool commandWasDisabled = false;
  257. for (int i = 0; i < mappings.size(); ++i)
  258. {
  259. CommandMapping& cm = *mappings.getUnchecked(i);
  260. if (cm.keypresses.contains (key))
  261. {
  262. if (const ApplicationCommandInfo* const ci = commandManager.getCommandForID (cm.commandID))
  263. {
  264. if ((ci->flags & ApplicationCommandInfo::wantsKeyUpDownCallbacks) == 0)
  265. {
  266. ApplicationCommandInfo info (0);
  267. if (commandManager.getTargetForCommand (cm.commandID, info) != nullptr)
  268. {
  269. if ((info.flags & ApplicationCommandInfo::isDisabled) == 0)
  270. {
  271. invokeCommand (cm.commandID, key, true, 0, originatingComponent);
  272. return true;
  273. }
  274. commandWasDisabled = true;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. if (originatingComponent != nullptr && commandWasDisabled)
  281. originatingComponent->getLookAndFeel().playAlertSound();
  282. return false;
  283. }
  284. bool KeyPressMappingSet::keyStateChanged (const bool /*isKeyDown*/, Component* originatingComponent)
  285. {
  286. bool used = false;
  287. const uint32 now = Time::getMillisecondCounter();
  288. for (int i = mappings.size(); --i >= 0;)
  289. {
  290. CommandMapping& cm = *mappings.getUnchecked(i);
  291. if (cm.wantsKeyUpDownCallbacks)
  292. {
  293. for (int j = cm.keypresses.size(); --j >= 0;)
  294. {
  295. const KeyPress key (cm.keypresses.getReference (j));
  296. const bool isDown = key.isCurrentlyDown();
  297. int keyPressEntryIndex = 0;
  298. bool wasDown = false;
  299. for (int k = keysDown.size(); --k >= 0;)
  300. {
  301. if (key == keysDown.getUnchecked(k)->key)
  302. {
  303. keyPressEntryIndex = k;
  304. wasDown = true;
  305. used = true;
  306. break;
  307. }
  308. }
  309. if (isDown != wasDown)
  310. {
  311. int millisecs = 0;
  312. if (isDown)
  313. {
  314. KeyPressTime* const k = new KeyPressTime();
  315. k->key = key;
  316. k->timeWhenPressed = now;
  317. keysDown.add (k);
  318. }
  319. else
  320. {
  321. const uint32 pressTime = keysDown.getUnchecked (keyPressEntryIndex)->timeWhenPressed;
  322. if (now > pressTime)
  323. millisecs = (int) (now - pressTime);
  324. keysDown.remove (keyPressEntryIndex);
  325. }
  326. invokeCommand (cm.commandID, key, isDown, millisecs, originatingComponent);
  327. used = true;
  328. }
  329. }
  330. }
  331. }
  332. return used;
  333. }
  334. void KeyPressMappingSet::globalFocusChanged (Component* focusedComponent)
  335. {
  336. if (focusedComponent != nullptr)
  337. focusedComponent->keyStateChanged (false);
  338. }
  339. } // namespace juce