buttonEvent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Filename: buttonEvent.h
  2. // Created by: drose (01Mar00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #ifndef BUTTONEVENT_H
  15. #define BUTTONEVENT_H
  16. #include "pandabase.h"
  17. #include "buttonHandle.h"
  18. #include "clockObject.h"
  19. #include "modifierButtons.h"
  20. class Datagram;
  21. class DatagramIterator;
  22. ////////////////////////////////////////////////////////////////////
  23. // Class : ButtonEvent
  24. // Description : Records a button event of some kind. This is either
  25. // a keyboard or mouse button (or some other kind of
  26. // button) changing state from up to down, or
  27. // vice-versa, or it is a single "keystroke".
  28. //
  29. // A keystroke is different than a button event in that
  30. // (a) it does not necessarily correspond to a physical
  31. // button on a keyboard, but might be the result of a
  32. // combination of buttons (e.g. "A" is the result of
  33. // shift + "a"); and (b) it does not manage separate
  34. // "up" and "down" events, but is itself an
  35. // instantaneous event.
  36. //
  37. // Normal up/down button events can be used to track the
  38. // state of a particular button on the keyboard, while
  39. // keystroke events are best used to monitor what a user
  40. // is attempting to type.
  41. //
  42. // Button up/down events are defined across all the
  43. // physical keys on the keyboard (and other buttons for
  44. // which there is a corresponding ButtonHandle object),
  45. // while keystroke events are defined across the entire
  46. // Unicode character set.
  47. ////////////////////////////////////////////////////////////////////
  48. class EXPCL_PANDA_EVENT ButtonEvent {
  49. public:
  50. enum Type {
  51. // T_down and T_up represent a button changing state
  52. // correspondingly. T_resume_down is a special event that is only
  53. // thrown when focus is returned to a window and a button is
  54. // detected as being held down at that point; it indicates that
  55. // the button should be considered down now (if it wasn't
  56. // already), but it didn't just get pressed down at this moment,
  57. // it was depressed some time ago. It's mainly used for correct
  58. // tracking of modifier keys like shift and control, and can be
  59. // ignored for other keys.
  60. T_down,
  61. T_resume_down,
  62. T_up,
  63. // T_repeat is sent for each a keyrepeat event generated by the
  64. // system, for a button that is continually held down. If you
  65. // want to respect keyrepeat, treat T_down and T_repeat
  66. // equivalently.
  67. T_repeat,
  68. // T_keystroke is a special keystroke event, and is sent along
  69. // with a Unicode keycode value, not a ButtonHandle.
  70. T_keystroke,
  71. // T_candidate is used to indicate that the user is using the IME
  72. // and has in the process of selecting some possible text to type
  73. // from a menu.
  74. T_candidate,
  75. // T_move is used to indicate that the mouse has moved within the
  76. // current region. Button drag mode needs this, others may ignore
  77. // this event
  78. T_move,
  79. // T_raw_down is usually sent together with T_down, except that
  80. // this is the original, untransformed scan key sent by the keyboard.
  81. // It is not altered by modifier keys and acts as if the user is
  82. // using the US (qwerty) keyboard layout.
  83. T_raw_down,
  84. T_raw_up,
  85. };
  86. INLINE ButtonEvent();
  87. INLINE ButtonEvent(ButtonHandle button, Type type, double time = ClockObject::get_global_clock()->get_frame_time());
  88. INLINE ButtonEvent(short keycode, double time = ClockObject::get_global_clock()->get_frame_time());
  89. INLINE ButtonEvent(const wstring &candidate_string, size_t highlight_start,
  90. size_t highlight_end, size_t cursor_pos);
  91. INLINE ButtonEvent(const ButtonEvent &copy);
  92. INLINE void operator = (const ButtonEvent &copy);
  93. INLINE bool operator == (const ButtonEvent &other) const;
  94. INLINE bool operator != (const ButtonEvent &other) const;
  95. INLINE bool operator < (const ButtonEvent &other) const;
  96. INLINE bool update_mods(ModifierButtons &mods) const;
  97. void output(ostream &out) const;
  98. void write_datagram(Datagram &dg) const;
  99. void read_datagram(DatagramIterator &scan);
  100. public:
  101. // _button will be filled in if type is T_down, T_resume_down, or
  102. // T_up.
  103. ButtonHandle _button;
  104. // _keycode will be filled in if type is T_keystroke. It will be
  105. // the Unicode character that was typed.
  106. short _keycode;
  107. // _candidate_string will be filled in if type is T_candidate.
  108. wstring _candidate_string;
  109. size_t _highlight_start;
  110. size_t _highlight_end;
  111. size_t _cursor_pos;
  112. // This is the type of the button event (see above).
  113. Type _type;
  114. // This is the time the event occurred, as recorded from the OS if
  115. // that information is available. It is in seconds elapsed from an
  116. // arbitrary epoch, and it matches the time reported by
  117. // ClockObject::get_global_clock().
  118. double _time;
  119. };
  120. INLINE ostream &operator << (ostream &out, const ButtonEvent &be) {
  121. be.output(out);
  122. return out;
  123. }
  124. #include "buttonEvent.I"
  125. #endif