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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Represents the state of the mouse buttons and modifier keys.
  23. This is used both by mouse events and by KeyPress objects to describe
  24. the state of keys such as shift, control, alt, etc.
  25. @see KeyPress, MouseEvent::mods
  26. @tags{GUI}
  27. */
  28. class JUCE_API ModifierKeys
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a ModifierKeys object with no flags set. */
  33. ModifierKeys() = default;
  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&) = default;
  42. /** Copies this object from another one. */
  43. ModifierKeys& operator= (const ModifierKeys&) = default;
  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 cmd 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 || JUCE_IOS
  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. /** This object represents the last-known state of the keyboard and mouse buttons. */
  136. static ModifierKeys currentModifiers;
  137. /** Creates a ModifierKeys object to represent the last-known state of the
  138. keyboard and mouse buttons.
  139. This method is here for backwards compatibility and there's no need to call it anymore,
  140. you should use the public currentModifiers member directly.
  141. */
  142. static ModifierKeys getCurrentModifiers() noexcept { return currentModifiers; }
  143. /** Creates a ModifierKeys object to represent the current state of the
  144. keyboard and mouse buttons.
  145. This method is here for backwards compatibility and you should call ComponentPeer::getCurrentModifiersRealtime()
  146. instead (which is what this method now does).
  147. */
  148. static ModifierKeys getCurrentModifiersRealtime() noexcept;
  149. private:
  150. int flags = 0;
  151. };
  152. } // namespace juce