PyQt5 combinación de teclas

PyQt5 Ctrl + combinación de teclas

def keyPressEvent(self,event):

    print("按下:" + str(event.key()))

    if event.key() == Qt.Key_S:

        if QApplication.keyboardModifiers() == Qt.ControlModifier:

            self.actionFile.save(self.action_text.toPlainText())

            self.status.showMessage("保存成功 %s" % self.file)

Pyqt5 consigue utilizando un teclado (incluyendo una combinación de teclas) en respuesta a eventos de ratón

Uso python3.6, pyqt5

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import QtCore
from PyQt5.QtCore import *

# 声明窗口
class Window(QWidget):
    # 初始化
    def __init__(self):
        super().__init__()
        self.initUI()
    # 设置窗口的参数
    def initUI(self):
        self.setGeometry(300, 300, 300, 200)
        self.setFixedWidth(300)
        self.setFixedHeight(200)
        self.setWindowTitle('按键检测')
        self.show()

    # 检测键盘回车按键,函数名字不要改,这是重写键盘事件
    def keyPressEvent(self, event):
        #这里event.key()显示的是按键的编码
        print("按下:" + str(event.key()))
        # 举例,这里Qt.Key_A注意虽然字母大写,但按键事件对大小写不敏感
        if (event.key() == Qt.Key_Escape):
            print('测试:ESC')
        if (event.key() == Qt.Key_A):
            print('测试:A')
        if (event.key() == Qt.Key_1):
            print('测试:1')
        if (event.key() == Qt.Key_Enter):
            print('测试:Enter')
        if (event.key() == Qt.Key_Space):
            print('测试:Space')
        # 当需要组合键时,要很多种方式,这里举例为“shift+单个按键”,也可以采用shortcut、或者pressSequence的方法。
        if (event.key() == Qt.Key_P):
            if QApplication.keyboardModifiers() == Qt.ShiftModifier:
                print("shift + p")
            else :
                print("p")

        if (event.key() == Qt.Key_O) and QApplication.keyboardModifiers() == Qt.ShiftModifier:
            print("shift + o")

    # 响应鼠标事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            print("鼠标左键点击")
        elif event.button() == Qt.RightButton:
            print("鼠标右键点击")
        elif event.button() == Qt.MidButton:
            print("鼠标中键点击")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

 keyPressEvent (QKeyEvent) llamada al pulsar el teclado

Se llama cuando liberación teclado keyReleaseEvent (QKeyEvent)

 

from PyQt5.QtWidgets import QApplication, QWidget,QLabel,QPushButton
import sys
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPixmap


class Label(QLabel):
    def keyPressEvent(self, QKeyEvent):  # 键盘某个键被按下时调用
        #参数1  控件
        if QKeyEvent.key()== Qt.Key_A:  #判断是否按下了A键
            #key()  是普通键
            print('按下了A键')

        if QKeyEvent.modifiers()==Qt.ControlModifier and QKeyEvent.key()== Qt.Key_A:#两键组合
            #modifiers()   判断修饰键
            #Qt.NoModifier   没有修饰键
            #Qt.ShiftModifier    Shift键被按下
            #Qt.ControlModifier    Ctrl键被按下
            #Qt.AltModifier      Alt键被按下
            print('按下了Ctrl-A键')

        if QKeyEvent.modifiers() == Qt.ControlModifier|Qt.ShiftModifier and QKeyEvent.key() == Qt.Key_A:  # 三键组合
            print('按下了Ctrl+Shift+A键')


class win(QWidget): #创建一个类,为了集成控件
    def __init__(self):
        super().__init__()
        self.resize(600,600)
        self.setWindowTitle('事件学习')
        self.num=0
        self.label=Label('标签',self)
        self.label.move(100,100)
        self.label.grabKeyboard()   #控件开始捕获键盘
        # 只有控件开始捕获键盘,控件的键盘事件才能收到消息



if __name__=='__main__':
    app=QApplication(sys.argv)  #创建应用
    w=win()
    w.show()
    sys.exit(app.exec_())

 self.label.grabKeyboard controles del teclado # inicio de captura ()

# Sólo iniciar la captura de teclado de control, evento de teclado del control con el fin de recibir el mensaje. 

self.label.releaseKeyboard () # Detener Capturar el teclado 

Publicados 136 artículos originales · ganado elogios 71 · vistas 160 000 +

Supongo que te gusta

Origin blog.csdn.net/u012308586/article/details/104982137
Recomendado
Clasificación