How to send artificial QKeyEvent to QWebEngineView?

Context: I am creating a small web browser with a custom on-screen keyboard.

It worked almost perfectly with Qt WebKit ( QWeb* classes), but there were crashes due to errors in WebKit ... which will not be fixed after Qt 5.4.0, since they are switching to Qt WebEngine.

So, I decided to move the material to Qt WebEngine ( QWebEngine* classes), following the short guide on navigating webkit-> webengine. Following the QWebElement clause QWebElement , I worked out a way to show / hide the on-screen keyboard (which now requires the launch of async. JS code). But I scratch my head how to send fake key events to a web page.

I tried some things:

  • QCoreApplication::postEvent(m_webview, event) does nothing when it works with old QWeb things;
  • You can send keys by running JavaScript, but I find this too messy

Thanks,

+10
c ++ qt qtwebengine qwebview
source share
3 answers

I guess the only way to achieve this right now would be to use QAction to send the event to the WebView, using for example something like this:

 connect( this , SIGNAL( keyPressed( int ) ) , &m_webview , SLOT( handleKey( int ) ) ); 

I believe that functionality will be added in Qt 5.5.1, as you can see below:

https://codereview.qt-project.org/#/c/104901/

+1
source share

Despite the fact that the initial question is one year old, it is still relevant for those who, like me, decided to move (finally!) From QWebKit to QWebEngine (Qt 5.5 - 5.6b). Here is a dirty solution that requires an existing webenginepage-> view (). This is for mouse events, and it would not be a big surprise if it were not located 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 ); } 

Inspired by Using QWebEngine to Render an Image as well as How Can I Draw Events Using QtWebEngine? and googling.

+4
source share

This code is working fine

  for(auto* child : ui->webEngineView->children() ) { int key = Qt::Key_V; //or some other QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString()); QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier); qApp->sendEvent(child, &pressEvent); qApp->sendEvent(child, &releaseEvent); } 
0
source share

All Articles