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 "keyboarddevice_p.h"
5
6#include <Qt3DInput/QKeyEvent>
7#include <Qt3DCore/qnode.h>
8
9#include <Qt3DInput/private/inputhandler_p.h>
10#include <Qt3DInput/private/inputmanagers_p.h>
11
12
13QT_BEGIN_NAMESPACE
14
15namespace Qt3DInput {
16namespace Input {
17
18namespace {
19
20QPair<int, int> getKeyPosition(int key)
21{
22 QPair<int, int> position;
23
24 switch (key) {
25 case Qt::Key_Escape:
26 return QPair<int, int>(0, 0);
27 case Qt::Key_Tab:
28 return QPair<int, int>(0, 1);
29 case Qt::Key_Backtab:
30 return QPair<int, int>(0, 2);
31 case Qt::Key_Backspace:
32 return QPair<int, int>(0, 3);
33 case Qt::Key_Return:
34 return QPair<int, int>(0, 4);
35 case Qt::Key_Enter:
36 return QPair<int, int>(0, 5);
37 case Qt::Key_Insert:
38 return QPair<int, int>(0, 6);
39 case Qt::Key_Delete:
40 return QPair<int, int>(0, 7);
41 case Qt::Key_Pause:
42 return QPair<int, int>(0, 8);
43 case Qt::Key_Print:
44 return QPair<int, int>(0, 9);
45 case Qt::Key_SysReq:
46 return QPair<int, int>(0, 10);
47 case Qt::Key_Clear:
48 return QPair<int, int>(0, 11);
49 case Qt::Key_Home:
50 return QPair<int, int>(0, 12);
51 case Qt::Key_End:
52 return QPair<int, int>(0, 13);
53 case Qt::Key_Left:
54 return QPair<int, int>(0, 14);
55 case Qt::Key_Right:
56 return QPair<int, int>(0, 16);
57 case Qt::Key_Up:
58 return QPair<int, int>(0, 15);
59 case Qt::Key_Down:
60 return QPair<int, int>(0, 17);
61 case Qt::Key_PageUp:
62 return QPair<int, int>(0, 18);
63 case Qt::Key_PageDown:
64 return QPair<int, int>(0, 19);
65 case Qt::Key_Shift:
66 return QPair<int, int>(0, 20);
67 case Qt::Key_Alt:
68 return QPair<int, int>(0, 23);
69 case Qt::Key_Control:
70 return QPair<int, int>(0, 21);
71 case Qt::Key_Meta:
72 return QPair<int, int>(0, 22);
73 case Qt::Key_CapsLock:
74 return QPair<int, int>(0, 24);
75 case Qt::Key_NumLock:
76 return QPair<int, int>(0, 25);
77 case Qt::Key_ScrollLock:
78 return QPair<int, int>(0, 26);
79 case Qt::Key_F1:
80 return QPair<int, int>(0, 27);
81 case Qt::Key_F2:
82 return QPair<int, int>(0, 28);
83 case Qt::Key_F3:
84 return QPair<int, int>(0, 29);
85 case Qt::Key_F4:
86 return QPair<int, int>(0, 30);
87 case Qt::Key_F5:
88 return QPair<int, int>(0, 31);
89 case Qt::Key_F6:
90 return QPair<int, int>(1, 0);
91 case Qt::Key_F7:
92 return QPair<int, int>(1, 1);
93 case Qt::Key_F8:
94 return QPair<int, int>(1, 2);
95 case Qt::Key_F9:
96 return QPair<int, int>(1, 3);
97 case Qt::Key_F10:
98 return QPair<int, int>(1, 4);
99 case Qt::Key_F11:
100 return QPair<int, int>(1, 5);
101 case Qt::Key_F12:
102 return QPair<int, int>(1, 6);
103 case Qt::Key_F13:
104 return QPair<int, int>(1, 7);
105 case Qt::Key_F14:
106 return QPair<int, int>(1, 8);
107 case Qt::Key_F15:
108 return QPair<int, int>(1, 9);
109 case Qt::Key_F16:
110 return QPair<int, int>(1, 10);
111 case Qt::Key_F17:
112 return QPair<int, int>(1, 11);
113 case Qt::Key_F18:
114 return QPair<int, int>(1, 12);
115 case Qt::Key_F19:
116 return QPair<int, int>(1, 13);
117 case Qt::Key_F20:
118 return QPair<int, int>(1, 14);
119 case Qt::Key_F21:
120 return QPair<int, int>(1, 15);
121 case Qt::Key_F22:
122 return QPair<int, int>(1, 16);
123 case Qt::Key_F23:
124 return QPair<int, int>(1, 17);
125 case Qt::Key_F24:
126 return QPair<int, int>(1, 18);
127 case Qt::Key_F25:
128 return QPair<int, int>(1, 19);
129 case Qt::Key_F26:
130 return QPair<int, int>(1, 20);
131 case Qt::Key_F27:
132 return QPair<int, int>(1, 21);
133 case Qt::Key_F28:
134 return QPair<int, int>(1, 22);
135 case Qt::Key_F29:
136 return QPair<int, int>(1, 23);
137 case Qt::Key_F30:
138 return QPair<int, int>(1, 24);
139 case Qt::Key_F31:
140 return QPair<int, int>(1, 25);
141 case Qt::Key_F32:
142 return QPair<int, int>(1, 26);
143 case Qt::Key_F33:
144 return QPair<int, int>(1, 27);
145 case Qt::Key_F34:
146 return QPair<int, int>(1, 28);
147 case Qt::Key_F35:
148 return QPair<int, int>(1, 29);
149 case Qt::Key_Super_L:
150 return QPair<int, int>(1, 30);
151 case Qt::Key_Super_R:
152 return QPair<int, int>(1, 31);
153 case Qt::Key_Menu:
154 return QPair<int, int>(2, 1);
155 case Qt::Key_Help:
156 return QPair<int, int>(2, 4);
157 case Qt::Key_Hyper_L:
158 return QPair<int, int>(2, 2);
159 case Qt::Key_Hyper_R:
160 return QPair<int, int>(2, 3);
161 case Qt::Key_Direction_L:
162 return QPair<int, int>(2, 5);
163 case Qt::Key_Direction_R:
164 return QPair<int, int>(2, 6);
165 case Qt::Key_Space:
166 return QPair<int, int>(2, 7);
167 case Qt::Key_Exclam:
168 return QPair<int, int>(2, 9);
169 case Qt::Key_QuoteDbl:
170 return QPair<int, int>(2, 10);
171 case Qt::Key_NumberSign:
172 return QPair<int, int>(2, 11);
173 case Qt::Key_Dollar:
174 return QPair<int, int>(2, 12);
175 case Qt::Key_Percent:
176 return QPair<int, int>(2, 13);
177 case Qt::Key_Ampersand:
178 return QPair<int, int>(2, 14);
179 case Qt::Key_Apostrophe:
180 return QPair<int, int>(2, 15);
181 case Qt::Key_ParenLeft:
182 return QPair<int, int>(2, 16);
183 case Qt::Key_ParenRight:
184 return QPair<int, int>(2, 17);
185 case Qt::Key_Asterisk:
186 return QPair<int, int>(2, 18);
187 case Qt::Key_Plus:
188 return QPair<int, int>(2, 19);
189 case Qt::Key_Comma:
190 return QPair<int, int>(2, 20);
191 case Qt::Key_Minus:
192 return QPair<int, int>(2, 21);
193 case Qt::Key_Period:
194 return QPair<int, int>(2, 22);
195 case Qt::Key_Slash:
196 return QPair<int, int>(2, 23);
197 case Qt::Key_0:
198 return QPair<int, int>(2, 24);
199 case Qt::Key_1:
200 return QPair<int, int>(2, 25);
201 case Qt::Key_2:
202 return QPair<int, int>(2, 26);
203 case Qt::Key_3:
204 return QPair<int, int>(2, 27);
205 case Qt::Key_4:
206 return QPair<int, int>(2, 28);
207 case Qt::Key_5:
208 return QPair<int, int>(2, 29);
209 case Qt::Key_6:
210 return QPair<int, int>(2, 30);
211 case Qt::Key_7:
212 return QPair<int, int>(2, 31);
213 case Qt::Key_8:
214 return QPair<int, int>(3, 0);
215 case Qt::Key_9:
216 return QPair<int, int>(3, 1);
217 case Qt::Key_Colon:
218 return QPair<int, int>(3, 2);
219 case Qt::Key_Semicolon:
220 return QPair<int, int>(3, 3);
221 case Qt::Key_Less:
222 return QPair<int, int>(3, 4);
223 case Qt::Key_Equal:
224 return QPair<int, int>(3, 5);
225 case Qt::Key_Greater:
226 return QPair<int, int>(3, 6);
227 case Qt::Key_Question:
228 return QPair<int, int>(3, 7);
229 case Qt::Key_At:
230 return QPair<int, int>(3, 8);
231 case Qt::Key_A:
232 return QPair<int, int>(3, 9);
233 case Qt::Key_B:
234 return QPair<int, int>(3, 10);
235 case Qt::Key_C:
236 return QPair<int, int>(3, 11);
237 case Qt::Key_D:
238 return QPair<int, int>(3, 12);
239 case Qt::Key_E:
240 return QPair<int, int>(3, 13);
241 case Qt::Key_F:
242 return QPair<int, int>(3, 14);
243 case Qt::Key_G:
244 return QPair<int, int>(3, 15);
245 case Qt::Key_H:
246 return QPair<int, int>(3, 16);
247 case Qt::Key_I:
248 return QPair<int, int>(3, 17);
249 case Qt::Key_J:
250 return QPair<int, int>(3, 18);
251 case Qt::Key_K:
252 return QPair<int, int>(3, 19);
253 case Qt::Key_L:
254 return QPair<int, int>(3, 20);
255 case Qt::Key_M:
256 return QPair<int, int>(3, 21);
257 case Qt::Key_N:
258 return QPair<int, int>(3, 22);
259 case Qt::Key_O:
260 return QPair<int, int>(3, 23);
261 case Qt::Key_P:
262 return QPair<int, int>(3, 24);
263 case Qt::Key_Q:
264 return QPair<int, int>(3, 25);
265 case Qt::Key_R:
266 return QPair<int, int>(3, 26);
267 case Qt::Key_S:
268 return QPair<int, int>(3, 27);
269 case Qt::Key_T:
270 return QPair<int, int>(3, 28);
271 case Qt::Key_U:
272 return QPair<int, int>(3, 29);
273 case Qt::Key_V:
274 return QPair<int, int>(3, 30);
275 case Qt::Key_W:
276 return QPair<int, int>(3, 31);
277 case Qt::Key_X:
278 return QPair<int, int>(4, 0);
279 case Qt::Key_Y:
280 return QPair<int, int>(4, 1);
281 case Qt::Key_Z:
282 return QPair<int, int>(4, 2);
283 case Qt::Key_BracketLeft:
284 return QPair<int, int>(4, 3);
285 case Qt::Key_Backslash:
286 return QPair<int, int>(4, 4);
287 case Qt::Key_BracketRight:
288 return QPair<int, int>(4, 5);
289 case Qt::Key_AsciiCircum:
290 return QPair<int, int>(4, 6);
291 case Qt::Key_Underscore:
292 return QPair<int, int>(4, 7);
293 case Qt::Key_QuoteLeft:
294 return QPair<int, int>(4, 8);
295 case Qt::Key_BraceLeft:
296 return QPair<int, int>(4, 9);
297 case Qt::Key_Bar:
298 return QPair<int, int>(4, 10);
299 case Qt::Key_BraceRight:
300 return QPair<int, int>(4, 11);
301 case Qt::Key_AsciiTilde:
302 return QPair<int, int>(4, 12);
303 case Qt::Key_plusminus:
304 return QPair<int, int>(4, 13);
305 case Qt::Key_onesuperior:
306 return QPair<int, int>(4, 14);
307 case Qt::Key_multiply:
308 return QPair<int, int>(4, 15);
309 case Qt::Key_division:
310 return QPair<int, int>(4, 16);
311 case Qt::Key_diaeresis:
312 return QPair<int, int>(4, 17);
313 default:
314 return QPair<int, int>(-1, -1);
315 }
316 return position;
317}
318
319} // anonymous
320
321
322// TO DO: Send change to frontend when activeInput changes
323
324KeyboardDevice::KeyboardDevice()
325 : QAbstractPhysicalDeviceBackendNode(QBackendNode::ReadOnly)
326 , m_inputHandler(nullptr)
327{
328 m_keyStates.keys[0] = 0;
329 m_keyStates.keys[1] = 0;
330 m_keyStates.keys[2] = 0;
331 m_keyStates.keys[3] = 0;
332 m_keyStates.keys[4] = 0;
333}
334
335void KeyboardDevice::cleanup()
336{
337 QAbstractPhysicalDeviceBackendNode::cleanup();
338 m_keyStates.keys[0] = 0;
339 m_keyStates.keys[1] = 0;
340 m_keyStates.keys[2] = 0;
341 m_keyStates.keys[3] = 0;
342 m_keyStates.keys[4] = 0;
343}
344
345void KeyboardDevice::requestFocusForInput(Qt3DCore::QNodeId inputId)
346{
347 // Saves the last inputId, this will then be used in an Aspect Job to determine which
348 // input will have the focus. This in turn will call KeyboardInput::setFocus which will
349 // decide if sending a notification to the frontend is necessary
350 m_lastRequester = inputId;
351}
352
353void KeyboardDevice::setInputHandler(InputHandler *handler)
354{
355 m_inputHandler = handler;
356}
357
358void KeyboardDevice::setCurrentFocusItem(Qt3DCore::QNodeId input)
359{
360 m_currentFocusItem = input;
361}
362
363float KeyboardDevice::axisValue(int axisIdentifier) const
364{
365 Q_UNUSED(axisIdentifier);
366 return 0.0f;
367}
368
369bool KeyboardDevice::isButtonPressed(int buttonIdentifier) const
370{
371 QPair<int, int> position = getKeyPosition(key: buttonIdentifier);
372 if (position.first != -1 && position.second != -1)
373 return m_keyStates.keys[position.first] & (1 << position.second);
374 return false;
375}
376
377void KeyboardDevice::setButtonValue(int key, bool value)
378{
379 QPair<int, int> position = getKeyPosition(key);
380 if (position.first != -1 && position.second != -1) {
381 if (value)
382 m_keyStates.keys[position.first] |= (1 << position.second);
383 else
384 m_keyStates.keys[position.first] &= ~(1 << position.second);
385 }
386}
387
388void KeyboardDevice::updateKeyEvent(QT_PREPEND_NAMESPACE(QKeyEvent) *event)
389{
390 setButtonValue(key: event->key(), value: event->type() == QT_PREPEND_NAMESPACE(QKeyEvent)::KeyPress ? true : false);
391}
392
393KeyboardDeviceFunctor::KeyboardDeviceFunctor(QInputAspect *inputaspect, InputHandler *handler)
394 : m_inputAspect(inputaspect)
395 , m_handler(handler)
396{
397}
398
399Qt3DCore::QBackendNode *KeyboardDeviceFunctor::create(Qt3DCore::QNodeId id) const
400{
401 KeyboardDevice *keyboardDevice = m_handler->keyboardDeviceManager()->getOrCreateResource(id);
402 keyboardDevice->setInputAspect(m_inputAspect);
403 keyboardDevice->setInputHandler(m_handler);
404 m_handler->appendKeyboardDevice(device: m_handler->keyboardDeviceManager()->lookupHandle(id));
405 return keyboardDevice;
406}
407
408Qt3DCore::QBackendNode *KeyboardDeviceFunctor::get(Qt3DCore::QNodeId id) const
409{
410 return m_handler->keyboardDeviceManager()->lookupResource(id);
411}
412
413void KeyboardDeviceFunctor::destroy(Qt3DCore::QNodeId id) const
414{
415 m_handler->removeKeyboardDevice(device: m_handler->keyboardDeviceManager()->lookupHandle(id));
416 m_handler->keyboardDeviceManager()->releaseResource(id);
417}
418
419} // namespace Inputs
420} // namespace Qt3DInput
421
422QT_END_NAMESPACE
423

source code of qt3d/src/input/backend/keyboarddevice.cpp