Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This my project app for key cast in tutorial and gui.py is widget front app code and main.py is backend app code

sources code

gui.py

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QSizePolicy, QWidget)

class Ui_keycast(object):
    def setupUi(self, keycast):
        if not keycast.objectName():
            keycast.setObjectName(u"keycast")
        keycast.resize(287, 75)
        self.label_2 = QLabel(keycast)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setGeometry(QRect(9, 30, 269, 16))
        self.label_2.setAlignment(Qt.AlignCenter)
        self.label_2.setStyleSheet(f"""
                                    /*color*/
                                    color: #464d55;
                                    /*font_weight*/
                                    font-weight: bold;
                                    /*font_size*/
                                    font-size: 18px;
                                    """)

        self.retranslateUi(keycast)

        QMetaObject.connectSlotsByName(keycast)
    # setupUi
    def retranslateUi(self, keycast):
        keycast.setWindowTitle(QCoreApplication.translate("keycast", u"keycast", None))
        self.label_2.setText(QCoreApplication.translate("keycast", u"TextLabel", None))
    # retranslateUi


main.py

Python
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform,QKeyEvent)
from PySide6.QtWidgets import (QApplication, QLabel, QSizePolicy, QWidget,QDialog)
import time
import sys
import threading
# import main_gui
import keypressed_gui
class App(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        # self.ui = main_gui.Ui_JumpingButtons()
        
        self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
        # self.setw
        self.ui = keypressed_gui.Ui_keycast()
        self.ui.setupUi(self)

    def keyPressEvent(self, event) :
        # print(event.keyCombination().keyboardModifiers())
        # print(event.keyCombination().key().name)
        if event.keyCombination().keyboardModifiers() == Qt.KeyboardModifier.ShiftModifier and event.keyCombination().key() == Qt.Key.Key_S:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", f"Shift + S", None))
            self.ui.label_2.update()
        elif event.keyCombination().keyboardModifiers() == Qt.KeyboardModifier.ShiftModifier:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", u"Shift", None))
            self.ui.label_2.update()
        elif event.keyCombination().keyboardModifiers() == Qt.KeyboardModifier.ControlModifier:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", u"Ctrl", None))
            self.ui.label_2.update()
        elif event.keyCombination().keyboardModifiers() == Qt.KeyboardModifier.AltModifier:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", u"Alt", None))
            self.ui.label_2.update()
        elif event.keyCombination().key() == Qt.Key.Key_Return:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", u"Enter", None))
            self.ui.label_2.update()
        elif event.keyCombination().key() == Qt.Key.Key_AltGr :
            self.ui.label_2.setText(QCoreApplication.translate("keycast", u"Alt", None))
            self.ui.label_2.update()
        else:
            self.ui.label_2.setText(QCoreApplication.translate("keycast", f"{event.text().capitalize()}", None))
        return QWidget().keyPressEvent(event)
    
    def keyReleaseEvent(self, event) :
        # print(event.keyCombination().keyboardModifiers())
        # print(event.keyCombination().key().name)
        time.sleep(0.3)
        self.ui.label_2.setText(QCoreApplication.translate("keycast", f"", None))

        return QWidget().keyPressEvent(event)



if __name__ == '__main__':
    app = QApplication([])
    window = App()
    window.show()
  
    
    exit(app.exec())


Problem: My Pyside6 application Event is not working in the background and I need window type WindowStaysOnTopHint.

What I have tried:

example : When I open my application and when my application is selected, it shows me the keyboard key name in the label and when I select another application, my app pauses and does not show the keyboard key name.

alternative try : I also tried pynput module but that too is not working. In that I am getting key error because when application pauses then event listener function is not able to be called
Posted
Updated 5-Jul-23 0:29am
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900