juce_KeyPress.h 12 KB

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