比如要监听Ctrl + F
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.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_F:
print("按下了ctr和F键")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
监听ctrl + shift + f 键
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QWidget
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.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_F:
print("按下了ctr和F键")
if event.modifiers() == Qt.ControlModifier | Qt.ShiftModifier and event.key() == Qt.Key_F:
print("按下了ctr,shift和F键")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())