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.

310 lines
11KB

  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. KeyPress::KeyPress() noexcept
  22. : keyCode (0), textCharacter (0)
  23. {
  24. }
  25. KeyPress::KeyPress (int code, ModifierKeys m, juce_wchar textChar) noexcept
  26. : keyCode (code), mods (m), textCharacter (textChar)
  27. {
  28. }
  29. KeyPress::KeyPress (const int code) noexcept
  30. : keyCode (code), textCharacter (0)
  31. {
  32. }
  33. KeyPress::KeyPress (const KeyPress& other) noexcept
  34. : keyCode (other.keyCode), mods (other.mods),
  35. textCharacter (other.textCharacter)
  36. {
  37. }
  38. KeyPress& KeyPress::operator= (const KeyPress& other) noexcept
  39. {
  40. keyCode = other.keyCode;
  41. mods = other.mods;
  42. textCharacter = other.textCharacter;
  43. return *this;
  44. }
  45. bool KeyPress::operator== (int otherKeyCode) const noexcept
  46. {
  47. return keyCode == otherKeyCode && ! mods.isAnyModifierKeyDown();
  48. }
  49. bool KeyPress::operator== (const KeyPress& other) const noexcept
  50. {
  51. return mods.getRawFlags() == other.mods.getRawFlags()
  52. && (textCharacter == other.textCharacter
  53. || textCharacter == 0
  54. || other.textCharacter == 0)
  55. && (keyCode == other.keyCode
  56. || (keyCode < 256
  57. && other.keyCode < 256
  58. && CharacterFunctions::toLowerCase ((juce_wchar) keyCode)
  59. == CharacterFunctions::toLowerCase ((juce_wchar) other.keyCode)));
  60. }
  61. bool KeyPress::operator!= (const KeyPress& other) const noexcept
  62. {
  63. return ! operator== (other);
  64. }
  65. bool KeyPress::operator!= (int otherKeyCode) const noexcept
  66. {
  67. return ! operator== (otherKeyCode);
  68. }
  69. bool KeyPress::isCurrentlyDown() const
  70. {
  71. return isKeyCurrentlyDown (keyCode)
  72. && (ModifierKeys::getCurrentModifiers().getRawFlags() & ModifierKeys::allKeyboardModifiers)
  73. == (mods.getRawFlags() & ModifierKeys::allKeyboardModifiers);
  74. }
  75. //==============================================================================
  76. namespace KeyPressHelpers
  77. {
  78. struct KeyNameAndCode
  79. {
  80. const char* name;
  81. int code;
  82. };
  83. const KeyNameAndCode translations[] =
  84. {
  85. { "spacebar", KeyPress::spaceKey },
  86. { "return", KeyPress::returnKey },
  87. { "escape", KeyPress::escapeKey },
  88. { "backspace", KeyPress::backspaceKey },
  89. { "cursor left", KeyPress::leftKey },
  90. { "cursor right", KeyPress::rightKey },
  91. { "cursor up", KeyPress::upKey },
  92. { "cursor down", KeyPress::downKey },
  93. { "page up", KeyPress::pageUpKey },
  94. { "page down", KeyPress::pageDownKey },
  95. { "home", KeyPress::homeKey },
  96. { "end", KeyPress::endKey },
  97. { "delete", KeyPress::deleteKey },
  98. { "insert", KeyPress::insertKey },
  99. { "tab", KeyPress::tabKey },
  100. { "play", KeyPress::playKey },
  101. { "stop", KeyPress::stopKey },
  102. { "fast forward", KeyPress::fastForwardKey },
  103. { "rewind", KeyPress::rewindKey }
  104. };
  105. struct ModifierDescription
  106. {
  107. const char* name;
  108. int flag;
  109. };
  110. static const ModifierDescription modifierNames[] =
  111. {
  112. { "ctrl", ModifierKeys::ctrlModifier },
  113. { "control", ModifierKeys::ctrlModifier },
  114. { "ctl", ModifierKeys::ctrlModifier },
  115. { "shift", ModifierKeys::shiftModifier },
  116. { "shft", ModifierKeys::shiftModifier },
  117. { "alt", ModifierKeys::altModifier },
  118. { "option", ModifierKeys::altModifier },
  119. { "command", ModifierKeys::commandModifier },
  120. { "cmd", ModifierKeys::commandModifier }
  121. };
  122. static const char* numberPadPrefix() noexcept { return "numpad "; }
  123. static int getNumpadKeyCode (const String& desc)
  124. {
  125. if (desc.containsIgnoreCase (numberPadPrefix()))
  126. {
  127. const juce_wchar lastChar = desc.trimEnd().getLastCharacter();
  128. switch (lastChar)
  129. {
  130. case '0': case '1': case '2': case '3': case '4':
  131. case '5': case '6': case '7': case '8': case '9':
  132. return (int) (KeyPress::numberPad0 + (int) lastChar - '0');
  133. case '+': return KeyPress::numberPadAdd;
  134. case '-': return KeyPress::numberPadSubtract;
  135. case '*': return KeyPress::numberPadMultiply;
  136. case '/': return KeyPress::numberPadDivide;
  137. case '.': return KeyPress::numberPadDecimalPoint;
  138. case '=': return KeyPress::numberPadEquals;
  139. default: break;
  140. }
  141. if (desc.endsWith ("separator")) return KeyPress::numberPadSeparator;
  142. if (desc.endsWith ("delete")) return KeyPress::numberPadDelete;
  143. }
  144. return 0;
  145. }
  146. #if JUCE_MAC
  147. struct OSXSymbolReplacement
  148. {
  149. const char* text;
  150. juce_wchar symbol;
  151. };
  152. const OSXSymbolReplacement osxSymbols[] =
  153. {
  154. { "shift + ", 0x21e7 },
  155. { "command + ", 0x2318 },
  156. { "option + ", 0x2325 },
  157. { "ctrl + ", 0x2303 },
  158. { "return", 0x21b5 },
  159. { "cursor left", 0x2190 },
  160. { "cursor right", 0x2192 },
  161. { "cursor up", 0x2191 },
  162. { "cursor down", 0x2193 },
  163. { "backspace", 0x232b },
  164. { "delete", 0x2326 },
  165. { "spacebar", 0x2423 }
  166. };
  167. #endif
  168. }
  169. //==============================================================================
  170. KeyPress KeyPress::createFromDescription (const String& desc)
  171. {
  172. int modifiers = 0;
  173. for (int i = 0; i < numElementsInArray (KeyPressHelpers::modifierNames); ++i)
  174. if (desc.containsWholeWordIgnoreCase (KeyPressHelpers::modifierNames[i].name))
  175. modifiers |= KeyPressHelpers::modifierNames[i].flag;
  176. int key = 0;
  177. for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
  178. {
  179. if (desc.containsWholeWordIgnoreCase (String (KeyPressHelpers::translations[i].name)))
  180. {
  181. key = KeyPressHelpers::translations[i].code;
  182. break;
  183. }
  184. }
  185. if (key == 0)
  186. key = KeyPressHelpers::getNumpadKeyCode (desc);
  187. if (key == 0)
  188. {
  189. // see if it's a function key..
  190. if (! desc.containsChar ('#')) // avoid mistaking hex-codes like "#f1"
  191. for (int i = 1; i <= 12; ++i)
  192. if (desc.containsWholeWordIgnoreCase ("f" + String (i)))
  193. key = F1Key + i - 1;
  194. if (key == 0)
  195. {
  196. // give up and use the hex code..
  197. const int hexCode = desc.fromFirstOccurrenceOf ("#", false, false)
  198. .retainCharacters ("0123456789abcdefABCDEF")
  199. .getHexValue32();
  200. if (hexCode > 0)
  201. key = hexCode;
  202. else
  203. key = (int) CharacterFunctions::toUpperCase (desc.getLastCharacter());
  204. }
  205. }
  206. return KeyPress (key, ModifierKeys (modifiers), 0);
  207. }
  208. String KeyPress::getTextDescription() const
  209. {
  210. String desc;
  211. if (keyCode > 0)
  212. {
  213. // some keyboard layouts use a shift-key to get the slash, but in those cases, we
  214. // want to store it as being a slash, not shift+whatever.
  215. if (textCharacter == '/' && keyCode != numberPadDivide)
  216. return "/";
  217. if (mods.isCtrlDown()) desc << "ctrl + ";
  218. if (mods.isShiftDown()) desc << "shift + ";
  219. #if JUCE_MAC
  220. if (mods.isAltDown()) desc << "option + ";
  221. if (mods.isCommandDown()) desc << "command + ";
  222. #else
  223. if (mods.isAltDown()) desc << "alt + ";
  224. #endif
  225. for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
  226. if (keyCode == KeyPressHelpers::translations[i].code)
  227. return desc + KeyPressHelpers::translations[i].name;
  228. // not all F keys have consecutive key codes on all platforms
  229. if (keyCode >= F1Key && keyCode <= F16Key) desc << 'F' << (1 + keyCode - F1Key);
  230. else if (keyCode >= F17Key && keyCode <= F24Key) desc << 'F' << (17 + keyCode - F17Key);
  231. else if (keyCode >= F25Key && keyCode <= F35Key) desc << 'F' << (25 + keyCode - F25Key);
  232. else if (keyCode >= numberPad0 && keyCode <= numberPad9) desc << KeyPressHelpers::numberPadPrefix() << (keyCode - numberPad0);
  233. else if (keyCode >= 33 && keyCode < 176) desc += CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
  234. else if (keyCode == numberPadAdd) desc << KeyPressHelpers::numberPadPrefix() << '+';
  235. else if (keyCode == numberPadSubtract) desc << KeyPressHelpers::numberPadPrefix() << '-';
  236. else if (keyCode == numberPadMultiply) desc << KeyPressHelpers::numberPadPrefix() << '*';
  237. else if (keyCode == numberPadDivide) desc << KeyPressHelpers::numberPadPrefix() << '/';
  238. else if (keyCode == numberPadSeparator) desc << KeyPressHelpers::numberPadPrefix() << "separator";
  239. else if (keyCode == numberPadDecimalPoint) desc << KeyPressHelpers::numberPadPrefix() << '.';
  240. else if (keyCode == numberPadDelete) desc << KeyPressHelpers::numberPadPrefix() << "delete";
  241. else desc << '#' << String::toHexString (keyCode);
  242. }
  243. return desc;
  244. }
  245. String KeyPress::getTextDescriptionWithIcons() const
  246. {
  247. #if JUCE_MAC
  248. String s (getTextDescription());
  249. for (int i = 0; i < numElementsInArray (KeyPressHelpers::osxSymbols); ++i)
  250. s = s.replace (KeyPressHelpers::osxSymbols[i].text,
  251. String::charToString (KeyPressHelpers::osxSymbols[i].symbol));
  252. return s;
  253. #else
  254. return getTextDescription();
  255. #endif
  256. }
  257. } // namespace juce