1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5#include "qguiapplication.h"
6
7#include <private/qobject_p.h>
8#include "qkeymapper_p.h"
9
10#include <private/qguiapplication_p.h>
11#include <qpa/qplatformintegration.h>
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \class QKeyMapper
17 \since 4.2
18 \internal
19
20 \sa QObject
21*/
22
23/*!
24 Constructs a new key mapper.
25*/
26QKeyMapper::QKeyMapper()
27 : QObject(*new QKeyMapperPrivate, nullptr)
28{
29}
30
31/*!
32 Destroys the key mapper.
33*/
34QKeyMapper::~QKeyMapper()
35{
36}
37
38static QList<int> extractKeyFromEvent(QKeyEvent *e)
39{
40 QList<int> result;
41 if (e->key() && (e->key() != Qt::Key_unknown))
42 result << e->keyCombination().toCombined();
43 else if (!e->text().isEmpty())
44 result << int(e->text().at(i: 0).unicode() + (int)e->modifiers());
45 return result;
46}
47
48QList<int> QKeyMapper::possibleKeys(QKeyEvent *e)
49{
50 return instance()->d_func()->possibleKeys(e);
51}
52
53extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); // in qapplication_*.cpp
54void QKeyMapper::changeKeyboard()
55{
56 // ## TODO: Support KeyboardLayoutChange on QPA
57#if 0
58 // inform all toplevel widgets of the change
59 QEvent e(QEvent::KeyboardLayoutChange);
60 QWidgetList list = QApplication::topLevelWidgets();
61 for (int i = 0; i < list.size(); ++i) {
62 QWidget *w = list.at(i);
63 qt_sendSpontaneousEvent(w, &e);
64 }
65#endif
66}
67
68Q_GLOBAL_STATIC(QKeyMapper, keymapper)
69/*!
70 Returns the pointer to the single instance of QKeyMapper in the application.
71 If none yet exists, the function ensures that one is created.
72*/
73QKeyMapper *QKeyMapper::instance()
74{
75 return keymapper();
76}
77
78QKeyMapperPrivate *qt_keymapper_private()
79{
80 return QKeyMapper::instance()->d_func();
81}
82
83QKeyMapperPrivate::QKeyMapperPrivate()
84{
85 keyboardInputLocale = QLocale::system();
86 keyboardInputDirection = keyboardInputLocale.textDirection();
87}
88
89QKeyMapperPrivate::~QKeyMapperPrivate()
90{
91}
92
93QList<int> QKeyMapperPrivate::possibleKeys(QKeyEvent *e)
94{
95 QList<int> result = QGuiApplicationPrivate::platformIntegration()->possibleKeys(e);
96 if (!result.isEmpty())
97 return result;
98
99 return extractKeyFromEvent(e);
100}
101
102void *QKeyMapper::resolveInterface(const char *name, int revision) const
103{
104 Q_UNUSED(name); Q_UNUSED(revision);
105 using namespace QNativeInterface::Private;
106
107#if QT_CONFIG(evdev)
108 QT_NATIVE_INTERFACE_RETURN_IF(QEvdevKeyMapper, QGuiApplicationPrivate::platformIntegration());
109#endif
110
111 return nullptr;
112}
113
114QT_END_NAMESPACE
115
116#include "moc_qkeymapper_p.cpp"
117

source code of qtbase/src/gui/kernel/qkeymapper.cpp