1/****************************************************************************
2**
3** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qkeyboarddevice.h"
41#include "qkeyboarddevice_p.h"
42
43#include <Qt3DInput/qkeyboardhandler.h>
44
45#include <Qt3DCore/private/qscene_p.h>
46
47QT_BEGIN_NAMESPACE
48
49namespace Qt3DInput {
50
51QKeyboardDevicePrivate::QKeyboardDevicePrivate()
52 : QAbstractPhysicalDevicePrivate()
53 , m_activeInput(nullptr)
54{
55 m_keyMap[QStringLiteral("escape")] = Qt::Key_Escape;
56 m_keyMap[QStringLiteral("tab")] = Qt::Key_Tab;
57 m_keyMap[QStringLiteral("backtab")] = Qt::Key_Backtab;
58 m_keyMap[QStringLiteral("backspace")] = Qt::Key_Backspace;
59 m_keyMap[QStringLiteral("return")] = Qt::Key_Return;
60 m_keyMap[QStringLiteral("enter")] = Qt::Key_Enter;
61 m_keyMap[QStringLiteral("insert")] = Qt::Key_Insert;
62 m_keyMap[QStringLiteral("delete")] = Qt::Key_Delete;
63 m_keyMap[QStringLiteral("pause")] = Qt::Key_Pause;
64 m_keyMap[QStringLiteral("print")] = Qt::Key_Print;
65 m_keyMap[QStringLiteral("sysreq")] = Qt::Key_SysReq;
66 m_keyMap[QStringLiteral("clear")] = Qt::Key_Clear;
67 m_keyMap[QStringLiteral("home")] = Qt::Key_Home;
68 m_keyMap[QStringLiteral("end")] = Qt::Key_End;
69 m_keyMap[QStringLiteral("left")] = Qt::Key_Left;
70 m_keyMap[QStringLiteral("right")] = Qt::Key_Right;
71 m_keyMap[QStringLiteral("up")] = Qt::Key_Up;
72 m_keyMap[QStringLiteral("down")] = Qt::Key_Down;
73 m_keyMap[QStringLiteral("pageUp")] = Qt::Key_PageUp;
74 m_keyMap[QStringLiteral("pageDown")] = Qt::Key_PageDown;
75 m_keyMap[QStringLiteral("shift")] = Qt::Key_Shift;
76 m_keyMap[QStringLiteral("control")] = Qt::Key_Control;
77 m_keyMap[QStringLiteral("meta")] = Qt::Key_Meta;
78 m_keyMap[QStringLiteral("alt")] = Qt::Key_Alt;
79 m_keyMap[QStringLiteral("capLock")] = Qt::Key_CapsLock;
80 m_keyMap[QStringLiteral("numLock")] = Qt::Key_NumLock;
81 m_keyMap[QStringLiteral("scrollLock")] = Qt::Key_ScrollLock;
82 m_keyMap[QStringLiteral("F1")] = Qt::Key_F1;
83 m_keyMap[QStringLiteral("F2")] = Qt::Key_F2;
84 m_keyMap[QStringLiteral("F3")] = Qt::Key_F3;
85 m_keyMap[QStringLiteral("F4")] = Qt::Key_F4;
86 m_keyMap[QStringLiteral("F5")] = Qt::Key_F5;
87 m_keyMap[QStringLiteral("F6")] = Qt::Key_F6;
88 m_keyMap[QStringLiteral("F7")] = Qt::Key_F7;
89 m_keyMap[QStringLiteral("F8")] = Qt::Key_F8;
90 m_keyMap[QStringLiteral("F9")] = Qt::Key_F9;
91 m_keyMap[QStringLiteral("F10")] = Qt::Key_F10;
92 m_keyMap[QStringLiteral("F11")] = Qt::Key_F11;
93 m_keyMap[QStringLiteral("F12")] = Qt::Key_F12;
94 m_keyMap[QStringLiteral("F13")] = Qt::Key_F13;
95 m_keyMap[QStringLiteral("F14")] = Qt::Key_F14;
96 m_keyMap[QStringLiteral("F15")] = Qt::Key_F15;
97 m_keyMap[QStringLiteral("F16")] = Qt::Key_F16;
98 m_keyMap[QStringLiteral("F17")] = Qt::Key_F17;
99 m_keyMap[QStringLiteral("F18")] = Qt::Key_F18;
100 m_keyMap[QStringLiteral("F19")] = Qt::Key_F19;
101 m_keyMap[QStringLiteral("F20")] = Qt::Key_F20;
102 m_keyMap[QStringLiteral("F21")] = Qt::Key_F21;
103 m_keyMap[QStringLiteral("F22")] = Qt::Key_F22;
104 m_keyMap[QStringLiteral("F23")] = Qt::Key_F23;
105 m_keyMap[QStringLiteral("F24")] = Qt::Key_F24;
106 m_keyMap[QStringLiteral("F25")] = Qt::Key_F25;
107 m_keyMap[QStringLiteral("F26")] = Qt::Key_F26;
108 m_keyMap[QStringLiteral("F27")] = Qt::Key_F27;
109 m_keyMap[QStringLiteral("F28")] = Qt::Key_F28;
110 m_keyMap[QStringLiteral("F29")] = Qt::Key_F29;
111 m_keyMap[QStringLiteral("F30")] = Qt::Key_F30;
112 m_keyMap[QStringLiteral("F31")] = Qt::Key_F31;
113 m_keyMap[QStringLiteral("F32")] = Qt::Key_F32;
114 m_keyMap[QStringLiteral("F33")] = Qt::Key_F33;
115 m_keyMap[QStringLiteral("F34")] = Qt::Key_F34;
116 m_keyMap[QStringLiteral("F35")] = Qt::Key_F35;
117 m_keyMap[QStringLiteral("superL")] = Qt::Key_Super_L;
118 m_keyMap[QStringLiteral("superR")] = Qt::Key_Super_R;
119 m_keyMap[QStringLiteral("menu")] = Qt::Key_Menu;
120 m_keyMap[QStringLiteral("hyperL")] = Qt::Key_Hyper_L;
121 m_keyMap[QStringLiteral("hyperR")] = Qt::Key_Hyper_R;
122 m_keyMap[QStringLiteral("help")] = Qt::Key_Help;
123 m_keyMap[QStringLiteral("directionL")] = Qt::Key_Direction_L;
124 m_keyMap[QStringLiteral("directionR")] = Qt::Key_Direction_R;
125 m_keyMap[QStringLiteral("space")] = Qt::Key_Space;
126 m_keyMap[QStringLiteral("any")] = Qt::Key_Any;
127 m_keyMap[QStringLiteral("exclam")] = Qt::Key_Exclam;
128 m_keyMap[QStringLiteral("quoteDbl")] = Qt::Key_QuoteDbl;
129 m_keyMap[QStringLiteral("numberSign")] = Qt::Key_NumberSign;
130 m_keyMap[QStringLiteral("dollar")] = Qt::Key_Dollar;
131 m_keyMap[QStringLiteral("percent")] = Qt::Key_Percent;
132 m_keyMap[QStringLiteral("ampersand")] = Qt::Key_Ampersand;
133 m_keyMap[QStringLiteral("apostrophe")] = Qt::Key_Apostrophe;
134 m_keyMap[QStringLiteral("parenLeft")] = Qt::Key_ParenLeft;
135 m_keyMap[QStringLiteral("parenRight")] = Qt::Key_ParenRight;
136 m_keyMap[QStringLiteral("asterisk")] = Qt::Key_Asterisk;
137 m_keyMap[QStringLiteral("plus")] = Qt::Key_Plus;
138 m_keyMap[QStringLiteral("comma")] = Qt::Key_Comma;
139 m_keyMap[QStringLiteral("minus")] = Qt::Key_Minus;
140 m_keyMap[QStringLiteral("period")] = Qt::Key_Period;
141 m_keyMap[QStringLiteral("slash")] = Qt::Key_Slash;
142 m_keyMap[QStringLiteral("0")] = Qt::Key_0;
143 m_keyMap[QStringLiteral("1")] = Qt::Key_1;
144 m_keyMap[QStringLiteral("2")] = Qt::Key_2;
145 m_keyMap[QStringLiteral("3")] = Qt::Key_3;
146 m_keyMap[QStringLiteral("4")] = Qt::Key_4;
147 m_keyMap[QStringLiteral("5")] = Qt::Key_5;
148 m_keyMap[QStringLiteral("6")] = Qt::Key_6;
149 m_keyMap[QStringLiteral("7")] = Qt::Key_7;
150 m_keyMap[QStringLiteral("8")] = Qt::Key_8;
151 m_keyMap[QStringLiteral("9")] = Qt::Key_9;
152 m_keyMap[QStringLiteral("colon")] = Qt::Key_Colon;
153 m_keyMap[QStringLiteral("semiColon")] = Qt::Key_Semicolon;
154 m_keyMap[QStringLiteral("less")] = Qt::Key_Less;
155 m_keyMap[QStringLiteral("equal")] = Qt::Key_Equal;
156 m_keyMap[QStringLiteral("greater")] = Qt::Key_Greater;
157 m_keyMap[QStringLiteral("question")] = Qt::Key_Question;
158 m_keyMap[QStringLiteral("at")] = Qt::Key_At;
159 m_keyMap[QStringLiteral("a")] = Qt::Key_A;
160 m_keyMap[QStringLiteral("b")] = Qt::Key_B;
161 m_keyMap[QStringLiteral("c")] = Qt::Key_C;
162 m_keyMap[QStringLiteral("d")] = Qt::Key_D;
163 m_keyMap[QStringLiteral("e")] = Qt::Key_E;
164 m_keyMap[QStringLiteral("f")] = Qt::Key_F;
165 m_keyMap[QStringLiteral("g")] = Qt::Key_G;
166 m_keyMap[QStringLiteral("h")] = Qt::Key_H;
167 m_keyMap[QStringLiteral("i")] = Qt::Key_I;
168 m_keyMap[QStringLiteral("j")] = Qt::Key_J;
169 m_keyMap[QStringLiteral("k")] = Qt::Key_K;
170 m_keyMap[QStringLiteral("l")] = Qt::Key_L;
171 m_keyMap[QStringLiteral("m")] = Qt::Key_M;
172 m_keyMap[QStringLiteral("n")] = Qt::Key_N;
173 m_keyMap[QStringLiteral("o")] = Qt::Key_O;
174 m_keyMap[QStringLiteral("p")] = Qt::Key_P;
175 m_keyMap[QStringLiteral("q")] = Qt::Key_Q;
176 m_keyMap[QStringLiteral("r")] = Qt::Key_R;
177 m_keyMap[QStringLiteral("s")] = Qt::Key_S;
178 m_keyMap[QStringLiteral("t")] = Qt::Key_T;
179 m_keyMap[QStringLiteral("u")] = Qt::Key_U;
180 m_keyMap[QStringLiteral("v")] = Qt::Key_V;
181 m_keyMap[QStringLiteral("w")] = Qt::Key_W;
182 m_keyMap[QStringLiteral("x")] = Qt::Key_X;
183 m_keyMap[QStringLiteral("y")] = Qt::Key_Y;
184 m_keyMap[QStringLiteral("z")] = Qt::Key_Z;
185 m_keyMap[QStringLiteral("bracketLeft")] = Qt::Key_BracketLeft;
186 m_keyMap[QStringLiteral("backslash")] = Qt::Key_Backslash;
187 m_keyMap[QStringLiteral("bracketRight")] = Qt::Key_BracketRight;
188 m_keyMap[QStringLiteral("asciiCircum")] = Qt::Key_AsciiCircum;
189 m_keyMap[QStringLiteral("underscore")] = Qt::Key_Underscore;
190 m_keyMap[QStringLiteral("quoteLeft")] = Qt::Key_QuoteLeft;
191 m_keyMap[QStringLiteral("braceLeft")] = Qt::Key_BraceLeft;
192 m_keyMap[QStringLiteral("bar")] = Qt::Key_Bar;
193 m_keyMap[QStringLiteral("braceRight")] = Qt::Key_BraceRight;
194 m_keyMap[QStringLiteral("asciiTilde")] = Qt::Key_AsciiTilde;
195 m_keyMap[QStringLiteral("plusminus")] = Qt::Key_plusminus;
196 m_keyMap[QStringLiteral("onesuperior")] = Qt::Key_onesuperior;
197 m_keyMap[QStringLiteral("multiply")] = Qt::Key_multiply;
198 m_keyMap[QStringLiteral("division")] = Qt::Key_division;
199 m_keyMap[QStringLiteral("diaeresis")] = Qt::Key_diaeresis;
200
201 m_keyNames = m_keyMap.keys();
202}
203
204/*!
205 \class Qt3DInput::QKeyboardDevice
206 \inmodule Qt3DInput
207 \brief QKeyboardDevice is in charge of dispatching keyboard events to
208 attached QQKeyboardHandler objects.
209 \since 5.5
210*/
211
212/*!
213 \qmltype KeyboardDevice
214 \inqmlmodule Qt3D.Input
215 \brief QML frontend for QKeyboardDevice C++ class.
216 \since 5.5
217 \instantiates Qt3DInput::QKeyboardDevice
218 \inherits Node
219*/
220
221/*!
222 Constructs a new QKeyboardDevice instance with \a parent.
223 */
224QKeyboardDevice::QKeyboardDevice(QNode *parent)
225 : QAbstractPhysicalDevice(*new QKeyboardDevicePrivate, parent)
226{
227}
228
229/*! \internal */
230QKeyboardDevice::~QKeyboardDevice()
231{
232}
233
234/*!
235 \qmlproperty KeyboardHandler Qt3D.Input::KeyboardDevice::activeInput
236 \readonly
237*/
238
239/*!
240 \property QKeyboardDevice::activeInput
241
242 Holds the active QKeyboardHandler of the device.
243 */
244QKeyboardHandler *QKeyboardDevice::activeInput() const
245{
246 Q_D(const QKeyboardDevice);
247 return d->m_activeInput;
248}
249
250/*!
251 \return the axis count.
252
253 \note Currently always returns zero.
254 */
255int QKeyboardDevice::axisCount() const
256{
257 return 0;
258}
259
260/*!
261 \return the button count.
262 */
263int QKeyboardDevice::buttonCount() const
264{
265 Q_D(const QKeyboardDevice);
266 return d->m_keyNames.size();
267}
268
269/*!
270 \return the axis names.
271
272 \note Currently always returns empty QStringList.
273 */
274QStringList QKeyboardDevice::axisNames() const
275{
276 return QStringList();
277}
278
279/*!
280 \return the button names.
281 */
282QStringList QKeyboardDevice::buttonNames() const
283{
284 Q_D(const QKeyboardDevice);
285 return d->m_keyNames;
286}
287
288/*!
289 \return the axisIdentifier matching the \a name.
290 */
291int QKeyboardDevice::axisIdentifier(const QString &name) const
292{
293 Q_UNUSED(name);
294 return 0;
295}
296
297/*!
298 \return the buttonIdentifier matching the \a name.
299 */
300int QKeyboardDevice::buttonIdentifier(const QString &name) const
301{
302 Q_D(const QKeyboardDevice);
303 return d->m_keyMap.value(akey: name, adefaultValue: 0);
304}
305
306/*! \internal */
307QKeyboardDevice::QKeyboardDevice(QKeyboardDevicePrivate &dd, QNode *parent)
308 : QAbstractPhysicalDevice(dd, parent)
309{
310}
311
312// TODO Unused remove in Qt6
313void QKeyboardDevice::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &)
314{
315}
316
317/*!
318 * Set the active input to \a activeInput
319 */
320void QKeyboardDevice::setActiveInput(QKeyboardHandler *activeInput)
321{
322 Q_D(QKeyboardDevice);
323 if (d->m_activeInput != activeInput) {
324 d->m_activeInput = activeInput;
325 emit activeInputChanged(activeInput);
326 }
327}
328
329} // namespace Qt3DInput
330
331QT_END_NAMESPACE
332

source code of qt3d/src/input/frontend/qkeyboarddevice.cpp