Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scene3D keyboard input



  • Hi,

    I'm trying to receive keyboard input in my Scene3D, but I fail to get it.
    I have an ApplicationWindow with a Scene3D and an Entity inside. On C++ side I have a QQmlApplicationEngine and QGuiApplication.

    Here's the code:

    Main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
    	QGuiApplication app(argc, argv);
    
    	QQmlApplicationEngine engine;
    
    	engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
    
    	return app.exec();
    }
    

    MainWindow.qml:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    
    import QtQuick.Scene3D 2.13
    import Qt3D.Core 2.0
    import Qt3D.Render 2.0
    import Qt3D.Input 2.0
    import Qt3D.Extras 2.0
    
    import QtQuick 2.0 as QQ2
    
    
    ApplicationWindow {
        id: mainApplicationWindow
    
        width: 640
        height: 480
    
        visible: true
    
        title: qsTr("TestTemplateProject")
    
        Scene3D {
            id: scene3D
            anchors.fill: parent
    
            aspects: ["input", "logic"]
            cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
    
            //focus: true;
            Entity {
                id: sceneRoot
    
                Camera {
                    id: camera
                    projectionType: CameraLens.PerspectiveProjection
                    fieldOfView: 45
                    nearPlane: 0.1
                    farPlane: 1000.0
                    position: Qt.vector3d(0.0, 0.0, 40.0)
                    upVector: Qt.vector3d(0.0, 1.0, 0.0)
                    viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
                }
    
                FirstPersonCameraController {
                    camera: camera
                }
    
                KeyboardDevice { id: keyboard1 }
    
                KeyboardHandler {
                    id: input
                    sourceDevice: keyboard1
                    focus: true
    
                    onTabPressed: {
                        console.log("Tab");
                    }
    
                    onSpacePressed: {
                        console.log("Test");
                    }
                }
    
                RenderSettings {
                    id: renderSettings
                    activeFrameGraph: ForwardRenderer {
                        camera: camera
                    }
                }
    
                components: [ renderSettings, input, inputSettings ]
    
                InputSettings { id: inputSettings; }
    
                PhongMaterial {
                    id: material
                }
    
                TorusMesh {
                    id: torusMesh
                    radius: 5
                    minorRadius: 1
                    rings: 100
                    slices: 20
                }
    
                Transform {
                    id: torusTransform
                    scale3D: Qt.vector3d(1.5, 1, 0.5)
                    rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45)
                }
    
                Entity {
                    id: torusEntity
                    components: [torusMesh, material, torusTransform]
                }
            }
        }
    
        Component.onCompleted:
        {
            console.log("Test");
            mainApplicationWindow.showMaximized();
        }
    }
    
    

    When I use a Qt3DQuickWindow on C++ side and just the Entity in qml (like in the Qt3D wave example), everything seems to work fine.
    What am I doing wrong?
    Regards,

    Jan



  • Hi,

    You need to uncomment the line "//focus: true;".
    Focus is required to get keyboards events in your Scene3D.

    Regards,


Log in to reply