关于PYQT双击enter键响应的实现几种方法
方法1.
PYQT5
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt, QDateTime
class KeyPressWidget(QWidget):
#QT初始化
def __init__(self):
super().__init__()
self.last_enter_time = None
def keyPressEvent(self, event):
# 监听键盘事件
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
current_time = QDateTime.currentDateTime()
if self.last_enter_time is not None:
time_diff = current_time.secsTo(self.last_enter_time)
if abs(time_diff) < 1:
print("Current local time:", current_time.toString(Qt.ISODateWithMs))
self.last_enter_time = current_time
if __name__ == "__main__":
#执行
app = QApplication(sys.argv)
window = KeyPressWidget()
window.setGeometry(100, 100, 300, 200)
window.setWindowTitle("Enter Key Press Event")
window.show()
sys.exit(app.exec_())
这里的1秒可以自己定义,反复测试0.5秒可能更合适。