kbdstate.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/kbdstate.h
  3. // Purpose: Declaration of wxKeyboardState class
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-09-19
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_KBDSTATE_H_
  10. #define _WX_KBDSTATE_H_
  11. #include "wx/defs.h"
  12. // ----------------------------------------------------------------------------
  13. // wxKeyboardState stores the state of the keyboard modifier keys
  14. // ----------------------------------------------------------------------------
  15. class WXDLLIMPEXP_CORE wxKeyboardState
  16. {
  17. public:
  18. wxKeyboardState(bool controlDown = false,
  19. bool shiftDown = false,
  20. bool altDown = false,
  21. bool metaDown = false)
  22. : m_controlDown(controlDown),
  23. m_shiftDown(shiftDown),
  24. m_altDown(altDown),
  25. m_metaDown(metaDown)
  26. #ifdef __WXOSX__
  27. ,m_rawControlDown(false)
  28. #endif
  29. {
  30. }
  31. // default copy ctor, assignment operator and dtor are ok
  32. // accessors for the various modifier keys
  33. // ---------------------------------------
  34. // should be used check if the key event has exactly the given modifiers:
  35. // "GetModifiers() = wxMOD_CONTROL" is easier to write than "ControlDown()
  36. // && !MetaDown() && !AltDown() && !ShiftDown()"
  37. int GetModifiers() const
  38. {
  39. return (m_controlDown ? wxMOD_CONTROL : 0) |
  40. (m_shiftDown ? wxMOD_SHIFT : 0) |
  41. (m_metaDown ? wxMOD_META : 0) |
  42. #ifdef __WXOSX__
  43. (m_rawControlDown ? wxMOD_RAW_CONTROL : 0) |
  44. #endif
  45. (m_altDown ? wxMOD_ALT : 0);
  46. }
  47. // returns true if any modifiers at all are pressed
  48. bool HasAnyModifiers() const { return GetModifiers() != wxMOD_NONE; }
  49. // returns true if any modifiers changing the usual key interpretation are
  50. // pressed, notably excluding Shift
  51. bool HasModifiers() const
  52. {
  53. return ControlDown() || RawControlDown() || AltDown();
  54. }
  55. // accessors for individual modifier keys
  56. bool ControlDown() const { return m_controlDown; }
  57. bool RawControlDown() const
  58. {
  59. #ifdef __WXOSX__
  60. return m_rawControlDown;
  61. #else
  62. return m_controlDown;
  63. #endif
  64. }
  65. bool ShiftDown() const { return m_shiftDown; }
  66. bool MetaDown() const { return m_metaDown; }
  67. bool AltDown() const { return m_altDown; }
  68. // "Cmd" is a pseudo key which is Control for PC and Unix platforms but
  69. // Apple ("Command") key under Macs: it makes often sense to use it instead
  70. // of, say, ControlDown() because Cmd key is used for the same thing under
  71. // Mac as Ctrl elsewhere (but Ctrl still exists, just not used for this
  72. // purpose under Mac)
  73. bool CmdDown() const
  74. {
  75. return ControlDown();
  76. }
  77. // these functions are mostly used by wxWidgets itself
  78. // ---------------------------------------------------
  79. void SetControlDown(bool down) { m_controlDown = down; }
  80. void SetRawControlDown(bool down)
  81. {
  82. #ifdef __WXOSX__
  83. m_rawControlDown = down;
  84. #else
  85. m_controlDown = down;
  86. #endif
  87. }
  88. void SetShiftDown(bool down) { m_shiftDown = down; }
  89. void SetAltDown(bool down) { m_altDown = down; }
  90. void SetMetaDown(bool down) { m_metaDown = down; }
  91. // for backwards compatibility with the existing code accessing these
  92. // members of wxKeyEvent directly, these variables are public, however you
  93. // should not use them in any new code, please use the accessors instead
  94. public:
  95. bool m_controlDown : 1;
  96. bool m_shiftDown : 1;
  97. bool m_altDown : 1;
  98. bool m_metaDown : 1;
  99. #ifdef __WXOSX__
  100. bool m_rawControlDown : 1;
  101. #endif
  102. };
  103. #endif // _WX_KBDSTATE_H_