The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

201 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #ifndef __JUCE_MODIFIERKEYS_JUCEHEADER__
  24. #define __JUCE_MODIFIERKEYS_JUCEHEADER__
  25. //==============================================================================
  26. /**
  27. Represents the state of the mouse buttons and modifier keys.
  28. This is used both by mouse events and by KeyPress objects to describe
  29. the state of keys such as shift, control, alt, etc.
  30. @see KeyPress, MouseEvent::mods
  31. */
  32. class JUCE_API ModifierKeys
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a ModifierKeys object from a raw set of flags.
  37. @param flags to represent the keys that are down
  38. @see shiftModifier, ctrlModifier, altModifier, leftButtonModifier,
  39. rightButtonModifier, commandModifier, popupMenuClickModifier
  40. */
  41. ModifierKeys (const int flags = 0) throw();
  42. /** Creates a copy of another object. */
  43. ModifierKeys (const ModifierKeys& other) throw();
  44. /** Copies this object from another one. */
  45. const ModifierKeys& operator= (const ModifierKeys& other) throw();
  46. //==============================================================================
  47. /** Checks whether the 'command' key flag is set (or 'ctrl' on Windows/Linux).
  48. This is a platform-agnostic way of checking for the operating system's
  49. preferred command-key modifier - so on the Mac it tests for the Apple key, on
  50. Windows/Linux, it's actually checking for the CTRL key.
  51. */
  52. inline bool isCommandDown() const throw() { return (flags & commandModifier) != 0; }
  53. /** Checks whether the user is trying to launch a pop-up menu.
  54. This checks for platform-specific modifiers that might indicate that the user
  55. is following the operating system's normal method of showing a pop-up menu.
  56. So on Windows/Linux, this method is really testing for a right-click.
  57. On the Mac, it tests for either the CTRL key being down, or a right-click.
  58. */
  59. inline bool isPopupMenu() const throw() { return (flags & popupMenuClickModifier) != 0; }
  60. /** Checks whether the flag is set for the left mouse-button. */
  61. inline bool isLeftButtonDown() const throw() { return (flags & leftButtonModifier) != 0; }
  62. /** Checks whether the flag is set for the right mouse-button.
  63. Note that for detecting popup-menu clicks, you should be using isPopupMenu() instead, as
  64. this is platform-independent (and makes your code more explanatory too).
  65. */
  66. inline bool isRightButtonDown() const throw() { return (flags & rightButtonModifier) != 0; }
  67. inline bool isMiddleButtonDown() const throw() { return (flags & middleButtonModifier) != 0; }
  68. /** Tests for any of the mouse-button flags. */
  69. inline bool isAnyMouseButtonDown() const throw() { return (flags & allMouseButtonModifiers) != 0; }
  70. /** Tests for any of the modifier key flags. */
  71. inline bool isAnyModifierKeyDown() const throw() { return (flags & (shiftModifier | ctrlModifier | altModifier | commandModifier)) != 0; }
  72. /** Checks whether the shift key's flag is set. */
  73. inline bool isShiftDown() const throw() { return (flags & shiftModifier) != 0; }
  74. /** Checks whether the CTRL key's flag is set.
  75. Remember that it's better to use the platform-agnostic routines to test for command-key and
  76. popup-menu modifiers.
  77. @see isCommandDown, isPopupMenu
  78. */
  79. inline bool isCtrlDown() const throw() { return (flags & ctrlModifier) != 0; }
  80. /** Checks whether the shift key's flag is set. */
  81. inline bool isAltDown() const throw() { return (flags & altModifier) != 0; }
  82. //==============================================================================
  83. /** Flags that represent the different keys. */
  84. enum Flags
  85. {
  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. };
  116. //==============================================================================
  117. /** Returns the raw flags for direct testing. */
  118. inline int getRawFlags() const throw() { return flags; }
  119. /** Tests a combination of flags and returns true if any of them are set. */
  120. inline bool testFlags (const int flagsToTest) const throw() { return (flags & flagsToTest) != 0; }
  121. //==============================================================================
  122. /** Creates a ModifierKeys object to represent the last-known state of the
  123. keyboard and mouse buttons.
  124. @see getCurrentModifiersRealtime
  125. */
  126. static const ModifierKeys getCurrentModifiers() throw();
  127. /** Creates a ModifierKeys object to represent the current state of the
  128. keyboard and mouse buttons.
  129. This isn't often needed and isn't recommended, but will actively check all the
  130. mouse and key states rather than just returning their last-known state like
  131. getCurrentModifiers() does.
  132. This is only needed in special circumstances for up-to-date modifier information
  133. at times when the app's event loop isn't running normally.
  134. */
  135. static const ModifierKeys getCurrentModifiersRealtime() throw();
  136. private:
  137. //==============================================================================
  138. int flags;
  139. static int currentModifierFlags;
  140. friend class ComponentPeer;
  141. static void updateCurrentModifiers() throw();
  142. };
  143. #endif // __JUCE_MODIFIERKEYS_JUCEHEADER__