-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
103 lines (69 loc) · 2.55 KB
/
test.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from tkinter import *
from tkinter import ttk
from cgitb import text
from typing import Callable
import keyboard as kb
class OrderedSet:
"""
A set that remembers the order items were added.
"""
def __init__(self):
self.data = {}
def add(self, item):
self.data[item] = None
def remove(self, item):
del self.data[item]
def toList(self):
return list(self.data.keys())
def __len__(self):
return len(self.data)
def __contains__(self, item):
return item in self.data
# def __iter__(self):
# return iter(self.data)
class Key:
def __init__(self, scanCode, name):
self.scanCode = scanCode
self.name = name
def __hash__(self):
return self.scanCode
def __eq__(self, other):
return self.scanCode == other.scanCode
def listenForKeyCombination(callback: Callable) -> Callable:
'''
callback: a function that will be called when the key combination changes. The callback won't be called when a key is released, only when pressed. This callback will be passed a string representing the pressed keys. for example: 'ctrl+shift+a'
Returns a function that can be called to stop listening for key presses.
'''
currentlyPressedKeys = OrderedSet()
savedPressedKeysString = None
def handleEvent(event):
nonlocal savedPressedKeysString
scanCode = event.scan_code
eventType = event.event_type
keyName = event.name.lower()
key = Key(scanCode, keyName)
if eventType == 'down':
if key in currentlyPressedKeys:
return
currentlyPressedKeys.add(key)
pressedKeysNames = [key.name for key in currentlyPressedKeys.toList()]
savedPressedKeysString = '+'.join(pressedKeysNames)
callback(savedPressedKeysString)
elif eventType == 'up':
currentlyPressedKeys.remove(key)
if len(currentlyPressedKeys) == 0:
savedPressedKeysString = None
kb.hook(handleEvent)
return lambda: kb.unhook(handleEvent)
# stop = listenForKeyCombination(lambda x: print(f'boom {x}'))
# kb.wait('esc')
# @@@@@@@@@@@@@@@@
window = Tk()
window.title("Welcome to TutorialsPoint")
window.geometry('400x400')
window.configure(background="grey")
a = Label(window, text="First Name")
a.grid(row=0, column=0)
listenForKeyCombination(lambda x: a.configure(text=x))
window.mainloop()
# @@@@@@@@@@@@@@@@