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.

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