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.

315 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. KeyPress::KeyPress() noexcept
  19. : keyCode (0),
  20. textCharacter (0)
  21. {
  22. }
  23. KeyPress::KeyPress (const int code, ModifierKeys m,
  24. const juce_wchar textChar) noexcept
  25. : keyCode (code),
  26. mods (m),
  27. textCharacter (textChar)
  28. {
  29. }
  30. KeyPress::KeyPress (const int code) noexcept
  31. : keyCode (code),
  32. textCharacter (0)
  33. {
  34. }
  35. KeyPress::KeyPress (const KeyPress& other) noexcept
  36. : keyCode (other.keyCode),
  37. mods (other.mods),
  38. textCharacter (other.textCharacter)
  39. {
  40. }
  41. KeyPress& KeyPress::operator= (const KeyPress& other) noexcept
  42. {
  43. keyCode = other.keyCode;
  44. mods = other.mods;
  45. textCharacter = other.textCharacter;
  46. return *this;
  47. }
  48. bool KeyPress::operator== (int otherKeyCode) const noexcept
  49. {
  50. return keyCode == otherKeyCode && ! mods.isAnyModifierKeyDown();
  51. }
  52. bool KeyPress::operator== (const KeyPress& other) const noexcept
  53. {
  54. return mods.getRawFlags() == other.mods.getRawFlags()
  55. && (textCharacter == other.textCharacter
  56. || textCharacter == 0
  57. || other.textCharacter == 0)
  58. && (keyCode == other.keyCode
  59. || (keyCode < 256
  60. && other.keyCode < 256
  61. && CharacterFunctions::toLowerCase ((juce_wchar) keyCode)
  62. == CharacterFunctions::toLowerCase ((juce_wchar) other.keyCode)));
  63. }
  64. bool KeyPress::operator!= (const KeyPress& other) const noexcept
  65. {
  66. return ! operator== (other);
  67. }
  68. bool KeyPress::operator!= (int otherKeyCode) const noexcept
  69. {
  70. return ! operator== (otherKeyCode);
  71. }
  72. bool KeyPress::isCurrentlyDown() const
  73. {
  74. return isKeyCurrentlyDown (keyCode)
  75. && (ModifierKeys::getCurrentModifiers().getRawFlags() & ModifierKeys::allKeyboardModifiers)
  76. == (mods.getRawFlags() & ModifierKeys::allKeyboardModifiers);
  77. }
  78. //==============================================================================
  79. namespace KeyPressHelpers
  80. {
  81. struct KeyNameAndCode
  82. {
  83. const char* name;
  84. int code;
  85. };
  86. const KeyNameAndCode translations[] =
  87. {
  88. { "spacebar", KeyPress::spaceKey },
  89. { "return", KeyPress::returnKey },
  90. { "escape", KeyPress::escapeKey },
  91. { "backspace", KeyPress::backspaceKey },
  92. { "cursor left", KeyPress::leftKey },
  93. { "cursor right", KeyPress::rightKey },
  94. { "cursor up", KeyPress::upKey },
  95. { "cursor down", KeyPress::downKey },
  96. { "page up", KeyPress::pageUpKey },
  97. { "page down", KeyPress::pageDownKey },
  98. { "home", KeyPress::homeKey },
  99. { "end", KeyPress::endKey },
  100. { "delete", KeyPress::deleteKey },
  101. { "insert", KeyPress::insertKey },
  102. { "tab", KeyPress::tabKey },
  103. { "play", KeyPress::playKey },
  104. { "stop", KeyPress::stopKey },
  105. { "fast forward", KeyPress::fastForwardKey },
  106. { "rewind", KeyPress::rewindKey }
  107. };
  108. struct ModifierDescription
  109. {
  110. const char* name;
  111. int flag;
  112. };
  113. static const ModifierDescription modifierNames[] =
  114. {
  115. { "ctrl", ModifierKeys::ctrlModifier },
  116. { "control", ModifierKeys::ctrlModifier },
  117. { "ctl", ModifierKeys::ctrlModifier },
  118. { "shift", ModifierKeys::shiftModifier },
  119. { "shft", ModifierKeys::shiftModifier },
  120. { "alt", ModifierKeys::altModifier },
  121. { "option", ModifierKeys::altModifier },
  122. { "command", ModifierKeys::commandModifier },
  123. { "cmd", ModifierKeys::commandModifier }
  124. };
  125. static const char* numberPadPrefix() noexcept { return "numpad "; }
  126. static int getNumpadKeyCode (const String& desc)
  127. {
  128. if (desc.containsIgnoreCase (numberPadPrefix()))
  129. {
  130. const juce_wchar lastChar = desc.trimEnd().getLastCharacter();
  131. switch (lastChar)
  132. {
  133. case '0': case '1': case '2': case '3': case '4':
  134. case '5': case '6': case '7': case '8': case '9':
  135. return (int) (KeyPress::numberPad0 + lastChar - '0');
  136. case '+': return KeyPress::numberPadAdd;
  137. case '-': return KeyPress::numberPadSubtract;
  138. case '*': return KeyPress::numberPadMultiply;
  139. case '/': return KeyPress::numberPadDivide;
  140. case '.': return KeyPress::numberPadDecimalPoint;
  141. case '=': return KeyPress::numberPadEquals;
  142. default: break;
  143. }
  144. if (desc.endsWith ("separator")) return KeyPress::numberPadSeparator;
  145. if (desc.endsWith ("delete")) return KeyPress::numberPadDelete;
  146. }
  147. return 0;
  148. }
  149. #if JUCE_MAC
  150. struct OSXSymbolReplacement
  151. {
  152. const char* text;
  153. juce_wchar symbol;
  154. };
  155. const OSXSymbolReplacement osxSymbols[] =
  156. {
  157. { "shift + ", 0x21e7 },
  158. { "command + ", 0x2318 },
  159. { "option + ", 0x2325 },
  160. { "ctrl + ", 0x2303 },
  161. { "return", 0x23ce },
  162. { "cursor left", 0x2190 },
  163. { "cursor right", 0x2192 },
  164. { "cursor up", 0x2191 },
  165. { "cursor down", 0x2193 },
  166. { "backspace", 0x232b },
  167. { "delete", 0x2326 }
  168. };
  169. #endif
  170. }
  171. //==============================================================================
  172. KeyPress KeyPress::createFromDescription (const String& desc)
  173. {
  174. int modifiers = 0;
  175. for (int i = 0; i < numElementsInArray (KeyPressHelpers::modifierNames); ++i)
  176. if (desc.containsWholeWordIgnoreCase (KeyPressHelpers::modifierNames[i].name))
  177. modifiers |= KeyPressHelpers::modifierNames[i].flag;
  178. int key = 0;
  179. for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
  180. {
  181. if (desc.containsWholeWordIgnoreCase (String (KeyPressHelpers::translations[i].name)))
  182. {
  183. key = KeyPressHelpers::translations[i].code;
  184. break;
  185. }
  186. }
  187. if (key == 0)
  188. key = KeyPressHelpers::getNumpadKeyCode (desc);
  189. if (key == 0)
  190. {
  191. // see if it's a function key..
  192. if (! desc.containsChar ('#')) // avoid mistaking hex-codes like "#f1"
  193. for (int i = 1; i <= 12; ++i)
  194. if (desc.containsWholeWordIgnoreCase ("f" + String (i)))
  195. key = F1Key + i - 1;
  196. if (key == 0)
  197. {
  198. // give up and use the hex code..
  199. const int hexCode = desc.fromFirstOccurrenceOf ("#", false, false)
  200. .retainCharacters ("0123456789abcdefABCDEF")
  201. .getHexValue32();
  202. if (hexCode > 0)
  203. key = hexCode;
  204. else
  205. key = (int) CharacterFunctions::toUpperCase (desc.getLastCharacter());
  206. }
  207. }
  208. return KeyPress (key, ModifierKeys (modifiers), 0);
  209. }
  210. String KeyPress::getTextDescription() const
  211. {
  212. String desc;
  213. if (keyCode > 0)
  214. {
  215. // some keyboard layouts use a shift-key to get the slash, but in those cases, we
  216. // want to store it as being a slash, not shift+whatever.
  217. if (textCharacter == '/')
  218. return "/";
  219. if (mods.isCtrlDown())
  220. desc << "ctrl + ";
  221. if (mods.isShiftDown())
  222. desc << "shift + ";
  223. #if JUCE_MAC
  224. if (mods.isAltDown())
  225. desc << "option + ";
  226. // only do this on the mac, because on Windows ctrl and command are the same,
  227. // and this would get confusing
  228. if (mods.isCommandDown())
  229. desc << "command + ";
  230. #else
  231. if (mods.isAltDown())
  232. desc << "alt + ";
  233. #endif
  234. for (int i = 0; i < numElementsInArray (KeyPressHelpers::translations); ++i)
  235. if (keyCode == KeyPressHelpers::translations[i].code)
  236. return desc + KeyPressHelpers::translations[i].name;
  237. if (keyCode >= F1Key && keyCode <= F16Key) desc << 'F' << (1 + keyCode - F1Key);
  238. else if (keyCode >= numberPad0 && keyCode <= numberPad9) desc << KeyPressHelpers::numberPadPrefix() << (keyCode - numberPad0);
  239. else if (keyCode >= 33 && keyCode < 176) desc += CharacterFunctions::toUpperCase ((juce_wchar) keyCode);
  240. else if (keyCode == numberPadAdd) desc << KeyPressHelpers::numberPadPrefix() << '+';
  241. else if (keyCode == numberPadSubtract) desc << KeyPressHelpers::numberPadPrefix() << '-';
  242. else if (keyCode == numberPadMultiply) desc << KeyPressHelpers::numberPadPrefix() << '*';
  243. else if (keyCode == numberPadDivide) desc << KeyPressHelpers::numberPadPrefix() << '/';
  244. else if (keyCode == numberPadSeparator) desc << KeyPressHelpers::numberPadPrefix() << "separator";
  245. else if (keyCode == numberPadDecimalPoint) desc << KeyPressHelpers::numberPadPrefix() << '.';
  246. else if (keyCode == numberPadDelete) desc << KeyPressHelpers::numberPadPrefix() << "delete";
  247. else desc << '#' << String::toHexString (keyCode);
  248. }
  249. return desc;
  250. }
  251. String KeyPress::getTextDescriptionWithIcons() const
  252. {
  253. #if JUCE_MAC
  254. String s (getTextDescription());
  255. for (int i = 0; i < numElementsInArray (KeyPressHelpers::osxSymbols); ++i)
  256. s = s.replace (KeyPressHelpers::osxSymbols[i].text,
  257. String::charToString (KeyPressHelpers::osxSymbols[i].symbol));
  258. return s;
  259. #else
  260. return getTextDescription();
  261. #endif
  262. }