使用pyqt5实现键盘(含组合键)鼠标事件响应
使用python3.6,pyqt5,在macOS上测试有效。
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):
print("按下:" + str(event.key()))
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')
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_())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)