【问题标题】:Determining if key is pressed in expression (Python) (PyQT)确定是否在表达式中按下键(Python)(PyQT)
【发布时间】:2018-01-26 13:21:49
【问题描述】:

在 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

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

【问题讨论】:

  • 请参阅this answer 以获得解决方案(但仅适用于修饰键)。
  • 如果你想使用其他键,你必须覆盖keyPressEventkeyReleaseEvent,然后在按下/释放键时设置/取消设置一个标志。

标签: python python-3.x user-interface pyqt pyqt4


【解决方案1】:
def mouseMoveEvent(self, event):
    modifiers = QApplication.keyboardModifiers()
    Mmodo = QApplication.mouseButtons()
    if bool(Mmodo == QtCore.Qt.LeftButton) and (bool(modifiers == QtCore.Qt.ControlModifier)):
        print 'yup'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多