强制键盘焦点到 LineEdit QT

Force keyboard focus to LineEdit QT

我正在尝试为 Windows 开发一个由全局键绑定触发的叠加弹出窗口,一旦按下键绑定,它应该将焦点捕获到 QLineEdit 中。问题是,如果它被聚焦一次,但我点击外面有效地移除了焦点,之后就无法重新获得焦点。

这是我试图用来强制键盘聚焦于 QLineEdit 的代码的简化版本:

from PySide6 import QtCore, QtWidgets, QtGui
from pynput import keyboard

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.input = QtWidgets.QLineEdit()
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.input)

        self.input.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setWindowState(QtCore.Qt.WindowActive)
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
    
    @QtCore.Slot()
    def toggle_visible(self):
        if self.isVisible():
            print("Hiding popup")
            self.hide()
        else:
            print("Showing popup")
            self.show()
            self.activateWindow()
            self.input.grabKeyboard()
            self.input.setFocus()

class KeybindPressed(QtCore.QObject):
    keybind_pressed = QtCore.Signal()

    def __call__(self):
        self.keybind_pressed.emit()


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    pressed = KeybindPressed()
    with keyboard.GlobalHotKeys({"<alt>+<space>": pressed}):

        widget = MyWidget()
        pressed.keybind_pressed.connect(widget.toggle_visible)
        widget.resize(800, 600)
        widget.show()

        app.exec()

这是一段记录,显示焦点停留在其他应用中而不是在显示时返回到 window 的不良行为。

多亏了 ekhumoro,我才能够弄清楚如何将注意力集中在 windows 上。以下是强制聚焦所需的代码:

导入和设置

windows = False
if os.name == "nt":
    import win32gui, win32con, win32process, win32api
    win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
    windows = True

实际强制聚焦的代码,这将在您的 window 应该获得焦点时调用:

def force_focus(qt_widget_instance: QtWidgets.QWidget):
    if windows:
        fgwin = win32gui.GetForegroundWindow()
        fg = win32process.GetWindowThreadProcessId(fgwin)[0]
        current = win32api.GetCurrentThreadId()
        if current != fg:
            win32process.AttachThreadInput(fg, current, True)
            win32gui.SetForegroundWindow(qt_widget_instance.winId())
            win32process.AttachThreadInput(fg, win32api.GetCurrentThreadId(), False)

这里是完整的示例:

from PySide6 import QtCore, QtWidgets, QtGui
from pynput import keyboard

import os

windows = False
if os.name == "nt":
    import win32gui, win32con, win32process, win32api
    win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE)
    windows = True

def force_focus(qt_widget_instance: QtWidgets.QWidget):
    if windows:
        fgwin = win32gui.GetForegroundWindow()
        fg = win32process.GetWindowThreadProcessId(fgwin)[0]
        current = win32api.GetCurrentThreadId()
        if current != fg:
            win32process.AttachThreadInput(fg, current, True)
            win32gui.SetForegroundWindow(qt_widget_instance.winId())
            win32process.AttachThreadInput(fg, win32api.GetCurrentThreadId(), False)

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.input = QtWidgets.QLineEdit()
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.input)

        self.input.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setWindowState(QtCore.Qt.WindowActive)
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
    
    @QtCore.Slot()
    def toggle_visible(self):
        if self.isVisible():
            print("Hiding popup")
            self.hide()
        else:
            print("Showing popup")
            self.show()
            force_focus(self)
            self.activateWindow()
            self.input.grabKeyboard()
            self.input.setFocus()



class KeybindPressed(QtCore.QObject):
    keybind_pressed = QtCore.Signal()

    def __call__(self):
        self.keybind_pressed.emit()


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    pressed = KeybindPressed()
    with keyboard.GlobalHotKeys({"<cmd>+<space>": pressed}):
    
        widget = MyWidget()
        pressed.keybind_pressed.connect(widget.toggle_visible)
        widget.resize(800, 600)
        widget.show()

        app.exec()