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.

253 lines
12KB

  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. #ifndef __JUCE_KEYPRESS_JUCEHEADER__
  19. #define __JUCE_KEYPRESS_JUCEHEADER__
  20. #include "juce_ModifierKeys.h"
  21. //==============================================================================
  22. /**
  23. Represents a key press, including any modifier keys that are needed.
  24. E.g. a KeyPress might represent CTRL+C, SHIFT+ALT+H, Spacebar, Escape, etc.
  25. @see Component, KeyListener, Button::addShortcut, KeyPressMappingManager
  26. */
  27. class JUCE_API KeyPress
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an (invalid) KeyPress.
  32. @see isValid
  33. */
  34. KeyPress() noexcept;
  35. /** Creates a KeyPress for a key and some modifiers.
  36. e.g.
  37. CTRL+C would be: KeyPress ('c', ModifierKeys::ctrlModifier)
  38. SHIFT+Escape would be: KeyPress (KeyPress::escapeKey, ModifierKeys::shiftModifier)
  39. @param keyCode a code that represents the key - this value must be
  40. one of special constants listed in this class, or an
  41. 8-bit character code such as a letter (case is ignored),
  42. digit or a simple key like "," or ".". Note that this
  43. isn't the same as the textCharacter parameter, so for example
  44. a keyCode of 'a' and a shift-key modifier should have a
  45. textCharacter value of 'A'.
  46. @param modifiers the modifiers to associate with the keystroke
  47. @param textCharacter the character that would be printed if someone typed
  48. this keypress into a text editor. This value may be
  49. null if the keypress is a non-printing character
  50. @see getKeyCode, isKeyCode, getModifiers
  51. */
  52. KeyPress (int keyCode,
  53. const ModifierKeys& modifiers,
  54. juce_wchar textCharacter) noexcept;
  55. /** Creates a keypress with a keyCode but no modifiers or text character.
  56. */
  57. KeyPress (int keyCode) noexcept;
  58. /** Creates a copy of another KeyPress. */
  59. KeyPress (const KeyPress& other) noexcept;
  60. /** Copies this KeyPress from another one. */
  61. KeyPress& operator= (const KeyPress& other) noexcept;
  62. /** Compares two KeyPress objects. */
  63. bool operator== (const KeyPress& other) const noexcept;
  64. /** Compares two KeyPress objects. */
  65. bool operator!= (const KeyPress& other) 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 "SPACE".
  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 numberPad0; /**< key-code for the 0 on the numeric keypad. */
  165. static const int numberPad1; /**< key-code for the 1 on the numeric keypad. */
  166. static const int numberPad2; /**< key-code for the 2 on the numeric keypad. */
  167. static const int numberPad3; /**< key-code for the 3 on the numeric keypad. */
  168. static const int numberPad4; /**< key-code for the 4 on the numeric keypad. */
  169. static const int numberPad5; /**< key-code for the 5 on the numeric keypad. */
  170. static const int numberPad6; /**< key-code for the 6 on the numeric keypad. */
  171. static const int numberPad7; /**< key-code for the 7 on the numeric keypad. */
  172. static const int numberPad8; /**< key-code for the 8 on the numeric keypad. */
  173. static const int numberPad9; /**< key-code for the 9 on the numeric keypad. */
  174. static const int numberPadAdd; /**< key-code for the add sign on the numeric keypad. */
  175. static const int numberPadSubtract; /**< key-code for the subtract sign on the numeric keypad. */
  176. static const int numberPadMultiply; /**< key-code for the multiply sign on the numeric keypad. */
  177. static const int numberPadDivide; /**< key-code for the divide sign on the numeric keypad. */
  178. static const int numberPadSeparator; /**< key-code for the comma on the numeric keypad. */
  179. static const int numberPadDecimalPoint; /**< key-code for the decimal point sign on the numeric keypad. */
  180. static const int numberPadEquals; /**< key-code for the equals key on the numeric keypad. */
  181. static const int numberPadDelete; /**< key-code for the delete key on the numeric keypad. */
  182. static const int playKey; /**< key-code for a multimedia 'play' key, (not all keyboards will have one) */
  183. static const int stopKey; /**< key-code for a multimedia 'stop' key, (not all keyboards will have one) */
  184. static const int fastForwardKey; /**< key-code for a multimedia 'fast-forward' key, (not all keyboards will have one) */
  185. static const int rewindKey; /**< key-code for a multimedia 'rewind' key, (not all keyboards will have one) */
  186. private:
  187. //==============================================================================
  188. int keyCode;
  189. ModifierKeys mods;
  190. juce_wchar textCharacter;
  191. JUCE_LEAK_DETECTOR (KeyPress);
  192. };
  193. #endif // __JUCE_KEYPRESS_JUCEHEADER__