- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个简单的 GUI,它将在特定点打开一个 opencv 窗口。这个窗口有一些非常基本的 keyEvents 来控制它。我想通过一些功能来推进这一点。因为我的 QtGui 是我的 Controller ,所以我认为使用 KeyPressedEvent 是一个好方法。我的问题是,如果我在 opencv 窗口中处于事件状态,我将无法触发 KeyEvent。
那么,如果我的 Gui 失去焦点,我该如何触发 KeyEvent?
我真的需要使用 GrabKeyboard 吗?
以下代码重现了我的问题:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.Qt import Qt
import cv2
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.first = True
def openselect(self):
im = cv2.imread(str('.\\images\\Steine\\0a5c8e512e.jpg'))
self.r = cv2.selectROI("Image", im)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space and self.first:
self.openselect()
self.first = False
print('Key Pressed!')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
最佳答案
keyPressEvent 方法仅在小部件具有焦点时调用,因此如果焦点具有另一个应用程序则不会通知它,因此如果你想检测键盘事件则必须处理操作系统库,但在 python 中它们已经存在将这些更改报告为 pyinput
的库(python -m pip install pyinput
):
import sys
from PyQt5 import QtCore, QtWidgets
from pynput.keyboard import Key, Listener, KeyCode
class KeyMonitor(QtCore.QObject):
keyPressed = QtCore.pyqtSignal(KeyCode)
def __init__(self, parent=None):
super().__init__(parent)
self.listener = Listener(on_release=self.on_release)
def on_release(self, key):
self.keyPressed.emit(key)
def stop_monitoring(self):
self.listener.stop()
def start_monitoring(self):
self.listener.start()
class MainWindow(QtWidgets.QWidget):
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
monitor = KeyMonitor()
monitor.keyPressed.connect(print)
monitor.start_monitoring()
window = MainWindow()
window.show()
sys.exit(app.exec_())
关于python - 没有焦点的 KeyPressEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61859385/
我正在编写一个简单的 GUI,它将在特定点打开一个 opencv 窗口。这个窗口有一些非常基本的 keyEvents 来控制它。我想通过一些功能来推进这一点。因为我的 QtGui 是我的 Contro
我无法理解 keyPressEvent 方法在该程序中的工作原理。具体来说,这里的“e”是什么? keyPressEvent 是使用预先存在的实例“e”重新定义的方法吗? import sys fro
我对 KeyPressEvent 有疑问, 我需要移动一个 QGraphicsItem播放器在我的场景中,但程序没有使用函数KeyPressEvent类game 文件“game.h”: #ifndef
我在QT上写作。 OpenGL 已连接,工作正常,但事件和 keyPressEvent mousePressEvent 不响应击键。 class MainWindow : public QGLWidg
我正在尝试在 Qt 上创建一个玩家可以沿对角线前进的游戏。所以当我按下 W 和 D 时,玩家可以转到右上角。但是,当我先按 W,然后按 D,然后松开 D 时,播放器不会朝 W 方向移动。事实上,按住
` from PyQt5.QtCore import Qt import sys from PyQt5.QtWidgets import QApplication, QMainWindow from
我想声明一个 com.google.gwt.event.dom.client.KeyPressEvent。它应该匹配任何字符或数字,以及 ,.-+~ 等符号。特别是我想排除所有“导航”键,如箭头、插入
我需要生成键盘输入,因为我有一个事件附加到此输入。我试着打电话: keyPressEvent(Qt::Key_Delete); 没有成功,有没有办法生成这个 keyPressEvent ? 最佳答案
我正在创建一个绘制颜色的程序,然后另一个类检查它们并在它们匹配时返回 true。整个代码都在一个对话框中。 colQ 是一个简单存储 RGB 值和其他东西的类;我们将使用这些来获取 RGB。 colD
我写了这段代码,但是我得到了这个错误: "left of '->key' must point to class/struct/union/generic type". 考虑到 GUI 是从 QWid
我有一个程序可以捕获单个按键,然后输出被按下的键。问题是,我无法返回按下的键的值,也无法让箭头键输出任何内容。这是我的代码: myApp.h class someClass: public QDial
我有一个主窗口,我在其中建立了标准的“编辑”>“剪切/复制/粘贴”操作,菜单操作链接到 QTextEdit 的相关插槽,它是主窗口的子窗口。 主窗口有一个不同的子窗口(在拆分器的另一侧),它显示一个
我从 PyQt GPL v5.4 创建一个 UI 并使用 pyuic5 将 *.ui 文件转换为 *.py 但我不知道 keyPressEvent 在这段代码中是如何工作的!! 它应该适用于 QWid
我有一个主要的 Widget 并且在这个主要的小部件里面我有 QListWidget 和两个按钮。我已经覆盖了主小部件内的 keyPressEvent(继承自 QWidget)。当焦点不在 QList
我有一个简单的表单,其中包含一些组合、标签、按钮和一个 QTextEdit。 我尝试使用 keyPressEvent 捕捉输入或返回键,但由于某些原因我无法捕捉。然而,我也使用的 ESC 键被识别。
当我按下按键时,此代码有效,但不适用于 Print Screen 键。 void KeyHooker::keyPressEvent(QKeyEvent *event) { qDebug() k
我想通过发送 KeyEvent 将字符附加到 QLineEdit。我正在使用这样的代码: ui.myEdit->setFocus(); for(size_t i = 0; i myEdit->setT
某些小部件将允许我这样做: self.widget.clicked.connect(on_click) 但是在做: self.widget.keyPressEvent.connect(on_key)
我正在尝试在 QT 中重写 QWidgets keyPressEvent 函数,但它只是行不通。我不得不说我是 CPP 的新手,但我知道 ObjC 和标准 C。 我的问题是这样的: class QSG
在我们的应用程序中,有一个用户控件,它允许输入一个数字(在 numericupdown 内),然后是一个 组合框,它允许选择 SI 前缀( [p, n, µ, m, -, k, M, G, T] 从小
我是一名优秀的程序员,十分优秀!