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.

juce_KeyPress.cpp 11KB

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