-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_key_window.py
More file actions
73 lines (60 loc) · 2.38 KB
/
assign_key_window.py
File metadata and controls
73 lines (60 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from typing import List
from PySide2.QtCore import Signal
from PySide2.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout, QPushButton
from pynput_robocorp import keyboard
class AssignHotKey(QWidget):
closed = Signal(QWidget, int, list)
def __init__(self, parent=None, index: int = 0):
super().__init__(None)
self.show()
self.launch_window = parent
self.index = index
self.pressed_keys = []
self.vertical_layout = QVBoxLayout(self)
self.label = QLabel(self)
self.load_ui()
self.listener = keyboard.Listener(on_press=self.press_keys,
supress=True)
self.listener.start()
def load_ui(self):
self.label.setFont("Verdana, 25")
self.label.setScaledContents(True)
h_layout = QHBoxLayout()
finish_button = QPushButton("Accept")
clear_button = QPushButton("Clear")
cancel_button = QPushButton("Cancel")
finish_button.clicked.connect(lambda: self.assign(True))
cancel_button.clicked.connect(lambda: self.assign(False))
clear_button.clicked.connect(self.clear)
for widget in finish_button, clear_button, cancel_button:
h_layout.addWidget(widget)
self.vertical_layout.addWidget(self.label)
self.vertical_layout.addLayout(h_layout)
self.setLayout(self.vertical_layout)
def assign(self, to_assign: bool):
if to_assign:
self.closed.emit(self, self.index, self.parse_input())
else:
self.closed.emit(self, self.index, [])
self.close()
def clear(self):
self.pressed_keys.clear()
self.deploy_keys()
def parse_input(self) -> List[str]:
without_quot = [x.replace("'", "").lower() for x in self.pressed_keys]
without_key_dot = [x.replace("key.", "") for x in without_quot]
return without_key_dot
def deploy_keys(self):
self.label.setText("+".join(self.pressed_keys))
self.label.adjustSize()
def press_keys(self, key: keyboard.KeyCode):
if str(key) not in self.pressed_keys:
self.pressed_keys.append(str(key))
self.deploy_keys()
else:
self.pressed_keys.remove(str(key))
self.deploy_keys()
def closeEvent(self, event) -> None:
self.stop()
def stop(self):
self.listener.stop()