我正在尝试开发一个由全局按键绑定(bind)触发的 Windows 覆盖弹出窗口,一旦按下按键绑定(bind),它应该将焦点捕获到 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()
这段记录显示了在显示时焦点停留在其他应用程序而不是返回到窗口的不良行为。
最佳答案
感谢ekhumoro我能够弄清楚如何强制聚焦在窗口上。下面是强制聚焦所需的代码:
导入和设置
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)
这是实现此功能的完整示例:
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()
关于python - 强制键盘焦点到 LineEdit QT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68764703/