Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for WebAssembly
  4. Dealing with keyboard layout for input on Qt WebAssembly
QtWS25 Last Chance

Dealing with keyboard layout for input on Qt WebAssembly

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
2 Posts 1 Posters 413 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by 8Observer8
    #1

    I am writing the Super Mario clone game for learning a programming, OpenGL ES, Box2D, OpenAL and so on (not for commerce). A keyboard works well when the system keyboard layout is set to English. But if the system keyboard layout is set to another language Mario cannot move and jump.

    I solved this problem for Windows only by setting the system keyboard layout to English using WinAPI but how to solve it for WebAssembly?

    #ifdef _WIN32
    #include <windows.h>
    #endif
    
    OpenGLWindow::OpenGLWindow()
    {
    #ifdef _WIN32
        PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, 1, 0x04090409);
    #endif
    

    Download my example to reproduce the problem: keyboard-layout-qt6.zip. If your system keyboard layout is English you can press WAS or arrow-keys and see "left", "right", "jump" in the debug console. But if you change the system keyboard layout to another language you do not see the debug output.

    keyboard-layout-qt6.pro

    QT       += core opengl gui widgets
    
    win32: LIBS += -lopengl32
    
    CONFIG += c++17
    
    SOURCES += \
        main.cpp
    

    main.pp

    #include <QtGui/QKeyEvent>
    #include <QtGui/QOpenGLFunctions>
    #include <QtOpenGL/QOpenGLWindow>
    #include <QtWidgets/QApplication>
    
    class OpenGLWindow : public QOpenGLWindow, private QOpenGLFunctions
    {
        void initializeGL() override
        {
            initializeOpenGLFunctions();
        }
    
        void keyPressEvent(QKeyEvent *event) override
        {
            if (event->key() == Qt::Key::Key_W || event->key() == Qt::Key::Key_Up)
            {
                qDebug() << "jump";
            }
            if (event->key() == Qt::Key::Key_A || event->key() == Qt::Key::Key_Left)
            {
                qDebug() << "left";
            }
            if (event->key() == Qt::Key::Key_D || event->key() == Qt::Key::Key_Right)
            {
                qDebug() << "right";
            }
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication::setAttribute(Qt::ApplicationAttribute::AA_UseDesktopOpenGL);
        QApplication app(argc, argv);
        OpenGLWindow w;
        w.show();
        return app.exec();
    }
    
    • You can play in my demo in the browser: https://65faec4d823b731715d4ac45--vermillion-gnome-1f3770.netlify.app/ (Use WAD-keys or arrow-keys to control a character)
    • Or download EXE for Windows 10 64-bit: mario-2d-jump-with-rays-opengles2-qt6-cpp-win10x64-exe.zip
    • APK file for Android 7-14: mario-2d-jump-with-rays-opengles2-qt6-cpp-android-7-14-apk.zip

    I made a level using Free Texture Packer and Tiled Map Editor.

    Desktop:
    mario-2d-jump-with-rays-opengles2-qt6-cpp.gif

    Android:
    mario-2d-jump-with-rays-opengles2-qt6-cpp-android.gif

    Cross-ref: https://stackoverflow.com/questions/78194124/dealing-with-keyboard-layout-for-input-on-qt-webassembly

    1 Reply Last reply
    2
    • 8Observer88 8Observer8 referenced this topic on
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by 8Observer8
      #2

      All resources (sprites, music and sounds) have been replaced with free ones. You can see a list of free resources here. For example, I took the sprites here: https://webfussel.itch.io/more-bit-8-bit-mario

      • itch.io page
      • Click this link to play in the browser (use WAD or arrow keys to move and jump)
      • Download EXE for Windows 10, 64-bit
      • APK-file for Android

      mario-2d-jumps-webfussel-opengles2-qt6-cpp-android.gif

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved