title: How to detect key press signal inside if statement and place inside it's own thread PyQt5
body: <p>I'm having trouble figuring out how to detect key press signal inside of an if statement that's in another class and thread and thus try to also place the key press signal emit/receive inside it's own thread PyQt5.</p>
  <p>The window class handles the gui stuff, but then the modbusworker class has is its thread to handle the exiting. Basically when I click run the script starts collecting and saving data from an Arduino. However, I would like a keyboard hotkey (return key) that can trigger additional print statements for labeling. Integrating this has been my issue.</p>
  <p>Thanks!</p>
  <pre><code>class Window(QMainWindow, Ui_MainWindow):
  
  def __init__(self, parent=None):
      super().__init__(parent)
      self.setupUi(self)
  
      self.m_thread = QtCore.QThread(self)
      self.m_thread.start()
  
      self.m_modbus_worker = ModbusWorker()
     # self.m_modbus_worker.connect()
      self.m_modbus_worker.moveToThread(self.m_thread)
  
      btn = self.pushButton_11
      btn.clicked.connect(Window.restart)
      btn_ex = self.pushButton_12  # exit button
      btn_ex.clicked.connect(lambda: self.stop())
  
      #btn_ex.clicked.connect(lambda: self.stopIt())
      self.show()
  
      # self.UIcomp()
      self.pushButton.clicked.connect(lambda: self.b1())
      self.pushButton_2.clicked.connect(
          lambda: self.b2())
      self.pushButton_3.clicked.connect(
          lambda: self.b3())
      self.pushButton_4.clicked.connect(
          lambda: self.b4())
      self.pushButton_5.clicked.connect(
          lambda: self.b5())
      self.pushButton_6.clicked.connect(
          lambda: self.b6())
      self.pushButton_7.clicked.connect(
          lambda: self.b7())
      self.pushButton_8.clicked.connect(
          lambda: self.b8())
      self.pushButton_9.clicked.connect(
          lambda: self.b9())
  
      self.pushButton_10.clicked.connect(
          lambda: self.start())
  
      keyPressed = QtCore.pyqtSignal(int)
  
  def keyPressEvent(self, event):
      super(Window, self).keyPressEvent(event)
      self.keyPressed.emit(event.key())
      #somehow this needs to be part of its known thread
  
  @QtCore.pyqtSlot()
  def start(self):
      QtCore.QTimer.singleShot(0, self.m_modbus_worker.do_work)
  
  @QtCore.pyqtSlot()
  def stop(self):
      self.m_thread.requestInterruption()
  
  
  
  class ModbusWorker(QtCore.QObject):   
  
      @QtCore.pyqtSlot()
      def do_work(self):
  
        
          now = datetime.datetime.now()
          arduino_port = &quot;/dev/tty.usbmodem2101&quot;  # serial port of Arduino
       #   baud = 115200
          baud = 9600
          # name of the CSV file generated
          fileName = b1file + now.strftime(&quot;%m %d %Y, %H:%M:%S &quot;) + &quot;.csv&quot;
          ser = serial.Serial(arduino_port, baud)
          print(&quot;Connected to Arduino port:&quot; + arduino_port)
  
        
  
          print(&quot;Created file&quot;)
  
          file = open(&quot;/Users/Documents/&quot; + fileName, &quot;w+&quot;)
          file.write(now.strftime(&quot;%m/%d/%Y %H:%M:%S &quot;))
          file.write(b1title)
  
          while not QtCore.QThread.currentThread().isInterruptionRequested():
  
  
              #Not sure what to do here to check the signal
              #if (signal detected):
              #     write...
  
              getData = str(ser.readline())
              data = getData[2:][:-7]
            
  
              # add the data to the file
              # append the data to the file
             # file.write(now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;))
              print(now.strftime(&quot;%H:%M:%S&quot;))
              file.write(now.strftime(&quot;%H:%M:%S&quot;))
              print(data)
              file.write(data + &quot;\n&quot;)  # write data with a newline
  
      
          QtCore.QThread.currentThread().quit()
    
  
  
  if __name__ == &quot;__main__&quot;:
  
      app = QApplication(sys.argv)
      win = Window()
      win.show()
  
      Window.restart()
      sys.exit(app.exec())
  </code></pre>