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