The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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