win32con win32gui PyQt5 focus problems

276 views Asked by At

So I found this example and its really cool and I want to do something similar in a app. The problem I have found though is that yes you can embed the window and yes you can click on things but you are not able to type in the window. so for example if I open a chrome or edge window and choose it to be embedded I am not able to type in the embedded browser.

from PyQt5.QtGui import QWindow
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QListWidget, QLabel
import win32con
import win32gui


class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.resize(800, 600)
        layout = QVBoxLayout(self)

        self.myhwnd = int(self.winId())

        layout.addWidget(QPushButton('get windows', self,clicked=self._getWindowList, maximumHeight=30))
        layout.addWidget(QLabel('load', self, maximumHeight=30))
        self.windowList = QListWidget(self, itemDoubleClicked=self.onItemDoubleClicked, maximumHeight=200)
        layout.addWidget(self.windowList)

    def closeEvent(self, event):
        if self.layout().count() == 4:
            self.restore()
        super(Window, self).closeEvent(event)

    def _getWindowList(self):
        self.windowList.clear()
        win32gui.EnumWindows(self._enumWindows, None)

    def onItemDoubleClicked(self, item):
        self.windowList.takeItem(self.windowList.indexFromItem(item).row())
        hwnd, phwnd, _, _ = item.text().split('|')

        if self.layout().count() == 4:
            self.restore()
        hwnd, phwnd = int(hwnd), int(phwnd)
        style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
        exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
        print('save', hwnd, style, exstyle)

        widget = QWidget.createWindowContainer(QWindow.fromWinId(hwnd))
        widget.hwnd = hwnd
        widget.phwnd = phwnd
        widget.style = style
        widget.exstyle = exstyle
        self.layout().addWidget(widget)

    def restore(self):
        widget = self.layout().itemAt(3).widget()
        print('restore', widget.hwnd, widget.style, widget.exstyle)
        win32gui.SetParent(widget.hwnd, widget.phwnd)
        win32gui.SetWindowLong( widget.hwnd, win32con.GWL_STYLE, widget.style | win32con.WS_VISIBLE)
        win32gui.SetWindowLong( widget.hwnd, win32con.GWL_EXSTYLE, widget.exstyle)
        win32gui.ShowWindow( widget.hwnd, win32con.SW_SHOW)
        widget.close()
        self.layout().removeWidget(widget)
        widget.deleteLater()

    def _enumWindows(self, hwnd, _):
        if hwnd == self.myhwnd:
            return
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
            phwnd = win32gui.GetParent(hwnd)
            title = win32gui.GetWindowText(hwnd)
            name = win32gui.GetClassName(hwnd)
            self.windowList.addItem('{0}|{1}|\t{2}\t|\t{3}'.format(hwnd, phwnd, title, name))


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

Now my question is how do I change the focuses to the embedded window for the keyboard? As it is the keyboard is still focused in the parent window while the mouse is in the window.

0

There are 0 answers