├── .gitignore ├── LICENSE ├── README.md ├── deployment.pri ├── key_emitter.cpp ├── key_emitter.h ├── main.cpp ├── main.qml ├── qml.qrc └── qt-virt-keyboard.pro /.gitignore: -------------------------------------------------------------------------------- 1 | qt-virt-keyboard.pro.user 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Michael Egli 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Virtual Keyboard in Qt 2 | ====================== 3 | 4 | This is an example that show how to build a virtual keyboard in Qt with QML. 5 | 6 | Please the the [Article][0] on our Website for more information. 7 | 8 | [0]: http://wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/ 9 | -------------------------------------------------------------------------------- /deployment.pri: -------------------------------------------------------------------------------- 1 | android-no-sdk { 2 | target.path = /data/user/qt 3 | export(target.path) 4 | INSTALLS += target 5 | } else:android { 6 | x86 { 7 | target.path = /libs/x86 8 | } else: armeabi-v7a { 9 | target.path = /libs/armeabi-v7a 10 | } else { 11 | target.path = /libs/armeabi 12 | } 13 | export(target.path) 14 | INSTALLS += target 15 | } else:unix { 16 | isEmpty(target.path) { 17 | qnx { 18 | target.path = /tmp/$${TARGET}/bin 19 | } else { 20 | target.path = /opt/$${TARGET}/bin 21 | } 22 | export(target.path) 23 | } 24 | INSTALLS += target 25 | } 26 | 27 | export(INSTALLS) 28 | -------------------------------------------------------------------------------- /key_emitter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "key_emitter.h" 6 | 7 | KeyEmitter::KeyEmitter() 8 | { 9 | } 10 | 11 | KeyEmitter::~KeyEmitter() 12 | { 13 | } 14 | 15 | void KeyEmitter::emitKey(Qt::Key key) 16 | { 17 | QQuickItem* receiver = qobject_cast(QGuiApplication::focusObject()); 18 | if(!receiver) { 19 | return; 20 | } 21 | QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString()); 22 | QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier); 23 | QCoreApplication::sendEvent(receiver, &pressEvent); 24 | QCoreApplication::sendEvent(receiver, &releaseEvent); 25 | } 26 | -------------------------------------------------------------------------------- /key_emitter.h: -------------------------------------------------------------------------------- 1 | #ifndef KEY_EMITTER_H 2 | #define KEY_EMITTER_H 3 | 4 | #include 5 | 6 | class KeyEmitter : public QObject 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | KeyEmitter(); 12 | ~KeyEmitter(); 13 | 14 | public slots: 15 | void emitKey(Qt::Key key); 16 | }; 17 | 18 | #endif // KEY_EMITTER_H 19 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "key_emitter.h" 5 | 6 | int main(int argc, char* argv[]) 7 | { 8 | QGuiApplication app(argc, argv); 9 | QQmlApplicationEngine engine; 10 | KeyEmitter keyEmitter; 11 | QQmlContext* ctx = engine.rootContext(); 12 | ctx->setContextProperty("keyEmitter", &keyEmitter); 13 | engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); 14 | return app.exec(); 15 | } 16 | -------------------------------------------------------------------------------- /main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.4 2 | import QtQuick.Controls 1.3 3 | import QtQuick.Window 2.2 4 | 5 | Window { 6 | id: mainWindow 7 | width: 250 8 | height: 150 9 | visible: true 10 | 11 | TextField { 12 | id: inputField 13 | anchors.horizontalCenter: parent.horizontalCenter 14 | height: 40 15 | text: "123" 16 | onAccepted: focus = false 17 | Keys.onEscapePressed: undo() 18 | } 19 | 20 | Row { 21 | id: keyRow1 22 | anchors.horizontalCenter: parent.horizontalCenter 23 | anchors.top: inputField.bottom 24 | anchors.topMargin: 20 25 | Button { 26 | id: button1 27 | text: qsTr("1") 28 | onClicked: keyEmitter.emitKey(Qt.Key_1) 29 | } 30 | Button { 31 | id: button2 32 | text: qsTr("2") 33 | onClicked: keyEmitter.emitKey(Qt.Key_2) 34 | } 35 | Button { 36 | id: button3 37 | text: qsTr("3") 38 | onClicked: keyEmitter.emitKey(Qt.Key_3) 39 | } 40 | } 41 | Row { 42 | id: keyRow2 43 | anchors.horizontalCenter: parent.horizontalCenter 44 | anchors.top: keyRow1.bottom 45 | Button { 46 | id: buttonBack 47 | text: qsTr("DEL") 48 | onClicked: keyEmitter.emitKey(Qt.Key_Backspace) 49 | } 50 | Button { 51 | id: buttonEnter 52 | text: qsTr("OK") 53 | onClicked: keyEmitter.emitKey(Qt.Key_Enter) 54 | } 55 | Button { 56 | id: buttonEsc 57 | text: qsTr("ESC") 58 | onClicked: keyEmitter.emitKey(Qt.Key_Escape) 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /qt-virt-keyboard.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | 3 | QT += qml quick 4 | 5 | SOURCES += main.cpp \ 6 | key_emitter.cpp 7 | 8 | RESOURCES += qml.qrc 9 | 10 | # Additional import path used to resolve QML modules in Qt Creator's code model 11 | QML_IMPORT_PATH = 12 | 13 | # Default rules for deployment. 14 | include(deployment.pri) 15 | 16 | HEADERS += \ 17 | key_emitter.h 18 | --------------------------------------------------------------------------------