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.

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