juce_ModifierKeys.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_MODIFIERKEYS_H_INCLUDED
  18. #define JUCE_MODIFIERKEYS_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Represents the state of the mouse buttons and modifier keys.
  22. This is used both by mouse events and by KeyPress objects to describe
  23. the state of keys such as shift, control, alt, etc.
  24. @see KeyPress, MouseEvent::mods
  25. */
  26. class JUCE_API ModifierKeys
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a ModifierKeys object with no flags set. */
  31. ModifierKeys() noexcept;
  32. /** Creates a ModifierKeys object from a raw set of flags.
  33. @param flags to represent the keys that are down
  34. @see shiftModifier, ctrlModifier, altModifier, leftButtonModifier,
  35. rightButtonModifier, commandModifier, popupMenuClickModifier
  36. */
  37. ModifierKeys (int flags) noexcept;
  38. /** Creates a copy of another object. */
  39. ModifierKeys (const ModifierKeys& other) noexcept;
  40. /** Copies this object from another one. */
  41. ModifierKeys& operator= (const ModifierKeys other) noexcept;
  42. //==============================================================================
  43. /** Checks whether the 'command' key flag is set (or 'ctrl' on Windows/Linux).
  44. This is a platform-agnostic way of checking for the operating system's
  45. preferred command-key modifier - so on the Mac it tests for the Apple key, on
  46. Windows/Linux, it's actually checking for the CTRL key.
  47. */
  48. inline bool isCommandDown() const noexcept { return testFlags (commandModifier); }
  49. /** Checks whether the user is trying to launch a pop-up menu.
  50. This checks for platform-specific modifiers that might indicate that the user
  51. is following the operating system's normal method of showing a pop-up menu.
  52. So on Windows/Linux, this method is really testing for a right-click.
  53. On the Mac, it tests for either the CTRL key being down, or a right-click.
  54. */
  55. inline bool isPopupMenu() const noexcept { return testFlags (popupMenuClickModifier); }
  56. /** Checks whether the flag is set for the left mouse-button. */
  57. inline bool isLeftButtonDown() const noexcept { return testFlags (leftButtonModifier); }
  58. /** Checks whether the flag is set for the right mouse-button.
  59. Note that for detecting popup-menu clicks, you should be using isPopupMenu() instead, as
  60. this is platform-independent (and makes your code more explanatory too).
  61. */
  62. inline bool isRightButtonDown() const noexcept { return testFlags (rightButtonModifier); }
  63. inline bool isMiddleButtonDown() const noexcept { return testFlags (middleButtonModifier); }
  64. /** Tests for any of the mouse-button flags. */
  65. inline bool isAnyMouseButtonDown() const noexcept { return testFlags (allMouseButtonModifiers); }
  66. /** Tests for any of the modifier key flags. */
  67. inline bool isAnyModifierKeyDown() const noexcept { return testFlags ((shiftModifier | ctrlModifier | altModifier | commandModifier)); }
  68. /** Checks whether the shift key's flag is set. */
  69. inline bool isShiftDown() const noexcept { return testFlags (shiftModifier); }
  70. /** Checks whether the CTRL key's flag is set.
  71. Remember that it's better to use the platform-agnostic routines to test for command-key and
  72. popup-menu modifiers.
  73. @see isCommandDown, isPopupMenu
  74. */
  75. inline bool isCtrlDown() const noexcept { return testFlags (ctrlModifier); }
  76. /** Checks whether the ALT key's flag is set. */
  77. inline bool isAltDown() const noexcept { return testFlags (altModifier); }
  78. //==============================================================================
  79. /** Flags that represent the different keys. */
  80. enum Flags
  81. {
  82. /** Indicates no modifier keys. */
  83. noModifiers = 0,
  84. /** Shift key flag. */
  85. shiftModifier = 1,
  86. /** CTRL key flag. */
  87. ctrlModifier = 2,
  88. /** ALT key flag. */
  89. altModifier = 4,
  90. /** Left mouse button flag. */
  91. leftButtonModifier = 16,
  92. /** Right mouse button flag. */
  93. rightButtonModifier = 32,
  94. /** Middle mouse button flag. */
  95. middleButtonModifier = 64,
  96. #if JUCE_MAC
  97. /** Command key flag - on windows this is the same as the CTRL key flag. */
  98. commandModifier = 8,
  99. /** Popup menu flag - on windows this is the same as rightButtonModifier, on the
  100. Mac it's the same as (rightButtonModifier | ctrlModifier). */
  101. popupMenuClickModifier = rightButtonModifier | ctrlModifier,
  102. #else
  103. /** Command key flag - on windows this is the same as the CTRL key flag. */
  104. commandModifier = ctrlModifier,
  105. /** Popup menu flag - on windows this is the same as rightButtonModifier, on the
  106. Mac it's the same as (rightButtonModifier | ctrlModifier). */
  107. popupMenuClickModifier = rightButtonModifier,
  108. #endif
  109. /** Represents a combination of all the shift, alt, ctrl and command key modifiers. */
  110. allKeyboardModifiers = shiftModifier | ctrlModifier | altModifier | commandModifier,
  111. /** Represents a combination of all the mouse buttons at once. */
  112. allMouseButtonModifiers = leftButtonModifier | rightButtonModifier | middleButtonModifier,
  113. /** Represents a combination of all the alt, ctrl and command key modifiers. */
  114. ctrlAltCommandModifiers = ctrlModifier | altModifier | commandModifier
  115. };
  116. //==============================================================================
  117. /** Returns a copy of only the mouse-button flags */
  118. ModifierKeys withOnlyMouseButtons() const noexcept { return ModifierKeys (flags & allMouseButtonModifiers); }
  119. /** Returns a copy of only the non-mouse flags */
  120. ModifierKeys withoutMouseButtons() const noexcept { return ModifierKeys (flags & ~allMouseButtonModifiers); }
  121. bool operator== (const ModifierKeys other) const noexcept { return flags == other.flags; }
  122. bool operator!= (const ModifierKeys other) const noexcept { return flags != other.flags; }
  123. //==============================================================================
  124. /** Returns the raw flags for direct testing. */
  125. inline int getRawFlags() const noexcept { return flags; }
  126. ModifierKeys withoutFlags (int rawFlagsToClear) const noexcept { return ModifierKeys (flags & ~rawFlagsToClear); }
  127. ModifierKeys withFlags (int rawFlagsToSet) const noexcept { return ModifierKeys (flags | rawFlagsToSet); }
  128. /** Tests a combination of flags and returns true if any of them are set. */
  129. bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  130. /** Returns the total number of mouse buttons that are down. */
  131. int getNumMouseButtonsDown() const noexcept;
  132. //==============================================================================
  133. /** Creates a ModifierKeys object to represent the last-known state of the
  134. keyboard and mouse buttons.
  135. @see getCurrentModifiersRealtime
  136. */
  137. static ModifierKeys getCurrentModifiers() noexcept;
  138. /** Creates a ModifierKeys object to represent the current state of the
  139. keyboard and mouse buttons.
  140. This isn't often needed and isn't recommended, but will actively check all the
  141. mouse and key states rather than just returning their last-known state like
  142. getCurrentModifiers() does.
  143. This is only needed in special circumstances for up-to-date modifier information
  144. at times when the app's event loop isn't running normally.
  145. Another reason to avoid this method is that it's not stateless, and calling it may
  146. update the value returned by getCurrentModifiers(), which could cause subtle changes
  147. in the behaviour of some components.
  148. */
  149. static ModifierKeys getCurrentModifiersRealtime() noexcept;
  150. private:
  151. //==============================================================================
  152. int flags;
  153. friend class ComponentPeer;
  154. friend class MouseInputSource;
  155. friend class MouseInputSourceInternal;
  156. static ModifierKeys currentModifiers;
  157. static void updateCurrentModifiers() noexcept;
  158. };
  159. #endif // JUCE_MODIFIERKEYS_H_INCLUDED