gpt4 book ai didi

python - 确定表达式中是否按下了键 (Python) (PyQT)

转载 作者:行者123 更新时间:2023-12-02 05:44:57 25 4
gpt4 key购买 nike

在 mouseMoveEvent 方法中,我看到如下代码来检查是否按下了鼠标左键或右键(如下所示)。有没有办法检查当前是否按下了某些键盘按键?理想情况下,我希望在单击鼠标左键并移动鼠标并按下某个键时执行该操作。我没有看到使用 keyPressEvent 的方法,因为它是与 mouseMoveEvent 分开调用的(就像本示例中未使用 mousePressEvent 一样)。

def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
#run this when mouse is moved with left button clicked
elif event.buttons() & QtCore.Qt.RightButton:
#run this when mouse is moved with right button clicked

编辑:基于ekhumoro的评论方法现在看起来像这样。它仅适用于按键修饰符:

def mouseMoveEvent(self, event):
modifiers = QtGui.QApplication.keyboardModifiers()
if bool(event.buttons() & QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
#run this when mouse is moved with left button and ctrl clicked
elif event.buttons() & QtCore.Qt.LeftButton:
#run this when mouse is moved with left button clicked
elif event.buttons() & QtCore.Qt.RightButton:
#run this when mouse is moved with Right button clicked

如果有人能够使其与任何键一起使用,我们将不胜感激

最佳答案

def mouseMoveEvent(self, event):
modifiers = QApplication.keyboardModifiers()
Mmodo = QApplication.mouseButtons()
if bool(Mmodo == QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
print 'yup'

关于python - 确定表达式中是否按下了键 (Python) (PyQT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com