3

in my QT application for embedded device , i want play a sound on key pressed event of the QML virtualkeyboard . Can I get this event? and How get it? I already have a class that play sound ( click effect) when a button was clicked that i use in the others qml pages

import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
import QtQuick.VirtualKeyboard 2.1

Page{
    id: pag
    width:  1280
    height: 800
    background:  Rectangle { color: "black"} 
    TextField {
        id: txtName
        height: 200
        width:200
        anchors.horizontalcenter:parent.horizontalCenter
        font.family: "Arial
            font.pixelSize: 24
             placeholderText: "insert your text here"
            background: Rectangle {
                anchors.fill: parent
                color: "transparent"
            }
    }
    InputPanel {
        id: virtualkeyboard
        width: 0.95*parent.width
        anchors.bottom: parent.bottom
    }
}
Mitch
  • 23,716
  • 9
  • 83
  • 122
pablodepas87
  • 197
  • 1
  • 16
  • See [this](https://doc.qt.io/qt-5/technical-guide.html) article to get more info about customizing your virtual keyboard. – folibis Oct 02 '17 at 07:24

2 Answers2

2

You can create a class Mykeyfilter, it's a QObject class

then in your file .h you declare:

bool eventFilter(QObject *object, QEvent *event);

Then in your Mykeyfilter.cpp file you define eventFilter like this:

bool MykeyFilter::eventFilter(QObject *object, QEvent *event)
{
    switch(event->type())
    {
    case QEvent::KeyPress:
    case QEvent::KeyRelease:
        {
         //////call your sound class here that you want to play/////
         qDebug()<<"I have clicked"  //For testing
        }
    default:
        break;

        //         return QObject::eventFilter(object, event);
    }

    return QObject::eventFilter(object, event);

}

Also add in your main.cpp file:

#include "mytouchfilter.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    app.installEventFilter(new MykeyFilter());

    return app.exec();
}
sebba23
  • 544
  • 7
  • 24
  • This doesn't work for mobile (android) , this code is for Desktops. On Android you get Key events only when user presses 'Done' button on virtual keyboard. But it doesn't work for intercepting. Also, there is no need to install event fileter since TextEdit has event handlers already and they can catch key events. – Nulik Jan 30 '19 at 23:42
1

You have two options:

  1. Use the clicked() signal of BaseKey.
  2. Use the soundEffect property of KeyPanel.

Both require having your own style. You can read more about creating your own style here. To quote from there:

A good starting point for creating a new style is to use an existing built-in style as a template and edit it. You can find the built-in styles from the virtual keyboard sources directory src/virtualkeyboard/content/styles. Copy one of the directories containing a built-in style into the Styles directory and rename it to "test". [...]

Mitch
  • 23,716
  • 9
  • 83
  • 122