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.

271 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Represents a key press, including any modifier keys that are needed.
  18. E.g. a KeyPress might represent CTRL+C, SHIFT+ALT+H, Spacebar, Escape, etc.
  19. @see Component, KeyListener, KeyPressMappingSet, Button::addShortcut
  20. @tags{GUI}
  21. */
  22. class JUCE_API KeyPress
  23. {
  24. public:
  25. //==============================================================================
  26. /** Creates an (invalid) KeyPress.
  27. @see isValid
  28. */
  29. KeyPress() = default;
  30. /** Destructor. */
  31. ~KeyPress() = default;
  32. /** Creates a KeyPress for a key and some modifiers.
  33. e.g.
  34. CTRL+C would be: KeyPress ('c', ModifierKeys::ctrlModifier, 0)
  35. SHIFT+Escape would be: KeyPress (KeyPress::escapeKey, ModifierKeys::shiftModifier, 0)
  36. @param keyCode a code that represents the key - this value must be
  37. one of special constants listed in this class, or an
  38. 8-bit character code such as a letter (case is ignored),
  39. digit or a simple key like "," or ".". Note that this
  40. isn't the same as the textCharacter parameter, so for example
  41. a keyCode of 'a' and a shift-key modifier should have a
  42. textCharacter value of 'A'.
  43. @param modifiers the modifiers to associate with the keystroke
  44. @param textCharacter the character that would be printed if someone typed
  45. this keypress into a text editor. This value may be
  46. null if the keypress is a non-printing character
  47. @see getKeyCode, isKeyCode, getModifiers
  48. */
  49. KeyPress (int keyCode,
  50. ModifierKeys modifiers,
  51. juce_wchar textCharacter) noexcept;
  52. /** Creates a keypress with a keyCode but no modifiers or text character. */
  53. explicit KeyPress (int keyCode) noexcept;
  54. /** Creates a copy of another KeyPress. */
  55. KeyPress (const KeyPress&) = default;
  56. /** Copies this KeyPress from another one. */
  57. KeyPress& operator= (const KeyPress&) = default;
  58. /** Compares two KeyPress objects. */
  59. bool operator== (const KeyPress& other) const noexcept;
  60. /** Compares two KeyPress objects. */
  61. bool operator!= (const KeyPress& other) const noexcept;
  62. /** Returns true if this keypress is for the given keycode without any modifiers. */
  63. bool operator== (int keyCode) const noexcept;
  64. /** Returns true if this keypress is not for the given keycode without any modifiers. */
  65. bool operator!= (int keyCode) const noexcept;
  66. //==============================================================================
  67. /** Returns true if this is a valid KeyPress.
  68. A null keypress can be created by the default constructor, in case it's
  69. needed.
  70. */
  71. bool isValid() const noexcept { return keyCode != 0; }
  72. /** Returns the key code itself.
  73. This will either be one of the special constants defined in this class,
  74. or an 8-bit character code.
  75. */
  76. int getKeyCode() const noexcept { return keyCode; }
  77. /** Returns the key modifiers.
  78. @see ModifierKeys
  79. */
  80. ModifierKeys getModifiers() const noexcept { return mods; }
  81. /** Returns the character that is associated with this keypress.
  82. This is the character that you'd expect to see printed if you press this
  83. keypress in a text editor or similar component.
  84. */
  85. juce_wchar getTextCharacter() const noexcept { return textCharacter; }
  86. /** Checks whether the KeyPress's key is the same as the one provided, without checking
  87. the modifiers.
  88. The values for key codes can either be one of the special constants defined in
  89. this class, or an 8-bit character code.
  90. @see getKeyCode
  91. */
  92. bool isKeyCode (int keyCodeToCompare) const noexcept { return keyCode == keyCodeToCompare; }
  93. //==============================================================================
  94. /** Converts a textual key description to a KeyPress.
  95. This attempts to decode a textual version of a keypress, e.g. "ctrl + c" or "spacebar".
  96. This isn't designed to cope with any kind of input, but should be given the
  97. strings that are created by the getTextDescription() method.
  98. If the string can't be parsed, the object returned will be invalid.
  99. @see getTextDescription
  100. */
  101. static KeyPress createFromDescription (const String& textVersion);
  102. /** Creates a textual description of the key combination.
  103. e.g. "ctrl + c" or "delete".
  104. To store a keypress in a file, use this method, along with createFromDescription()
  105. to retrieve it later.
  106. */
  107. String getTextDescription() const;
  108. /** Creates a textual description of the key combination, using unicode icon symbols if possible.
  109. On OSX, this uses the Apple symbols for command, option, shift, etc, instead of the textual
  110. modifier key descriptions that are returned by getTextDescription()
  111. */
  112. String getTextDescriptionWithIcons() const;
  113. //==============================================================================
  114. /** Checks whether the user is currently holding down the keys that make up this
  115. KeyPress.
  116. Note that this will return false if any extra modifier keys are
  117. down - e.g. if the keypress is CTRL+X and the user is actually holding CTRL+ALT+x
  118. then it will be false.
  119. */
  120. bool isCurrentlyDown() const;
  121. /** Checks whether a particular key is held down, irrespective of modifiers.
  122. The values for key codes can either be one of the special constants defined in
  123. this class, or an 8-bit character code.
  124. */
  125. static bool isKeyCurrentlyDown (int keyCode);
  126. //==============================================================================
  127. // Key codes
  128. //
  129. // Note that the actual values of these are platform-specific and may change
  130. // without warning, so don't store them anywhere as constants. For persisting/retrieving
  131. // KeyPress objects, use getTextDescription() and createFromDescription() instead.
  132. //
  133. static const int spaceKey; /**< key-code for the space bar */
  134. static const int escapeKey; /**< key-code for the escape key */
  135. static const int returnKey; /**< key-code for the return key*/
  136. static const int tabKey; /**< key-code for the tab key*/
  137. static const int deleteKey; /**< key-code for the delete key (not backspace) */
  138. static const int backspaceKey; /**< key-code for the backspace key */
  139. static const int insertKey; /**< key-code for the insert key */
  140. static const int upKey; /**< key-code for the cursor-up key */
  141. static const int downKey; /**< key-code for the cursor-down key */
  142. static const int leftKey; /**< key-code for the cursor-left key */
  143. static const int rightKey; /**< key-code for the cursor-right key */
  144. static const int pageUpKey; /**< key-code for the page-up key */
  145. static const int pageDownKey; /**< key-code for the page-down key */
  146. static const int homeKey; /**< key-code for the home key */
  147. static const int endKey; /**< key-code for the end key */
  148. static const int F1Key; /**< key-code for the F1 key */
  149. static const int F2Key; /**< key-code for the F2 key */
  150. static const int F3Key; /**< key-code for the F3 key */
  151. static const int F4Key; /**< key-code for the F4 key */
  152. static const int F5Key; /**< key-code for the F5 key */
  153. static const int F6Key; /**< key-code for the F6 key */
  154. static const int F7Key; /**< key-code for the F7 key */
  155. static const int F8Key; /**< key-code for the F8 key */
  156. static const int F9Key; /**< key-code for the F9 key */
  157. static const int F10Key; /**< key-code for the F10 key */
  158. static const int F11Key; /**< key-code for the F11 key */
  159. static const int F12Key; /**< key-code for the F12 key */
  160. static const int F13Key; /**< key-code for the F13 key */
  161. static const int F14Key; /**< key-code for the F14 key */
  162. static const int F15Key; /**< key-code for the F15 key */
  163. static const int F16Key; /**< key-code for the F16 key */
  164. static const int F17Key; /**< key-code for the F17 key */
  165. static const int F18Key; /**< key-code for the F18 key */
  166. static const int F19Key; /**< key-code for the F19 key */
  167. static const int F20Key; /**< key-code for the F20 key */
  168. static const int F21Key; /**< key-code for the F21 key */
  169. static const int F22Key; /**< key-code for the F22 key */
  170. static const int F23Key; /**< key-code for the F23 key */
  171. static const int F24Key; /**< key-code for the F24 key */
  172. static const int F25Key; /**< key-code for the F25 key */
  173. static const int F26Key; /**< key-code for the F26 key */
  174. static const int F27Key; /**< key-code for the F27 key */
  175. static const int F28Key; /**< key-code for the F28 key */
  176. static const int F29Key; /**< key-code for the F29 key */
  177. static const int F30Key; /**< key-code for the F30 key */
  178. static const int F31Key; /**< key-code for the F31 key */
  179. static const int F32Key; /**< key-code for the F32 key */
  180. static const int F33Key; /**< key-code for the F33 key */
  181. static const int F34Key; /**< key-code for the F34 key */
  182. static const int F35Key; /**< key-code for the F35 key */
  183. static const int numberPad0; /**< key-code for the 0 on the numeric keypad. */
  184. static const int numberPad1; /**< key-code for the 1 on the numeric keypad. */
  185. static const int numberPad2; /**< key-code for the 2 on the numeric keypad. */
  186. static const int numberPad3; /**< key-code for the 3 on the numeric keypad. */
  187. static const int numberPad4; /**< key-code for the 4 on the numeric keypad. */
  188. static const int numberPad5; /**< key-code for the 5 on the numeric keypad. */
  189. static const int numberPad6; /**< key-code for the 6 on the numeric keypad. */
  190. static const int numberPad7; /**< key-code for the 7 on the numeric keypad. */
  191. static const int numberPad8; /**< key-code for the 8 on the numeric keypad. */
  192. static const int numberPad9; /**< key-code for the 9 on the numeric keypad. */
  193. static const int numberPadAdd; /**< key-code for the add sign on the numeric keypad. */
  194. static const int numberPadSubtract; /**< key-code for the subtract sign on the numeric keypad. */
  195. static const int numberPadMultiply; /**< key-code for the multiply sign on the numeric keypad. */
  196. static const int numberPadDivide; /**< key-code for the divide sign on the numeric keypad. */
  197. static const int numberPadSeparator; /**< key-code for the comma on the numeric keypad. */
  198. static const int numberPadDecimalPoint; /**< key-code for the decimal point sign on the numeric keypad. */
  199. static const int numberPadEquals; /**< key-code for the equals key on the numeric keypad. */
  200. static const int numberPadDelete; /**< key-code for the delete key on the numeric keypad. */
  201. static const int playKey; /**< key-code for a multimedia 'play' key, (not all keyboards will have one) */
  202. static const int stopKey; /**< key-code for a multimedia 'stop' key, (not all keyboards will have one) */
  203. static const int fastForwardKey; /**< key-code for a multimedia 'fast-forward' key, (not all keyboards will have one) */
  204. static const int rewindKey; /**< key-code for a multimedia 'rewind' key, (not all keyboards will have one) */
  205. private:
  206. //==============================================================================
  207. int keyCode = 0;
  208. ModifierKeys mods;
  209. juce_wchar textCharacter = 0;
  210. JUCE_LEAK_DETECTOR (KeyPress)
  211. };
  212. } // namespace juce