PyQt Web 浏览器:无需单击鼠标即可专注于网页
我正在为没有鼠标输入的嵌入式设备编写一个应用程序。 Web 浏览器非常非常基本,没有任何按钮、地址栏、文件栏等。目前,它只是加载一个网页。该网页使用 JavaScript 来捕获按键事件以进行操作。
我遇到的问题是,当浏览器加载时,按键未被捕获。我已经将这个问题归结为我认为的焦点问题。加载浏览器时,只有在应用程序上单击鼠标才能获得焦点。由于我没有鼠标,因此无法发生初始单击。
如何确保浏览器应用程序正确聚焦,以便当我从终端或脚本启动它时,它将立即获得焦点并且关键事件可以相应发生?
我的代码如下:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
假设我正确使用它,QWidget.setFocus() 不起作用。任何帮助表示赞赏。谢谢
I am writing an application for an embedded device that has no mouse input. The web browser is very very basic and does not have any buttons, address bar, file bar , etc. For now, it simply just loads a web page. This web page uses javascript to catch key press events for actions.
The problem I am having is that when the browser loads, the key presses are not caught. I have tracked this problem down to what I believe is a focus problem. When the browser loads, it does not have focus until a mouse click occurs on the application. Since I do not have a mouse, that initial click cannot happen.
How can I make sure that the browser application gets focused correctly such that when I launch it from a terminal or script it will gain immediate focus and the key events can happen accordingly?
My code is as follows:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
The QWidget.setFocus() did not work, assuming I used it correctly. Any help is appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我的笔记本电脑上尝试了你的代码,QWebView 已经获得焦点 - 一旦 Google 加载,我就可以输入并且我的文本出现在文本框中。
如果是焦点问题,那么由于 QWebView 继承了 QWidget,您可以调用其
I tried your code on my laptop and the QWebView had focus already - once Google had loaded I could type and my text appeared in the text box.
If it is a focus problem then as QWebView inherits QWidget you can call its setFocus() method. Here is your code with an extra line calling the QWebView's setFocus method once a page has loaded:
您可以将焦点设置在主框架上:
You can set focus on the main frame: