Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(950, 495)
MainWindow.setAnimated(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.keyboard = QtWidgets.QLabel(self.centralwidget)
self.keyboard.setGeometry(QtCore.QRect(0, -30, 681, 351))
self.keyboard.setText("")
self.keyboard.setPixmap(QtGui.QPixmap("../Downloads/f.png"))
self.keyboard.setObjectName("keyboard")
self.f1 = QtWidgets.QLabel(self.centralwidget)
self.f1.setGeometry(QtCore.QRect(70, 20, 21, 31))
self.f1.setObjectName("f1")
self.f4 = QtWidgets.QLabel(self.centralwidget)
self.f4.setGeometry(QtCore.QRect(200, 20, 21, 31))
self.f4.setObjectName("f4")
self.f3 = QtWidgets.QLabel(self.centralwidget)
self.f3.setGeometry(QtCore.QRect(160, 20, 21, 31))
self.f3.setObjectName("f3")
self.f2 = QtWidgets.QLabel(self.centralwidget)
self.f2.setGeometry(QtCore.QRect(110, 20, 21, 31))
self.f2.setObjectName("f2")
self.esc = QtWidgets.QLabel(self.centralwidget)
self.esc.setGeometry(QtCore.QRect(30, 20, 21, 31))
self.esc.setObjectName("esc")
self.f5 = QtWidgets.QLabel(self.centralwidget)
self.f5.setGeometry(QtCore.QRect(240, 20, 21, 31))
self.f5.setObjectName("f5")
self.f6 = QtWidgets.QLabel(self.centralwidget)
self.f6.setGeometry(QtCore.QRect(290, 20, 21, 31))
self.f6.setObjectName("f6")
self.f7 = QtWidgets.QLabel(self.centralwidget)
self.f7.setGeometry(QtCore.QRect(330, 20, 21, 31))
self.f7.setObjectName("f7")
self.W = QtWidgets.QLabel(self.centralwidget)
self.W.setGeometry(QtCore.QRect(140, 90, 21, 31))
self.W.setObjectName("W")
self.S = QtWidgets.QLabel(self.centralwidget)
self.S.setGeometry(QtCore.QRect(150, 130, 21, 31))
self.S.setObjectName("S")
self.A = QtWidgets.QLabel(self.centralwidget)
self.A.setGeometry(QtCore.QRect(110, 130, 21, 31))
self.A.setObjectName("A")
self.D = QtWidgets.QLabel(self.centralwidget)
self.D.setGeometry(QtCore.QRect(190, 130, 21, 31))
self.D.setObjectName("D")
self.Ctrl = QtWidgets.QLabel(self.centralwidget)
self.Ctrl.setGeometry(QtCore.QRect(30, 220, 21, 31))
self.Ctrl.setObjectName("Ctrl")
self.Alt = QtWidgets.QLabel(self.centralwidget)
self.Alt.setGeometry(QtCore.QRect(160, 220, 21, 31))
self.Alt.setObjectName("Alt")
self.Win = QtWidgets.QLabel(self.centralwidget)
self.Win.setGeometry(QtCore.QRect(110, 220, 21, 31))
self.Win.setObjectName("Win")
self.ShifL = QtWidgets.QLabel(self.centralwidget)
self.ShifL.setGeometry(QtCore.QRect(30, 180, 81, 21))
self.ShifL.setObjectName("ShifL")
self.Enter = QtWidgets.QLabel(self.centralwidget)
self.Enter.setGeometry(QtCore.QRect(560, 130, 41, 31))
self.Enter.setObjectName("Enter")
self.Backspace = QtWidgets.QLabel(self.centralwidget)
self.Backspace.setGeometry(QtCore.QRect(570, 50, 41, 31))
font = QtGui.QFont()
font.setPointSize(20)
self.Backspace.setFont(font)
self.Backspace.setObjectName("Backspace")
self.dlt = QtWidgets.QLabel(self.centralwidget)
self.dlt.setGeometry(QtCore.QRect(590, 20, 21, 31))
self.dlt.setObjectName("dlt")
self.Tab = QtWidgets.QLabel(self.centralwidget)
self.Tab.setGeometry(QtCore.QRect(30, 90, 41, 31))
self.Tab.setObjectName("Tab")
self.tild = QtWidgets.QLabel(self.centralwidget)
self.tild.setGeometry(QtCore.QRect(30, 50, 21, 31))
self.tild.setObjectName("tild")
self.ShiftR = QtWidgets.QLabel(self.centralwidget)
self.ShiftR.setGeometry(QtCore.QRect(540, 180, 71, 21))
self.ShiftR.setObjectName("ShiftR")
self.F = QtWidgets.QLabel(self.centralwidget)
self.F.setGeometry(QtCore.QRect(230, 130, 21, 31))
self.F.setObjectName("F")
self.G = QtWidgets.QLabel(self.centralwidget)
self.G.setGeometry(QtCore.QRect(270, 130, 21, 31))
self.G.setObjectName("G")
self.H = QtWidgets.QLabel(self.centralwidget)
self.H.setGeometry(QtCore.QRect(310, 130, 21, 31))
self.H.setObjectName("H")
self.J = QtWidgets.QLabel(self.centralwidget)
self.J.setGeometry(QtCore.QRect(350, 130, 21, 31))
self.J.setObjectName("J")
self.K = QtWidgets.QLabel(self.centralwidget)
self.K.setGeometry(QtCore.QRect(390, 130, 21, 31))
self.K.setObjectName("K")
self.L = QtWidgets.QLabel(self.centralwidget)
self.L.setGeometry(QtCore.QRect(430, 130, 21, 31))
self.L.setObjectName("L")
self.colon = QtWidgets.QLabel(self.centralwidget)
self.colon.setGeometry(QtCore.QRect(470, 130, 21, 31))
self.colon.setObjectName("colon")
self.quatmark = QtWidgets.QLabel(self.centralwidget)
self.quatmark.setGeometry(QtCore.QRect(520, 130, 21, 31))
self.quatmark.setObjectName("quatmark")
self.Z = QtWidgets.QLabel(self.centralwidget)
self.Z.setGeometry(QtCore.QRect(120, 170, 21, 31))
self.Z.setObjectName("Z")
self.X = QtWidgets.QLabel(self.centralwidget)
self.X.setGeometry(QtCore.QRect(160, 170, 21, 31))
self.X.setObjectName("X")
self.C = QtWidgets.QLabel(self.centralwidget)
self.C.setGeometry(QtCore.QRect(210, 170, 21, 31))
self.C.setObjectName("C")
self.V = QtWidgets.QLabel(self.centralwidget)
self.V.setGeometry(QtCore.QRect(250, 170, 21, 31))
self.V.setObjectName("V")
self.B = QtWidgets.QLabel(self.centralwidget)
self.B.setGeometry(QtCore.QRect(290, 170, 21, 31))
self.B.setObjectName("B")
self.N = QtWidgets.QLabel(self.centralwidget)
self.N.setGeometry(QtCore.QRect(330, 170, 21, 31))
self.N.setObjectName("N")
self.M = QtWidgets.QLabel(self.centralwidget)
self.M.setGeometry(QtCore.QRect(370, 170, 21, 31))
self.M.setObjectName("M")
self.rig = QtWidgets.QLabel(self.centralwidget)
self.rig.setGeometry(QtCore.QRect(410, 170, 21, 31))
self.rig.setObjectName("rig")
self.lef = QtWidgets.QLabel(self.centralwidget)
self.lef.setGeometry(QtCore.QRect(450, 170, 21, 31))
self.lef.setObjectName("lef")
self.questmark = QtWidgets.QLabel(self.centralwidget)
self.questmark.setGeometry(QtCore.QRect(500, 170, 21, 31))
self.questmark.setObjectName("questmark")
self.AltR = QtWidgets.QLabel(self.centralwidget)
self.AltR.setGeometry(QtCore.QRect(420, 220, 21, 31))
self.AltR.setObjectName("AltR")
self.CtrlR = QtWidgets.QLabel(self.centralwidget)
self.CtrlR.setGeometry(QtCore.QRect(460, 220, 21, 31))
self.CtrlR.setObjectName("CtrlR")
self.up = QtWidgets.QLabel(self.centralwidget)
self.up.setGeometry(QtCore.QRect(550, 210, 16, 16))
self.up.setObjectName("up")
self.down = QtWidgets.QLabel(self.centralwidget)
self.down.setGeometry(QtCore.QRect(550, 230, 21, 21))
self.down.setObjectName("down")
self.left = QtWidgets.QLabel(self.centralwidget)
self.left.setGeometry(QtCore.QRect(510, 230, 21, 31))
self.left.setObjectName("left")
self.right = QtWidgets.QLabel(self.centralwidget)
self.right.setGeometry(QtCore.QRect(590, 230, 21, 31))
self.right.setObjectName("right")
self.CapsLock = QtWidgets.QLabel(self.centralwidget)
self.CapsLock.setGeometry(QtCore.QRect(30, 140, 61, 31))
self.CapsLock.setObjectName("CapsLock")
self.Q = QtWidgets.QLabel(self.centralwidget)
self.Q.setGeometry(QtCore.QRect(90, 90, 21, 31))
self.Q.setObjectName("Q")
self.E = QtWidgets.QLabel(self.centralwidget)
self.E.setGeometry(QtCore.QRect(180, 90, 21, 31))
self.E.setObjectName("E")
self.R = QtWidgets.QLabel(self.centralwidget)
self.R.setGeometry(QtCore.QRect(220, 90, 21, 31))
self.R.setObjectName("R")
self.T = QtWidgets.QLabel(self.centralwidget)
self.T.setGeometry(QtCore.QRect(260, 90, 21, 31))
self.T.setObjectName("T")
self.Y = QtWidgets.QLabel(self.centralwidget)
self.Y.setGeometry(QtCore.QRect(300, 90, 21, 31))
self.Y.setObjectName("Y")
self.U = QtWidgets.QLabel(self.centralwidget)
self.U.setGeometry(QtCore.QRect(340, 90, 21, 31))
self.U.setObjectName("U")
self.I = QtWidgets.QLabel(self.centralwidget)
self.I.setGeometry(QtCore.QRect(380, 90, 21, 31))
self.I.setObjectName("I")
self.O = QtWidgets.QLabel(self.centralwidget)
self.O.setGeometry(QtCore.QRect(420, 90, 21, 31))
self.O.setObjectName("O")
self.P = QtWidgets.QLabel(self.centralwidget)
self.P.setGeometry(QtCore.QRect(470, 90, 21, 31))
self.P.setObjectName("P")
self.curlybrace1 = QtWidgets.QLabel(self.centralwidget)
self.curlybrace1.setGeometry(QtCore.QRect(500, 90, 21, 31))
self.curlybrace1.setObjectName("curlybrace1")
self.curlybrace2 = QtWidgets.QLabel(self.centralwidget)
self.curlybrace2.setGeometry(QtCore.QRect(550, 90, 20, 31))
self.curlybrace2.setObjectName("curlybrace2")
self.revslash = QtWidgets.QLabel(self.centralwidget)
self.revslash.setGeometry(QtCore.QRect(590, 90, 31, 31))
self.revslash.setObjectName("revslash")
self.n1 = QtWidgets.QLabel(self.centralwidget)
self.n1.setGeometry(QtCore.QRect(70, 50, 21, 31))
self.n1.setObjectName("n1")
self.n2 = QtWidgets.QLabel(self.centralwidget)
self.n2.setGeometry(QtCore.QRect(110, 50, 21, 31))
self.n2.setObjectName("n2")
self.n3 = QtWidgets.QLabel(self.centralwidget)
self.n3.setGeometry(QtCore.QRect(150, 50, 21, 31))
self.n3.setObjectName("n3")
self.n4 = QtWidgets.QLabel(self.centralwidget)
self.n4.setGeometry(QtCore.QRect(190, 50, 21, 31))
self.n4.setObjectName("n4")
self.n5 = QtWidgets.QLabel(self.centralwidget)
self.n5.setGeometry(QtCore.QRect(240, 50, 21, 31))
self.n5.setObjectName("n5")
self.n6 = QtWidgets.QLabel(self.centralwidget)
self.n6.setGeometry(QtCore.QRect(280, 50, 21, 31))
self.n6.setObjectName("n6")
self.n7 = QtWidgets.QLabel(self.centralwidget)
self.n7.setGeometry(QtCore.QRect(320, 50, 21, 31))
self.n7.setObjectName("n7")
self.n8 = QtWidgets.QLabel(self.centralwidget)
self.n8.setGeometry(QtCore.QRect(360, 50, 21, 31))
self.n8.setObjectName("n8")
self.n9 = QtWidgets.QLabel(self.centralwidget)
self.n9.setGeometry(QtCore.QRect(400, 50, 21, 31))
self.n9.setObjectName("n9")
self.n0 = QtWidgets.QLabel(self.centralwidget)
self.n0.setGeometry(QtCore.QRect(440, 50, 21, 31))
self.n0.setObjectName("n0")
self.dash = QtWidgets.QLabel(self.centralwidget)
self.dash.setGeometry(QtCore.QRect(480, 50, 21, 31))
self.dash.setObjectName("dash")
self.plus = QtWidgets.QLabel(self.centralwidget)
self.plus.setGeometry(QtCore.QRect(520, 50, 21, 31))
self.plus.setObjectName("plus")
self.f8 = QtWidgets.QLabel(self.centralwidget)
self.f8.setGeometry(QtCore.QRect(370, 20, 21, 31))
self.f8.setObjectName("f8")
self.f9 = QtWidgets.QLabel(self.centralwidget)
self.f9.setGeometry(QtCore.QRect(420, 20, 21, 31))
self.f9.setObjectName("f9")
self.f10 = QtWidgets.QLabel(self.centralwidget)
self.f10.setGeometry(QtCore.QRect(460, 20, 21, 31))
self.f10.setObjectName("f10")
self.f11 = QtWidgets.QLabel(self.centralwidget)
self.f11.setGeometry(QtCore.QRect(500, 20, 21, 31))
self.f11.setObjectName("f11")
self.f12 = QtWidgets.QLabel(self.centralwidget)
self.f12.setGeometry(QtCore.QRect(550, 20, 21, 31))
self.f12.setObjectName("f12")
self.mouse = QtWidgets.QLabel(self.centralwidget)
self.mouse.setGeometry(QtCore.QRect(590, 0, 401, 361))
self.mouse.setText("")
self.mouse.setPixmap(QtGui.QPixmap("255016A1.png"))
self.mouse.setObjectName("mouse")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(700, 80, 71, 111))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.rightbuttonmouse = QtWidgets.QLabel(self.centralwidget)
self.rightbuttonmouse.setGeometry(QtCore.QRect(800, 80, 71, 111))
font = QtGui.QFont()
font.setPointSize(12)
self.rightbuttonmouse.setFont(font)
self.rightbuttonmouse.setObjectName("rightbuttonmouse")
self.middlebuttonmouse = QtWidgets.QLabel(self.centralwidget)
self.middlebuttonmouse.setGeometry(QtCore.QRect(780, 50, 16, 51))
self.middlebuttonmouse.setText("")
self.middlebuttonmouse.setObjectName("middlebuttonmouse")
self.space = QtWidgets.QLabel(self.centralwidget)
self.space.setGeometry(QtCore.QRect(200, 220, 201, 31))
self.space.setObjectName("space")
self.time_right = QtWidgets.QLabel(self.centralwidget)
self.time_right.setGeometry(QtCore.QRect(20, 260, 611, 51))
font = QtGui.QFont()
font.setPointSize(9)
self.time_right.setFont(font)
self.time_right.setObjectName("time_right")
self.time_left = QtWidgets.QLabel(self.centralwidget)
self.time_left.setGeometry(QtCore.QRect(20, 300, 611, 51))
font = QtGui.QFont()
font.setPointSize(9)
self.time_left.setFont(font)
self.time_left.setObjectName("time_left")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 950, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "skolkovo"))
self.f1.setText(_translate("MainWindow", "F1"))
self.f4.setText(_translate("MainWindow", "F4"))
self.f3.setText(_translate("MainWindow", "F3"))
self.f2.setText(_translate("MainWindow", "F2"))
self.esc.setText(_translate("MainWindow", "Esc"))
self.f5.setText(_translate("MainWindow", "F5"))
self.f6.setText(_translate("MainWindow", "F6"))
self.f7.setText(_translate("MainWindow", "F7"))
self.W.setText(_translate("MainWindow", "W"))
self.S.setText(_translate("MainWindow", "S"))
self.A.setText(_translate("MainWindow", "A"))
self.D.setText(_translate("MainWindow", "D"))
self.Ctrl.setText(_translate("MainWindow", "Ctrl"))
self.Alt.setText(_translate("MainWindow", "Alt"))
self.Win.setText(_translate("MainWindow", "Win"))
self.ShifL.setText(_translate("MainWindow", "Shift ⇫"))
self.Enter.setText(_translate("MainWindow", "↵ Enter"))
self.Backspace.setText(_translate("MainWindow", "⇽"))
self.dlt.setText(_translate("MainWindow", "Dlt"))
self.Tab.setText(_translate("MainWindow", "Tab ⭾"))
self.tild.setText(_translate("MainWindow", "`"))
self.ShiftR.setText(_translate("MainWindow", "Shift"))
self.F.setText(_translate("MainWindow", "F"))
self.G.setText(_translate("MainWindow", "G"))
self.H.setText(_translate("MainWindow", "H"))
self.J.setText(_translate("MainWindow", "J"))
self.K.setText(_translate("MainWindow", " K"))
self.L.setText(_translate("MainWindow", " L"))
self.colon.setText(_translate("MainWindow", " : ;"))
self.quatmark.setText(_translate("MainWindow", "\""))
self.Z.setText(_translate("MainWindow", "Z"))
self.X.setText(_translate("MainWindow", "X"))
self.C.setText(_translate("MainWindow", "C"))
self.V.setText(_translate("MainWindow", "V"))
self.B.setText(_translate("MainWindow", "B"))
self.N.setText(_translate("MainWindow", "N"))
self.M.setText(_translate("MainWindow", "M"))
self.rig.setText(_translate("MainWindow", "<"))
self.lef.setText(_translate("MainWindow", ">"))
self.questmark.setText(_translate("MainWindow", "?"))
self.AltR.setText(_translate("MainWindow", "Alt"))
self.CtrlR.setText(_translate("MainWindow", "Ctrl"))
self.up.setText(_translate("MainWindow", " ↑"))
self.down.setText(_translate("MainWindow", " ↓"))
self.left.setText(_translate("MainWindow", "←"))
self.right.setText(_translate("MainWindow", "→"))
self.CapsLock.setText(_translate("MainWindow", "Caps Lock"))
self.Q.setText(_translate("MainWindow", "Q"))
self.E.setText(_translate("MainWindow", "E"))
self.R.setText(_translate("MainWindow", "R"))
self.T.setText(_translate("MainWindow", "T"))
self.Y.setText(_translate("MainWindow", "Y"))
self.U.setText(_translate("MainWindow", "U"))
self.I.setText(_translate("MainWindow", " I"))
self.O.setText(_translate("MainWindow", " O"))
self.P.setText(_translate("MainWindow", "P"))
self.curlybrace1.setText(_translate("MainWindow", " {"))
self.curlybrace2.setText(_translate("MainWindow", "}"))
self.revslash.setText(_translate("MainWindow", "| \\"))
self.n1.setText(_translate("MainWindow", "1"))
self.n2.setText(_translate("MainWindow", "2"))
self.n3.setText(_translate("MainWindow", "3"))
self.n4.setText(_translate("MainWindow", "4"))
self.n5.setText(_translate("MainWindow", "5"))
self.n6.setText(_translate("MainWindow", "6"))
self.n7.setText(_translate("MainWindow", "7"))
self.n8.setText(_translate("MainWindow", "8"))
self.n9.setText(_translate("MainWindow", "9"))
self.n0.setText(_translate("MainWindow", "0"))
self.dash.setText(_translate("MainWindow", "- _"))
self.plus.setText(_translate("MainWindow", "+ ="))
self.f8.setText(_translate("MainWindow", "F8"))
self.f9.setText(_translate("MainWindow", "F9"))
self.f10.setText(_translate("MainWindow", "F10"))
self.f11.setText(_translate("MainWindow", "F11"))
self.f12.setText(_translate("MainWindow", "F12"))
self.label.setText(_translate("MainWindow", " ЛКМ"))
self.rightbuttonmouse.setText(_translate("MainWindow", " ПКМ"))
self.space.setText(_translate("MainWindow", " Space"))
self.time_right.setText(_translate("MainWindow", "Время нажатия на кнопку A: "))
self.time_left.setText(_translate("MainWindow", "Время нажатия на кнопку D: "))
keymap = {}
for key, value in vars(Qt).items():
if isinstance(value, Qt.Key):
keymap[value] = key.partition('_')[2]
modmap = {
Qt.ControlModifier: keymap[Qt.Key_Control],
Qt.AltModifier: keymap[Qt.Key_Alt],
Qt.ShiftModifier: keymap[Qt.Key_Shift],
Qt.MetaModifier: keymap[Qt.Key_Meta],
Qt.GroupSwitchModifier: keymap[Qt.Key_AltGr],
Qt.KeypadModifier: keymap[Qt.Key_NumLock],
}
class Example(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.prev_key = []
self.key_by_time = dict()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.label.setStyleSheet("color: red;")
self.prev_key = [self.label, ]
elif ev.button() == Qt.RightButton:
self.rightbuttonmouse.setStyleSheet("color: red;")
self.prev_key = [self.rightbuttonmouse, ]
elif ev.button() == Qt.MiddleButton:
self.middlebuttonmouse.setStyleSheet("background-color: red; color: red;")
self.prev_key = [self.middlebuttonmouse, ]
def mouseReleaseEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.label.setStyleSheet("color: black;")
self.prev_key = [self.label, ]
elif ev.button() == Qt.RightButton:
self.rightbuttonmouse.setStyleSheet("color: black;")
self.prev_key = [self.rightbuttonmouse, ]
elif ev.button() == Qt.MiddleButton:
self.middlebuttonmouse.setStyleSheet("background-color: ;")
self.prev_key = [self.middlebuttonmouse, ]
def keyPressEvent(self, event):
_key, _keys = self.keyevent_to_string(event)
key = event.key()
if not event.isAutoRepeat() and key not in self.key_by_time:
self.key_by_time[key] = time.perf_counter()
if self.prev_key:
for k in self.prev_key:
k.setStyleSheet("")
if _keys[0] == 'Control' and key == Qt.Key_W:
self.W.setStyleSheet("color: red;")
self.Ctrl.setStyleSheet("color: red;")
self.prev_key = [self.Ctrl, self.W]
elif _keys[0] == 'Alt' and key == Qt.Key_W:
self.W.setStyleSheet("color: red;")
self.Alt.setStyleSheet("color: red;")
self.prev_key = [self.Alt, self.W]
elif _keys[0] == 'Shift' and key == Qt.Key_W:
self.W.setStyleSheet("color: red;")
self.ShifL.setStyleSheet("color: red;")
self.prev_key = [self.ShifL, self.W]
elif _keys[0] == 'Shift' and key == Qt.Key_Alt:
self.Alt.setStyleSheet("color: red;")
self.ShifL.setStyleSheet("color: red;")
self.prev_key = [self.ShifL, self.Alt]
elif key == Qt.Key_W:
self.W.setStyleSheet("color: red;")
self.prev_key = [self.W, ]
elif key == Qt.Key_A:
self.A.setStyleSheet("color: red;")
self.prev_key = [self.A, ]
elif key == Qt.Key_S:
self.S.setStyleSheet("color: red;")
self.prev_key = [self.S, ]
elif key == Qt.Key_D:
self.D.setStyleSheet("color: red;")
self.prev_key = [self.D, ]
elif key == Qt.Key_Space:
self.space.setStyleSheet("color: red;")
self.prev_key = [self.D, ]
elif key == Qt.Key_Escape:
self.esc.setStyleSheet("color: red;")
self.prev_key = [self.esc, ]
elif key == Qt.Key_Tab:
self.Tab.setStyleSheet("color: red;")
self.prev_key = [self.Tab, ]
elif key == Qt.Key_Backspace:
self.Backspace.setStyleSheet("color: red;")
self.prev_key = [self.Backspace, ]
elif key == Qt.Key_Enter:
self.Enter.setStyleSheet("color: red;")
self.prev_key = [self.Enter, ]
elif key == Qt.Key_Return:
self.Enter.setStyleSheet("color: red;")
self.prev_key = [self.Enter, ]
elif key == Qt.Key_QuoteLeft:
self.tild.setStyleSheet("color: red;")
self.prev_key = [self.tild, ]
elif key == Qt.Key_Delete:
self.dlt.setStyleSheet("color: red;")
self.prev_key = [self.dlt, ]
elif key == Qt.Key_Shift:
self.ShifL.setStyleSheet("color: red;")
self.ShiftR.setStyleSheet("color: red;")
self.prev_key = [self.ShifL, self.ShiftR]
elif key == Qt.Key_Control:
self.Ctrl.setStyleSheet("color: red;")
self.prev_key = [self.Ctrl, ]
elif key == Qt.Key_Alt:
self.Alt.setStyleSheet("color: red;")
self.prev_key = [self.Alt, ]
elif key == Qt.Key_Meta:
self.Win.setStyleSheet(" color: red;")
self.prev_key = [self.Win, ]
elif key == Qt.Key_Q:
self.Q.setStyleSheet("color: red;")
self.prev_key = [self.Q, ]
elif key == Qt.Key_E:
self.E.setStyleSheet("color: red;")
self.prev_key = [self.E, ]
elif key == Qt.Key_T:
self.T.setStyleSheet("color: red;")
self.prev_key = [self.T, ]
elif key == Qt.Key_R:
self.R.setStyleSheet("color: red;")
self.prev_key = [self.R, ]
elif key == Qt.Key_Y:
self.Y.setStyleSheet("color: red;")
self.prev_key = [self.Y, ]
elif key == Qt.Key_U:
self.U.setStyleSheet("color: red;")
self.prev_key = [self.U, ]
elif key == Qt.Key_I:
self.I.setStyleSheet("color: red;")
self.prev_key = [self.I, ]
elif key == Qt.Key_O:
self.O.setStyleSheet("color: red;")
self.prev_key = [self.O, ]
elif key == Qt.Key_P:
self.P.setStyleSheet("color: red;")
self.prev_key = [self.P, ]
elif key == Qt.Key_F:
self.F.setStyleSheet("color: red;")
self.prev_key = [self.F, ]
elif key == Qt.Key_G:
self.G.setStyleSheet("color: red;")
self.prev_key = [self.G, ]
elif key == Qt.Key_H:
self.H.setStyleSheet("color: red;")
self.prev_key = [self.H, ]
elif key == Qt.Key_J:
self.J.setStyleSheet("color: red;")
self.prev_key = [self.J, ]
elif key == Qt.Key_K:
self.K.setStyleSheet("color: red;")
self.prev_key = [self.K, ]
elif key == Qt.Key_L:
self.L.setStyleSheet("color: red;")
self.prev_key = [self.L, ]
elif key == Qt.Key_Z:
self.Z.setStyleSheet("color: red;")
self.prev_key = [self.Z, ]
elif key == Qt.Key_X:
self.X.setStyleSheet("color: red;")
self.prev_key = [self.X, ]
elif key == Qt.Key_C:
self.C.setStyleSheet("color: red;")
self.prev_key = [self.C, ]
elif key == Qt.Key_V:
self.V.setStyleSheet("color: red;")
self.prev_key = [self.V, ]
elif key == Qt.Key_B:
self.B.setStyleSheet("color: red;")
self.prev_key = [self.B, ]
elif key == Qt.Key_N:
self.N.setStyleSheet("color: red;")
self.prev_key = [self.N, ]
elif key == Qt.Key_M:
self.M.setStyleSheet("color: red;")
self.prev_key = [self.M, ]
elif key == Qt.Key_F1:
self.f1.setStyleSheet("color: red;")
self.prev_key = [self.f1, ]
elif key == Qt.Key_F2:
self.f2.setStyleSheet("color: red;")
self.prev_key = [self.f2, ]
elif key == Qt.Key_F3:
self.f3.setStyleSheet("color: red;")
self.prev_key = [self.f3, ]
elif key == Qt.Key_F4:
self.f4.setStyleSheet("color: red;")
self.prev_key = [self.f4, ]
elif key == Qt.Key_F5:
self.f5.setStyleSheet("color: red;")
self.prev_key = [self.f5, ]
elif key == Qt.Key_F6:
self.f6.setStyleSheet("color: red;")
self.prev_key = [self.f6, ]
elif key == Qt.Key_F7:
self.f7.setStyleSheet("color: red;")
self.prev_key = [self.f7, ]
elif key == Qt.Key_F8:
self.f8.setStyleSheet("color: red;")
self.prev_key = [self.f8, ]
elif key == Qt.Key_F9:
self.f9.setStyleSheet("color: red;")
self.prev_key = [self.f9, ]
elif key == Qt.Key_F10:
self.f10.setStyleSheet("color: red;")
self.prev_key = [self.f10, ]
elif key == Qt.Key_F11:
self.f11.setStyleSheet("color: red;")
self.prev_key = [self.f11, ]
elif key == Qt.Key_F12:
self.f12.setStyleSheet("color: red;")
self.prev_key = [self.f12, ]
elif key == Qt.Key_1:
self.n1.setStyleSheet("color: red;")
self.prev_key = [self.n1,]
elif key == Qt.Key_2:
self.n2.setStyleSheet("color: red;")
self.prev_key = [self.n2, ]
elif key == Qt.Key_3:
self.n3.setStyleSheet("color: red;")
self.prev_key = [self.n3, ]
elif key == Qt.Key_4:
self.n4.setStyleSheet("color: red;")
self.prev_key = [self.n4, ]
elif key == Qt.Key_5:
self.n5.setStyleSheet("color: red;")
self.prev_key = [self.n5, ]
elif key == Qt.Key_6:
self.n6.setStyleSheet("color: red;")
self.prev_key = [self.n6, ]
elif key == Qt.Key_7:
self.n7.setStyleSheet("color: red;")
self.prev_key = [self.n7, ]
elif key == Qt.Key_8:
self.n8.setStyleSheet("color: red;")
self.prev_key = [self.n8, ]
elif key == Qt.Key_9:
self.n9.setStyleSheet("color: red;")
self.prev_key = [self.n9, ]
elif key == Qt.Key_0:
self.n0.setStyleSheet("color: red;")
self.prev_key = [self.n0, ]
elif key == Qt.Key_CapsLock:
self.CapsLock.setStyleSheet("color: red;")
self.prev_key = [self.CapsLock, ]
elif key == Qt.Key_Apostrophe:
self.quatmark.setStyleSheet("color: red;")
self.prev_key = [self.quatmark, ]
elif key == Qt.Key_Backslash:
self.revslash.setStyleSheet("color: red;")
self.prev_key = [self.revslash, ]
elif key == Qt.Key_Up:
self.up.setStyleSheet("color: red;")
self.prev_key = [self.up, ]
elif key == Qt.Key_Down:
self.down.setStyleSheet("color: red;")
self.prev_key = [self.down, ]
elif key == Qt.Key_Right:
self.right.setStyleSheet("color: red;")
self.prev_key = [self.right, ]
elif key == Qt.Key_Left:
self.left.setStyleSheet("color: red;")
self.prev_key = [self.left, ]
elif key == Qt.Key_BracketLeft:
self.curlybrace1.setStyleSheet("color: red;")
self.prev_key = [self.curlybrace1, ]
elif key == Qt.Key_BracketRight:
self.curlybrace2.setStyleSheet("color: red;")
self.prev_key = [self.curlybrace2, ]
elif key == Qt.Key_Comma:
self.rig.setStyleSheet("color: red;")
self.prev_key = [self.rig, ]
elif key == Qt.Key_Period:
self.lef.setStyleSheet("color: red;")
self.prev_key = [self.lef, ]
elif key == Qt.Key_Slash:
self.questmark.setStyleSheet("color: red;")
self.prev_key = [self.questmark, ]
elif key == Qt.Key_Minus:
self.dash.setStyleSheet("color: red;")
self.prev_key = [self.dash, ]
elif key == Qt.Key_Semicolon:
self.colon.setStyleSheet("color: red;")
self.prev_key = [self.colon, ]
elif key == Qt.Key_Equal:
self.plus.setStyleSheet("color: red;")
self.prev_key = [self.plus, ]
def keyevent_to_string(self, event):
sequence = []
for modifier, text in modmap.items():
if event.modifiers() & modifier:
sequence.append(text)
key = keymap.get(event.key(), event.text())
if key not in sequence:
sequence.append(key)
return '+'.join(sequence), sequence
def keyReleaseEvent(self, event):
_key, _keys = self.keyevent_to_string(event)
key = event.key()
if self.prev_key:
for k in self.prev_key:
k.setStyleSheet("")
if _keys[0] == 'Control' and key == Qt.Key_W:
self.W.setStyleSheet("color: black;")
self.Ctrl.setStyleSheet("color: black;")
self.prev_key = [self.Ctrl, self.W]
# ...
elif _keys[0] == 'Alt' and key == Qt.Key_W:
self.W.setStyleSheet("color: black;")
self.Alt.setStyleSheet("color: black;")
self.prev_key = [self.Alt, self.W]
elif key == Qt.Key_BracketRight:
self.curlybrace2.setStyleSheet("color: black;")
self.prev_key = [self.curlybrace2, ]
elif key == Qt.Key_Semicolon:
self.colon.setStyleSheet("color: black;")
self.prev_key = [self.colon, ]
elif key == Qt.Key_BracketLeft:
self.curlybrace1.setStyleSheet("color: black;")
self.prev_key = [self.curlybrace1, ]
elif key == Qt.Key_BracketLeft:
self.curlybrace1.setStyleSheet("color: black;")
self.prev_key = [self.curlybrace1, ]
elif key == Qt.Key_Space:
self.space.setStyleSheet("color: black;")
self.prev_key = [self.space, ]
elif key == Qt.Key_BraceLeft:
self.curlybrace1.setStyleSheet("color: black;")
self.prev_key = [self.curlybrace1, ]
elif _keys[0] == 'Shift' and key == Qt.Key_W:
self.W.setStyleSheet("color: black;")
self.ShifL.setStyleSheet("color: black;")
self.prev_key = [self.ShifL, self.W]
elif _keys[0] == 'Shift' and key == Qt.Key_Alt:
self.Alt.setStyleSheet("color: black;")
self.ShifL.setStyleSheet("color: black;")
self.prev_key = [self.ShifL, self.Alt]
elif key == Qt.Key_W:
self.W.setStyleSheet("color: black;")
self.prev_key = [self.W, ]
elif key == Qt.Key_Comma:
self.lef.setStyleSheet("color: black;")
self.prev_key = [self.lef, ]
elif key == Qt.Key_Period:
self.rig.setStyleSheet("color: black;")
self.prev_key = [self.rig, ]
elif key == Qt.Key_Slash:
self.questmark.setStyleSheet("color: black;")
self.prev_key = [self.questmark, ]
elif key == Qt.Key_A:
self.A.setStyleSheet("color: black;")
self.prev_key = [self.A, ]
if not event.isAutoRepeat() and key in self.key_by_time:
t = self.key_by_time.pop(key)
self.time_right.setText(f'Кнопка {key!r} была нажата {time.perf_counter() - t:.2f} секунд')
elif key == Qt.Key_S:
self.S.setStyleSheet("color: black;")
self.prev_key = [self.S, ]
elif key == Qt.Key_D:
self.D.setStyleSheet("color: black;")
self.prev_key = [self.D, ]
elif key == Qt.Key_Escape:
self.esc.setStyleSheet("color: black;")
self.prev_key = [self.esc, ]
elif key == Qt.Key_Tab:
self.Tab.setStyleSheet("color: black;")
self.prev_key = [self.Tab, ]
elif key == Qt.Key_Backspace:
self.Backspace.setStyleSheet("color: black;")
self.prev_key = [self.Backspace, ]
elif key == Qt.Key_Enter:
self.Enter.setStyleSheet("color: black;")
self.prev_key = [self.Enter, ]
elif key == Qt.Key_Return:
self.Enter.setStyleSheet("color: black;")
self.prev_key = [self.Enter, ]
elif key == Qt.Key_QuoteLeft:
self.tild.setStyleSheet("color: black;")
self.prev_key = [self.tild, ]
elif key == Qt.Key_Delete:
self.dlt.setStyleSheet("color: black;")
self.prev_key = [self.dlt, ]
elif key == Qt.Key_Shift:
self.ShifL.setStyleSheet("color: black;")
self.ShiftR.setStyleSheet("color: black;")
self.prev_key = [self.ShifL, self.ShiftR]
elif key == Qt.Key_Control:
self.Ctrl.setStyleSheet("color: black;")
self.prev_key = [self.Ctrl, ]
elif key == Qt.Key_Alt:
self.Alt.setStyleSheet("color: black;")
self.prev_key = [self.Alt, ]
elif key == Qt.Key_Meta:
self.Win.setStyleSheet(" color: black;")
self.prev_key = [self.Win, ]
elif key == Qt.Key_Q:
self.Q.setStyleSheet("color: black;")
self.prev_key = [self.Q, ]
elif key == Qt.Key_E:
self.E.setStyleSheet("color: black;")
self.prev_key = [self.E, ]
elif key == Qt.Key_T:
self.T.setStyleSheet("color: black;")
self.prev_key = [self.T, ]
elif key == Qt.Key_R:
self.R.setStyleSheet("color: black;")
self.prev_key = [self.R, ]
elif key == Qt.Key_Y:
self.Y.setStyleSheet("color: black;")
self.prev_key = [self.Y, ]
elif key == Qt.Key_U:
self.U.setStyleSheet("color: black;")
self.prev_key = [self.U, ]
elif key == Qt.Key_I:
self.I.setStyleSheet("color: black;")
self.prev_key = [self.I, ]
elif key == Qt.Key_O:
self.O.setStyleSheet("color: black;")
self.prev_key = [self.O, ]
elif key == Qt.Key_P:
self.P.setStyleSheet("color: black;")
self.prev_key = [self.P, ]
elif key == Qt.Key_F:
self.F.setStyleSheet("color: black;")
self.prev_key = [self.F, ]
elif key == Qt.Key_G:
self.G.setStyleSheet("color: black;")
self.prev_key = [self.G, ]
elif key == Qt.Key_H:
self.H.setStyleSheet("color: black;")
self.prev_key = [self.H, ]
elif key == Qt.Key_J:
self.J.setStyleSheet("color: black;")
self.prev_key = [self.J, ]
elif key == Qt.Key_K:
self.K.setStyleSheet("color: black;")
self.prev_key = [self.K, ]
elif key == Qt.Key_L:
self.L.setStyleSheet("color: black;")
self.prev_key = [self.L, ]
elif key == Qt.Key_Z:
self.Z.setStyleSheet("color: black;")
self.prev_key = [self.Z, ]
elif key == Qt.Key_X:
self.X.setStyleSheet("color: black;")
self.prev_key = [self.X, ]
elif key == Qt.Key_C:
self.C.setStyleSheet("color: black;")
self.prev_key = [self.C, ]
elif key == Qt.Key_V:
self.V.setStyleSheet("color: black;")
self.prev_key = [self.V, ]
elif key == Qt.Key_B:
self.B.setStyleSheet("color: black;")
self.prev_key = [self.B, ]
elif key == Qt.Key_N:
self.N.setStyleSheet("color: black;")
self.prev_key = [self.N, ]
elif key == Qt.Key_M:
self.M.setStyleSheet("color: black;")
self.prev_key = [self.M, ]
elif key == Qt.Key_F1:
self.f1.setStyleSheet("color: black;")
self.prev_key = [self.f1, ]
elif key == Qt.Key_F2:
self.f2.setStyleSheet("color: black;")
self.prev_key = [self.f2, ]
elif key == Qt.Key_F3:
self.f3.setStyleSheet("color: black;")
self.prev_key = [self.f3, ]
elif key == Qt.Key_F4:
self.f4.setStyleSheet("color: black;")
self.prev_key = [self.f4, ]
elif key == Qt.Key_F5:
self.f5.setStyleSheet("color: black;")
self.prev_key = [self.f5, ]
elif key == Qt.Key_F6:
self.f6.setStyleSheet("color: black;")
self.prev_key = [self.f6, ]
elif key == Qt.Key_F7:
self.f7.setStyleSheet("color: black;")
self.prev_key = [self.f7, ]
elif key == Qt.Key_F8:
self.f8.setStyleSheet("color: black;")
self.prev_key = [self.f8, ]
elif key == Qt.Key_F9:
self.f9.setStyleSheet("color: black;")
self.prev_key = [self.f9, ]
elif key == Qt.Key_F10:
self.f10.setStyleSheet("color: black;")
self.prev_key = [self.f10, ]
elif key == Qt.Key_F11:
self.f11.setStyleSheet("color: black;")
self.prev_key = [self.f11, ]
elif key == Qt.Key_F12:
self.f12.setStyleSheet("color: black;")
self.prev_key = [self.f12, ]
elif key == Qt.Key_1:
self.n1.setStyleSheet("color: black;")
self.prev_key = [self.n1, ]
elif key == Qt.Key_2:
self.n2.setStyleSheet("color: black;")
self.prev_key = [self.n2, ]
elif key == Qt.Key_3:
self.n3.setStyleSheet("color: black;")
self.prev_key = [self.n3, ]
elif key == Qt.Key_4:
self.n4.setStyleSheet("color: black;")
self.prev_key = [self.n4, ]
elif key == Qt.Key_5:
self.n5.setStyleSheet("color: black;")
self.prev_key = [self.n5, ]
elif key == Qt.Key_6:
self.n6.setStyleSheet("color: black;")
self.prev_key = [self.n6, ]
elif key == Qt.Key_7:
self.n7.setStyleSheet("color: black;")
self.prev_key = [self.n7, ]
elif key == Qt.Key_8:
self.n8.setStyleSheet("color: black;")
self.prev_key = [self.n8, ]
elif key == Qt.Key_9:
self.n9.setStyleSheet("color: black;")
self.prev_key = [self.n9, ]
elif key == Qt.Key_0:
self.n0.setStyleSheet("color: black;")
self.prev_key = [self.n0, ]
elif key == Qt.Key_CapsLock:
self.CapsLock.setStyleSheet("color: black;")
self.prev_key = [self.CapsLock, ]
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment