如何向 QWebEngineView 发送人工 QKeyEvent
How to send artificial QKeyEvent to QWebEngineView?
本文关键字:QKeyEvent QWebEngineView 更新时间:2023-10-16
>上下文:我正在创建一个带有自定义屏幕键盘的小型网络浏览器。
它在Qt WebKit(QWeb*
类)上几乎可以正常工作,但是由于WebKit中的错误而导致崩溃...在Qt 5.4.0之后不会修复,因为它们正在迁移到Qt WebEngine。
所以我决定按照简短的webkit->webengine过渡指南将东西移到Qt WebEngine(QWebEngine*
类)。按照QWebElement
上的警告部分,我已经解决了显示/隐藏屏幕键盘的方法(现在需要运行异步。JS代码)。但是我对如何将人工密钥事件发送到网页感到挠头。
我已经尝试了一些东西:
-
QCoreApplication::postEvent(m_webview, event)
在处理旧QWeb
的东西时什么都不做;
可以通过 - 运行JavaScript来发送密钥,但我发现这太脏了
谢谢
尽管最初的问题已经存在了一年,但对于那些像我一样决定(最终!)从QWebKit迁移到QWebEngine(Qt 5.5 - 5.6b)的人来说,这仍然是现实的。这是一个需要现有webenginepage->view()的脏解决方案。这是针对鼠标事件的,如果它不用于键盘事件,那也就不足为奇了:
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 );
}
灵感来源使用 QWebEngine 渲染图像和如何使用QtWebEngine获取绘制事件?和谷歌搜索。
此代码工作正常
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);
}
我想
现在实现这一目标的唯一可能性是利用QAction
将事件发送到 WebView,例如使用如下内容:
connect( this , SIGNAL( keyPressed( int ) ) , &m_webview , SLOT( handleKey( int ) ) );
我想该功能将在Qt 5.5.1中添加,如下所示:
https://codereview.qt-project.org/#/c/104901/
相关文章:
- 如何从qml Keys.oPressed调用qt keyPressEvent(QKeyEvent*事件)
- 如何调用 KeyPressEvent(QKeyEvent *) of linedit of QCombobox.
- 如何接受/忽略QKeyEvent
- 如何将QKeyEvent::nativeModifiers()转换为UINT fsModifiers
- 如何向 QWebEngineView 发送人工 QKeyEvent
- QKeyEvent 键按事件未检测到箭头键
- 无法在 QopenGLWidget 中捕获 QkeyEvent
- QKeyEvent在我的应用程序不工作
- QKeyEvent isAutoRepeat not working?