如何在QT虚拟键盘上按下信号键并播放声音点击轨道?

How to get signal key pressed on QT virtual keyboard and play a sound click track?

在我的嵌入式设备 QT 应用程序中,我想在 QML 虚拟键盘的按键事件中播放声音。我能得到这个事件吗?以及如何得到它? 我已经有一个 class 可以在单击我在其他 qml 页面中使用的按钮时播放声音(点击效果)

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
    }
}

你有两个选择:

  1. 使用BaseKeyclicked()信号。
  2. 使用 KeyPanelsoundEffect 属性。

两者都需要有自己的风格。您可以阅读有关创建自己的样式的更多信息 here。从那里引用:

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". [...]

您可以创建一个 class Mykeyfilter,它是一个 QObject class

然后在您的文件 .h 中声明:

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

然后在您的 Mykeyfilter.cpp 文件中定义 eventFilter 如下:

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);

}

同时在您的 main.cpp 文件中添加:

#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();
}