Congratulations to our 2022 Qt Champions!

How do you send key events to QWebEngineView if sendkey() doesn't work?

  • How can I send a key event to QWebEngineView? With QWebView I just used sendkey(), but that doesn't work for webengine. I basically have a virtual keyboard as part of my kiosk application the users can use to enter information on a specific webpage.

  • Here is a dirty solution that requires existing webenginepage->view(). This is for mouse events, and it would not be a big surprise if it's not situated for keyboard events...

    void Whatever::sendMouseEvent( QObject* targetObj, QMouseEvent::Type type, const QPoint& pnt ) const
        QMouseEvent event( type, pnt, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
        QApplication::sendEvent( targetObj, &event );
    void Whatever::sendMouseClick( QObject* targetObj, const QPoint& pnt ) const
        sendMouseEvent( targetObj, QMouseEvent::MouseMove, pnt );
        sendMouseEvent( targetObj, QMouseEvent::MouseButtonPress, pnt );
        sendMouseEvent( targetObj, QMouseEvent::MouseButtonRelease, pnt );
    void Whatever::emulateMouseClick( const QPoint& pnt ) const
        //-- right now (Qt 5.5 & 5.6) there is only one child - 
        //-- QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget
        //-- but it could change in future
        Q_FOREACH( QObject* obj, mWebEnPage->view()->children() ) //-- ACHTUNG! Check mWebEnPage->view() in real code!
            if( qobject_cast<QWidget*>( obj ) )
                sendMouseClick( obj, pnt );

    This is the solution I'm using for both Mouse and Keyboard events.
    BTW: In Qt 5.7.0 there are 3 children.

  • @ThatDud3 Could you post a full working example of this?

  • This is the full working example. I'm currently using it in my projects.

    If you want to change it for sending keys just replace sendMouseClick( obj, pnt ); in Q_FOREACH() with sendKey(obj, key); where you implement key press and release (instead of MouseMove, MouseButtonPress, MouseButtonRelease)

    NB! Original post said that there was one single object, while recently I noticed at least 3 different objects in mWebEnPage->view()->children() and that could change in the fututre so instead of discovering which one is needed and hard coding it I prefer to use the loop and just send events to all children.


Log in to reply