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_ModifierKeys.h 9.7KB

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