When I try to use key listener with pyqt5 to detect if some key is clicked. My app stops working...
the listener works well but when I use pyqt with the listener the app crashes
I want to detect if this button is pressed ']'
my code:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.uic import loadUiType
import sys
from pynput import keyboard
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setGeometry(500, 500, 500, 300)
self.btn = QPushButton('start', self)
self.btn.move(100, 100)
self.btn.clicked.connect(lambda : self.start())
def on_press(self, key):
try:
k = key.char
except:
k = key.name
if k == ']':
print('dad')
def start(self):
self.listener = keyboard.Listener(on_press=self.on_press)
self.listener.start()
self.listener.join()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
ex.show()
sys.exit(app.exec_())