'How to make clicking a keyboard button have a subsequent action? PyQt6/PySide6
I'm trying to implement into my code the ability for the program to recognize if the user presses buttons on the keyboard, but I haven't been able to figure it out. Right now I'm trying to make the program write "Hi" to the terminal if it recognizes that the 0 button has been pressed. I've looked at a ton of stuff on google but none of it has worked so far.
from PySide6 import QtCore, QtWidgets, QtGui
import sys
WIDTH, HEIGHT = 1200, 700
class Ui_Window(object):
def setupUi(self, Window):
Window.setObjectName("Window")
Window.setFixedSize(WIDTH, HEIGHT)
Window.setWindowTitle("App") # app title
self.key = QtCore.Signal()
self.key.connect(Window, self.on_key)
def keyPressEvent(self, event):
super(Ui_Window, self).__init__()
self.key.emit(event)
def on_key(self, event):
if event.key() == QtCore.Qt.Key_0:
print("Hi")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Window = QtWidgets.QWidget()
ui = Ui_Window()
ui.setupUi(Window)
Window.show()
sys.exit(app.exec())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
