Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. How do you send key events to QWebEngineView if sendkey() doesn't work?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved QtWebEngine
4 Posts 3 Posters 5.6k 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.
  • mgood22M Offline
    mgood22M Offline
    mgood22
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    1
    • T Offline
      T Offline
      ThatDud3
      wrote on last edited by ThatDud3
      #2

      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.
      HTH

      C 1 Reply Last reply
      0
      • T ThatDud3

        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.
        HTH

        C Offline
        C Offline
        cassie
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThatDud3
          wrote on last edited by ThatDud3
          #4

          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.

          HTH

          1 Reply Last reply
          0

          • Login

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