2

I have my own "Virtual Keyboard". I already got to transform buttons clicked into KeyEvents and deliver it to QTextEdit and so on. My problem now is that I want to do the same for writable areas inside a QWebEngineView.

For example, I use my keyboard to edit my QLineEdit, and request a website. DONE

Let's say I requested google. Now I have the Google website right in front of me. I need to send KeyEvents from my Keyboard to it's search box.(Box that is inside my QWebEngineView.

Let's now point a few points:

  1. I am using PyQt5
  2. As I've read, the API says me that it's parent should consume the KeyEvent to the corect place. here
  3. This snippet says "...like it was possible with QtWebKit."
  4. I've seen now that there is no more QtWebKit, and so Chromium instead.(Maybe that's the reason I'm not getting to post these events)

This is what I have for example to simulate KeyEvents to my QEditText and whatever..

from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import QSize
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QPushButton


class KeyboardKey(QPushButton):

    __path = ""

    __size = [30, 30]
    __style = ""
    __icon_on = ""
    __icon_off = ""
    __auto_repeat = True
    __receiver = None
    __key = None
    __str_key = None

    def __init__(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
        super(KeyboardKey, self).__init__()
        self.__size = size
        self.__style = style
        self.__icon_on = str_icon_on
        self.__icon_off = str_icon_off
        self.__auto_repeat = auto_repeat
        self.__receiver = receiver
        self.__key = key
        self.__str_key = str_key
        self.set_up_button(style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key)

    def set_up_button(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
        self.__size = size
        self.__style = style
        self.__icon_on = str_icon_on
        self.__icon_off = str_icon_off
        self.__auto_repeat = auto_repeat
        self.__receiver = receiver
        self.__key = key
        self.__str_key = str_key
        self.setText(str_key)

        self.setFixedSize(size[0], size[1])
        self.setStyleSheet(style)
        self.setIconSize(QSize(size[0], size[1]))
        self.setIcon(QIcon(self.__path + str_icon_off + ".png"))
        self.setAutoRepeat(auto_repeat)
        pixmap = QPixmap(self.__path + str_icon_off + ".png")
        self.setMask(pixmap.mask())
        self.pressed.connect(self.key_pressed)
        self.released.connect(self.key_released)

    def set_receiver(self, receiver):
        self.__receiver = receiver

    def key_pressed(self):
        self.setStyleSheet("""
                            border-width: 5px;
                            border-color: rgb(37,43,52);
                            color: white;
                            background-color: rgb(0,187,255);
                        """,)

    def key_released(self):
        event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
                          "a", False)
        # self.__receiver is my QEditText/QLineEdit/...
        self.__receiver.keyPressEvent(event)

This Last part is the one that I post my events on the "self.__receiver". This receiver is set always for the "QWidget" that invoke it.

I have tried just to say something like:

def key_released(self):
    event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
                      "a", False)
    # web_view is my QWebEngineView... but it won't consume. Or maybe it's not consuming to the right place.
    self.web_view.keyPressEvent(event)
4

1 回答 1

6

当您将事件发送到 QWebEngineViews focusProxy 时,这应该可以工作 - 这样的事情应该可以工作:

recipient = self.web_view.focusProxy()
QApplication.postEvent(recipient, event)
于 2016-11-10T19:29:13.110 回答