PyQt5 keyPressEvent 阻止通过 QWebView 页面读取键

PyQt5 keyPressEvent blocks reading keys by QWebView page

我想将 Escape 键设置为退出应用程序的键

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            self.close()

但是当我这样做时,QWebView 会丢失键盘事件。

class PlayFlash(QWebView):
    def __init__(self):
        # QWebView
        self.view = QWebView.__init__(self)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.resize(1024, 768)
        self.move(0, 0)
        # enable flashplayer plugin
        self._settings = QWebSettings.globalSettings()
        self._settings.setAttribute(QWebSettings.PluginsEnabled, True)
        self.setFocusPolicy(Qt.StrongFocus)
        self.load("file:///home/kamil/gitlab/PlayFlash/PlayFlash.html")

如果我根本不使用 keyPressEvent,键盘事件将由 QWebView 页面读取。

解决方案:

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            self.restoreMainMenu()
        else:
            super().keyPressEvent(event)