Panda3D
 All Classes Functions Variables Enumerations
buttonEvent.h
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 
15 #ifndef BUTTONEVENT_H
16 #define BUTTONEVENT_H
17 
18 #include "pandabase.h"
19 
20 #include "buttonHandle.h"
21 #include "clockObject.h"
22 #include "modifierButtons.h"
23 
24 class Datagram;
25 class DatagramIterator;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Class : ButtonEvent
29 // Description : Records a button event of some kind. This is either
30 // a keyboard or mouse button (or some other kind of
31 // button) changing state from up to down, or
32 // vice-versa, or it is a single "keystroke".
33 //
34 // A keystroke is different than a button event in that
35 // (a) it does not necessarily correspond to a physical
36 // button on a keyboard, but might be the result of a
37 // combination of buttons (e.g. "A" is the result of
38 // shift + "a"); and (b) it does not manage separate
39 // "up" and "down" events, but is itself an
40 // instantaneous event.
41 //
42 // Normal up/down button events can be used to track the
43 // state of a particular button on the keyboard, while
44 // keystroke events are best used to monitor what a user
45 // is attempting to type.
46 //
47 // Button up/down events are defined across all the
48 // physical keys on the keyboard (and other buttons for
49 // which there is a corresponding ButtonHandle object),
50 // while keystroke events are defined across the entire
51 // Unicode character set.
52 ////////////////////////////////////////////////////////////////////
53 class EXPCL_PANDA_EVENT ButtonEvent {
54 public:
55  enum Type {
56  // T_down and T_up represent a button changing state
57  // correspondingly. T_resume_down is a special event that is only
58  // thrown when focus is returned to a window and a button is
59  // detected as being held down at that point; it indicates that
60  // the button should be considered down now (if it wasn't
61  // already), but it didn't just get pressed down at this moment,
62  // it was depressed some time ago. It's mainly used for correct
63  // tracking of modifier keys like shift and control, and can be
64  // ignored for other keys.
65  T_down,
66  T_resume_down,
67  T_up,
68 
69  // T_repeat is sent for each a keyrepeat event generated by the
70  // system, for a button that is continually held down. If you
71  // want to respect keyrepeat, treat T_down and T_repeat
72  // equivalently.
73  T_repeat,
74 
75  // T_keystroke is a special keystroke event, and is sent along
76  // with a Unicode keycode value, not a ButtonHandle.
77  T_keystroke,
78 
79  // T_candidate is used to indicate that the user is using the IME
80  // and has in the process of selecting some possible text to type
81  // from a menu.
82  T_candidate,
83 
84  // T_move is used to indicate that the mouse has moved within the
85  // current region. Button drag mode needs this, others may ignore
86  // this event
87  T_move,
88 
89  // T_raw_down is usually sent together with T_down, except that
90  // this is the original, untransformed scan key sent by the keyboard.
91  // It is not altered by modifier keys and acts as if the user is
92  // using the US (qwerty) keyboard layout.
93  T_raw_down,
94  T_raw_up,
95  };
96 
97  INLINE ButtonEvent();
98  INLINE ButtonEvent(ButtonHandle button, Type type, double time = ClockObject::get_global_clock()->get_frame_time());
99  INLINE ButtonEvent(short keycode, double time = ClockObject::get_global_clock()->get_frame_time());
100  INLINE ButtonEvent(const wstring &candidate_string, size_t highlight_start,
101  size_t highlight_end, size_t cursor_pos);
102  INLINE ButtonEvent(const ButtonEvent &copy);
103  INLINE void operator = (const ButtonEvent &copy);
104 
105  INLINE bool operator == (const ButtonEvent &other) const;
106  INLINE bool operator != (const ButtonEvent &other) const;
107  INLINE bool operator < (const ButtonEvent &other) const;
108 
109  INLINE bool update_mods(ModifierButtons &mods) const;
110 
111  void output(ostream &out) const;
112 
113  void write_datagram(Datagram &dg) const;
114  void read_datagram(DatagramIterator &scan);
115 
116 public:
117  // _button will be filled in if type is T_down, T_resume_down, or
118  // T_up.
119  ButtonHandle _button;
120 
121  // _keycode will be filled in if type is T_keystroke. It will be
122  // the Unicode character that was typed.
123  short _keycode;
124 
125  // _candidate_string will be filled in if type is T_candidate.
126  wstring _candidate_string;
127  size_t _highlight_start;
128  size_t _highlight_end;
129  size_t _cursor_pos;
130 
131  // This is the type of the button event (see above).
132  Type _type;
133 
134  // This is the time the event occurred, as recorded from the OS if
135  // that information is available. It is in seconds elapsed from an
136  // arbitrary epoch, and it matches the time reported by
137  // ClockObject::get_global_clock().
138  double _time;
139 };
140 
141 INLINE ostream &operator << (ostream &out, const ButtonEvent &be) {
142  be.output(out);
143  return out;
144 }
145 
146 #include "buttonEvent.I"
147 
148 #endif
149 
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:271
This class monitors the state of a number of individual buttons and tracks whether each button is kno...
Records a button event of some kind.
Definition: buttonEvent.h:53
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
A class to retrieve the individual data elements previously stored in a Datagram. ...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43