KeyEvent.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2  Copyright (C) 2015 Laurentiu-Gheorghe Crisan
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 #ifndef Pt_Forms_KeyEvent_h
30 #define Pt_Forms_KeyEvent_h
31 
32 #include <Pt/Forms/Api.h>
33 #include <Pt/Forms/Widget.h>
34 #include <Pt/Forms/Key.h>
35 #include <Pt/Event.h>
36 #include <Pt/String.h>
37 
38 namespace Pt {
39 
40 namespace Forms {
41 
49 class PT_FORMS_API KeyEvent : public Pt::BasicEvent<KeyEvent>
50 {
51  public:
54  enum Action
55  {
56  Press,
57  Release
58  };
59 
60  public:
64  : _widgetId_(0)
65  , _widget(0)
66  , _action(Release)
67  {
68  }
69 
72  explicit KeyEvent(Widget& widget)
73  : _widgetId_( widget.id() )
74  , _widget(&widget)
75  , _action(Release)
76  {
77  }
78 
82  {
83  return _widgetId_;
84  }
85 
88  Widget* widget() const
89  {
90  return _widget;
91  }
92 
95  void setWidget(Widget* widget)
96  {
97  _widget = widget;
98  _widgetId_ = widget ? widget->id() : 0;
99  }
100 
103  const Key& key() const
104  {
105  return _key;
106  }
107 
110  const Pt::Char& unicode() const
111  {
112  return _unicode;
113  }
114 
117  bool isPress() const
118  {
119  return _action == Press;
120  }
121 
124  void setPress(const Key& key, const Pt::Char& ch)
125  {
126  _action = Press;
127  _key = key;
128  _unicode = ch;
129  }
130 
133  bool isRelease() const
134  {
135  return _action == Release;
136  }
137 
140  void setRelease(const Key& key, const Pt::Char& ch)
141  {
142  _action = Release;
143  _key = key;
144  _unicode = ch;
145  }
146 
147  private:
148  Pt::uint64_t _widgetId_;
149  Widget* _widget;
150  Action _action;
151  Key _key;
152  Pt::Char _unicode;
153 };
154 
155 } // namespace
156 
157 } // namespace
158 
159 #endif
Core module.
Definition: Allocator.h:33
Key press or release.
Definition: KeyEvent.h:50
Widget * widget() const
Returns the target widget, or 0 when no target is set.
Definition: KeyEvent.h:88
bool isRelease() const
Returns true when the event is a key release.
Definition: KeyEvent.h:133
const Key & key() const
Returns the key and modifiers.
Definition: KeyEvent.h:103
KeyEvent()
Creates a key-release event without a target.
Definition: KeyEvent.h:63
bool isPress() const
Returns true when the event is a key press.
Definition: KeyEvent.h:117
Common type of every visual Forms object.
Definition: Widget.h:105
Unicode character type.
Definition: String.h:67
void setPress(const Key &key, const Pt::Char &ch)
Changes the event to a press of key with text ch.
Definition: KeyEvent.h:124
KeyEvent(Widget &widget)
Creates a key-release event targeted at widget.
Definition: KeyEvent.h:72
void setWidget(Widget *widget)
Sets the target widget and its ID.
Definition: KeyEvent.h:95
Pt::uint64_t widgetId() const
Returns the target widget ID, or 0 when no target is set.
Definition: KeyEvent.h:81
void setRelease(const Key &key, const Pt::Char &ch)
Changes the event to a release of key with text ch.
Definition: KeyEvent.h:140
Action
Defines the key action.
Definition: KeyEvent.h:55
Key code and modifiers.
Definition: Key.h:23
Pt::uint64_t id() const
Returns the application-unique ID of this live widget.
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
const Pt::Char & unicode() const
Returns the optional Unicode text.
Definition: KeyEvent.h:110