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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Represents the state of the mouse buttons and modifier keys.
  18. This is used both by mouse events and by KeyPress objects to describe
  19. the state of keys such as shift, control, alt, etc.
  20. @see KeyPress, MouseEvent::mods
  21. @tags{GUI}
  22. */
  23. class JUCE_API ModifierKeys
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates a ModifierKeys object with no flags set. */
  28. ModifierKeys() = default;
  29. /** Creates a ModifierKeys object from a raw set of flags.
  30. @param flags to represent the keys that are down
  31. @see shiftModifier, ctrlModifier, altModifier, leftButtonModifier,
  32. rightButtonModifier, commandModifier, popupMenuClickModifier
  33. */
  34. ModifierKeys (int flags) noexcept;
  35. /** Creates a copy of another object. */
  36. ModifierKeys (const ModifierKeys&) = default;
  37. /** Copies this object from another one. */
  38. ModifierKeys& operator= (const ModifierKeys&) = default;
  39. //==============================================================================
  40. /** Checks whether the 'command' key flag is set (or 'ctrl' on Windows/Linux).
  41. This is a platform-agnostic way of checking for the operating system's
  42. preferred command-key modifier - so on the Mac it tests for the cmd key, on
  43. Windows/Linux, it's actually checking for the CTRL key.
  44. */
  45. inline bool isCommandDown() const noexcept { return testFlags (commandModifier); }
  46. /** Checks whether the user is trying to launch a pop-up menu.
  47. This checks for platform-specific modifiers that might indicate that the user
  48. is following the operating system's normal method of showing a pop-up menu.
  49. So on Windows/Linux, this method is really testing for a right-click.
  50. On the Mac, it tests for either the CTRL key being down, or a right-click.
  51. */
  52. inline bool isPopupMenu() const noexcept { return testFlags (popupMenuClickModifier); }
  53. /** Checks whether the flag is set for the left mouse-button. */
  54. inline bool isLeftButtonDown() const noexcept { return testFlags (leftButtonModifier); }
  55. /** Checks whether the flag is set for the right mouse-button.
  56. Note that for detecting popup-menu clicks, you should be using isPopupMenu() instead, as
  57. this is platform-independent (and makes your code more explanatory too).
  58. */
  59. inline bool isRightButtonDown() const noexcept { return testFlags (rightButtonModifier); }
  60. inline bool isMiddleButtonDown() const noexcept { return testFlags (middleButtonModifier); }
  61. /** Tests for any of the mouse-button flags. */
  62. inline bool isAnyMouseButtonDown() const noexcept { return testFlags (allMouseButtonModifiers); }
  63. /** Tests for any of the modifier key flags. */
  64. inline bool isAnyModifierKeyDown() const noexcept { return testFlags ((shiftModifier | ctrlModifier | altModifier | commandModifier)); }
  65. /** Checks whether the shift key's flag is set. */
  66. inline bool isShiftDown() const noexcept { return testFlags (shiftModifier); }
  67. /** Checks whether the CTRL key's flag is set.
  68. Remember that it's better to use the platform-agnostic routines to test for command-key and
  69. popup-menu modifiers.
  70. @see isCommandDown, isPopupMenu
  71. */
  72. inline bool isCtrlDown() const noexcept { return testFlags (ctrlModifier); }
  73. /** Checks whether the ALT key's flag is set. */
  74. inline bool isAltDown() const noexcept { return testFlags (altModifier); }
  75. //==============================================================================
  76. /** Flags that represent the different keys. */
  77. enum Flags
  78. {
  79. /** Indicates no modifier keys. */
  80. noModifiers = 0,
  81. /** Shift key flag. */
  82. shiftModifier = 1,
  83. /** CTRL key flag. */
  84. ctrlModifier = 2,
  85. /** ALT key flag. */
  86. altModifier = 4,
  87. /** Left mouse button flag. */
  88. leftButtonModifier = 16,
  89. /** Right mouse button flag. */
  90. rightButtonModifier = 32,
  91. /** Middle mouse button flag. */
  92. middleButtonModifier = 64,
  93. #if JUCE_MAC || JUCE_IOS
  94. /** Command key flag - on windows this is the same as the CTRL key flag. */
  95. commandModifier = 8,
  96. /** Popup menu flag - on windows this is the same as rightButtonModifier, on the
  97. Mac it's the same as (rightButtonModifier | ctrlModifier). */
  98. popupMenuClickModifier = rightButtonModifier | ctrlModifier,
  99. #else
  100. /** Command key flag - on windows this is the same as the CTRL key flag. */
  101. commandModifier = ctrlModifier,
  102. /** Popup menu flag - on windows this is the same as rightButtonModifier, on the
  103. Mac it's the same as (rightButtonModifier | ctrlModifier). */
  104. popupMenuClickModifier = rightButtonModifier,
  105. #endif
  106. /** Represents a combination of all the shift, alt, ctrl and command key modifiers. */
  107. allKeyboardModifiers = shiftModifier | ctrlModifier | altModifier | commandModifier,
  108. /** Represents a combination of all the mouse buttons at once. */
  109. allMouseButtonModifiers = leftButtonModifier | rightButtonModifier | middleButtonModifier,
  110. /** Represents a combination of all the alt, ctrl and command key modifiers. */
  111. ctrlAltCommandModifiers = ctrlModifier | altModifier | commandModifier
  112. };
  113. //==============================================================================
  114. /** Returns a copy of only the mouse-button flags */
  115. ModifierKeys withOnlyMouseButtons() const noexcept { return ModifierKeys (flags & allMouseButtonModifiers); }
  116. /** Returns a copy of only the non-mouse flags */
  117. ModifierKeys withoutMouseButtons() const noexcept { return ModifierKeys (flags & ~allMouseButtonModifiers); }
  118. bool operator== (const ModifierKeys other) const noexcept { return flags == other.flags; }
  119. bool operator!= (const ModifierKeys other) const noexcept { return flags != other.flags; }
  120. //==============================================================================
  121. /** Returns the raw flags for direct testing. */
  122. inline int getRawFlags() const noexcept { return flags; }
  123. ModifierKeys withoutFlags (int rawFlagsToClear) const noexcept { return ModifierKeys (flags & ~rawFlagsToClear); }
  124. ModifierKeys withFlags (int rawFlagsToSet) const noexcept { return ModifierKeys (flags | rawFlagsToSet); }
  125. /** Tests a combination of flags and returns true if any of them are set. */
  126. bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  127. /** Returns the total number of mouse buttons that are down. */
  128. int getNumMouseButtonsDown() const noexcept;
  129. //==============================================================================
  130. /** This object represents the last-known state of the keyboard and mouse buttons. */
  131. static ModifierKeys currentModifiers;
  132. /** Creates a ModifierKeys object to represent the last-known state of the
  133. keyboard and mouse buttons.
  134. This method is here for backwards compatibility and there's no need to call it anymore,
  135. you should use the public currentModifiers member directly.
  136. */
  137. static ModifierKeys getCurrentModifiers() noexcept { return currentModifiers; }
  138. /** Creates a ModifierKeys object to represent the current state of the
  139. keyboard and mouse buttons.
  140. This method is here for backwards compatibility and you should call ComponentPeer::getCurrentModifiersRealtime()
  141. instead (which is what this method now does).
  142. */
  143. static ModifierKeys getCurrentModifiersRealtime() noexcept;
  144. private:
  145. int flags = 0;
  146. };
  147. } // namespace juce