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

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