I am making an application that will change a mdiArea's colour dependant on whether the "O" or "P" key is pressed. This is so when using a mod in assetto corsa I can track whether my DRS is on as there isnt a ui element in the mod I am using. The program tracks the inputs as I'd like it to when I have clicked in the window (ie it is focussed) however I need it to run even when I am "focussed" on the game when playing with borderless windows.
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__() # Call the inherited classes __init__ method
uic.loadUi('drs.ui', self) # Load the .ui file
self.show() # Show the GUI
self.drs = False
def keyPressEvent(self, e):
if e.key() == Qt.Key_O:
if self.drs == True:
self.drs = False
self.label_2.setText("OFF")
self.mdiArea.setBackground(QtGui.QBrush(QtGui.QColor("red")))
else:
self.drs = True
self.label_2.setText("ON")
self.mdiArea.setBackground(QtGui.QBrush(QtGui.QColor("green")))
if e.key()==Qt.Key_P:
self.drs = False
self.label_2.setText("OFF")
self.mdiArea.setBackground(QtGui.QBrush(QtGui.QColor("red")))
I have tried using the keyboard module but I can't seem to integrate those events with pyqt without using a while True: loop which obviously stop pyqt from reaching its event loop which stops widgets from updating(I did try using signals and another thread but could not get this to work either). I have also tried applications that "pin" the window on top and this doesnt work either as despite not being hidden they are not technically in focus
Basically how can I make the application detect keyboard events regardless of window focus