-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (32 loc) · 1000 Bytes
/
Copy pathmain.py
File metadata and controls
45 lines (32 loc) · 1000 Bytes
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
from pynput import keyboard
from datetime import datetime
from time import time
from log_analyzer import analyze
LOG_FILE = "logs/keystrokes.log"
def start_session():
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(LOG_FILE, "a", encoding="utf-8") as f:
f.write(f"\n--- Session started {timestamp} ---\n")
def format_key(key):
try:
return key.char
except AttributeError:
return f"[ {key.name} ]"
def log(key, log_file):
k_text = format_key(key)
timestamp = time()
log_file.write(f"{timestamp:.3f} {k_text}"+"\n")
def finish(key):
if key==keyboard.Key.esc:
print('\n')
return False
def main():
start_session()
with open(LOG_FILE, "a", encoding="utf-8") as log_file:
def keyPressed(key):
log(key, log_file)
with keyboard.Listener(on_press=keyPressed, on_release=finish) as listener:
listener.join()
analyze()
if __name__ == '__main__':
main()