import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import *
#from PyQt5.QtCore import QCoreApplication
from pynput import keyboard
class Geo:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def __init__
(self, key_name, key_char, geo, parent
=None, highlight_color
=QColor(255,
255,
0,
200)): super(Alert, self).__init__(parent)
self.key_name = key_name
self.geo = geo
rect
= QRectF(0,
0, geo.
w, geo.
h) normal.setBrush(Qt.darkGray)
normal.setPen(Qt.white)
brush
= QtGui.
QBrush(Qt.
white) normal.setBrush(brush)
self.normal = normal
#self.highlighted = QtWidgets.QGraphicsRectItem(normal)
brush2
= QtGui.
QBrush(highlight_color
) self.highlighted.setBrush(brush2)
#self.highlighted.setBrush(highlight_color)
#self.highlighted.setBrush(brush)
txt.setX(geo.w/2)
#self.highlighted.hide()
def highlight(self, on=False):
if on:
self.highlighted.show()
self.normal.hide()
else:
self.highlighted.hide()
self.normal.show()
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
geo = Geo(10, 10, 50, 50)
self.buzz = Alert("Buzz", "Buzz", geo)
self.scene.addItem(self.buzz)
self.
buzz.
setPos(QPointF(130,
120))
self.create_central_widget()
self.listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release)
self.listener.start()
def create_layout(self):
self.layout = QtWidgets.QGraphicsLinearLayout()
self.widget = QtWidgets.QGraphicsWidget()
self.widget.setLayout(self.layout)
self.scene.addItem(self.widget)
width = self.keyboard_width
height = self.keyboard_height
self.setMinimumSize(width, height)
self.scene.setSceneRect(0, 0, width, height)
def create_central_widget(self):
self.
view.
setBackgroundBrush((QColor("bisque"))) self.setCentralWidget(self.view)
self.view.scale(3, 3)
def on_press(self, key):
self.buzz.highlight(True)
def on_release(self, key):
self.buzz.highlight(False)
def main():
w = MainWindow()
w.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QPainter, QColor
#from PyQt5.QtCore import QCoreApplication
from pynput import keyboard
class Geo:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
class Alert(QtWidgets.QGraphicsRectItem):
def __init__(self, key_name, key_char, geo, parent=None, highlight_color=QColor(255, 255, 0, 200)):
super(Alert, self).__init__(parent)
self.key_name = key_name
self.geo = geo
rect = QRectF(0, 0, geo.w, geo.h)
normal = QtWidgets.QGraphicsRectItem(rect, self)
normal.setBrush(Qt.darkGray)
normal.setPen(Qt.white)
normal_txt = QtWidgets.QGraphicsSimpleTextItem(key_char, normal)
brush = QtGui.QBrush(Qt.white)
normal.setBrush(brush)
self.normal = normal
#self.highlighted = QtWidgets.QGraphicsRectItem(normal)
self.highlighted = QtWidgets.QGraphicsRectItem(self)
brush2 = QtGui.QBrush(highlight_color)
self.highlighted.setBrush(brush2)
#self.highlighted.setBrush(highlight_color)
#self.highlighted.setBrush(brush)
txt = QtWidgets.QGraphicsTextItem(key_char, self.highlighted)
txt.setX(geo.w/2)
#self.highlighted.hide()
def highlight(self, on=False):
if on:
self.highlighted.show()
self.normal.hide()
else:
self.highlighted.hide()
self.normal.show()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.scene = QtWidgets.QGraphicsScene(self)
self.scene.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex)
geo = Geo(10, 10, 50, 50)
self.buzz = Alert("Buzz", "Buzz", geo)
self.scene.addItem(self.buzz)
self.buzz.setPos(QPointF(130, 120))
self.create_central_widget()
self.listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release)
self.listener.start()
def create_layout(self):
self.layout = QtWidgets.QGraphicsLinearLayout()
self.widget = QtWidgets.QGraphicsWidget()
self.widget.setLayout(self.layout)
self.scene.addItem(self.widget)
width = self.keyboard_width
height = self.keyboard_height
self.setMinimumSize(width, height)
self.scene.setSceneRect(0, 0, width, height)
def create_central_widget(self):
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)
self.view.setBackgroundBrush((QColor("bisque")))
self.setCentralWidget(self.view)
self.view.scale(3, 3)
def on_press(self, key):
self.buzz.highlight(True)
def on_release(self, key):
self.buzz.highlight(False)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
To copy to clipboard, switch view to plain text mode
Bookmarks