|
| 1 | +<h1 align='center'>Keylogger with pynput</h1> |
| 2 | + |
| 3 | +This Python script uses the `pynput` library to create a simple keylogger that captures and logs all keyboard inputs. Below is an explanation of how the code works: |
| 4 | + |
| 5 | +## Importing the Necessary Libraries |
| 6 | + |
| 7 | + - `pynput`: A library that allows you to control and monitor input devices. |
| 8 | + - `pynput.keyboard`: Provides the `Key` and `Listener` classes to handle keyboard events. |
| 9 | + |
| 10 | +## Global Variables |
| 11 | + |
| 12 | + - `keys` : A list to store all the keys that are pressed. |
| 13 | + |
| 14 | +## Defining the Event Handlers |
| 15 | + |
| 16 | + ### `on_press` Function : |
| 17 | + |
| 18 | + - `on_press(key)`: A function that is called whenever a key is pressed. |
| 19 | + - It appends the key to the `keys` list. |
| 20 | + - Calls the `key_write(keys)` function to write the keys to a file. |
| 21 | + - Attempts to print the key character if it's an alphanumeric key; otherwise, it prints the special key. |
| 22 | + |
| 23 | + ### `key_write` Function : |
| 24 | + |
| 25 | + - `key_write(keys)`: A function that writes the logged keys to a file. |
| 26 | + - Opens (or creates) `logs.txt` in write mode. |
| 27 | + - Iterates over the `keys` list and writes each key to the file, replacing single quotes with empty strings. |
| 28 | + |
| 29 | + ### `on_release` Function : |
| 30 | + |
| 31 | + - `on_release(key)`: A function that is called whenever a key is released. |
| 32 | + - Prints the released key. |
| 33 | + - If the `Esc` key is released, it prints "Program Exit" and returns `False` to stop the listener. |
| 34 | + |
| 35 | +## Setting Up the Listener : |
| 36 | + |
| 37 | + - `Listener(on_press=on_press, on_release=on_release)`: Creates a `Listener` object that monitors keyboard events. |
| 38 | + - `on_press`: The function to call when a key is pressed. |
| 39 | + - `on_release`: The function to call when a key is released. |
| 40 | + - `listener.join()`: Starts the listener and blocks the program, keeping it running to capture keyboard events. |
| 41 | + |
| 42 | +### Summary |
| 43 | + |
| 44 | +This script is a basic keylogger that logs every key press and release to a text file `logs.txt`. It captures both alphanumeric and special keys, prints them to the console, and writes them to the file. The logging stops when the `Esc` key is pressed. |
| 45 | + |
| 46 | +## Requirements |
| 47 | + |
| 48 | +- Python 3.x |
| 49 | +- `pynput` library |
| 50 | + |
| 51 | +You can install the `pynput` library using pip: |
| 52 | + |
| 53 | +``` |
| 54 | + pip install pynput |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +1. Clone the repository or download the script file. |
| 60 | +2. Ensure you have Python 3.x installed on your machine. |
| 61 | +3. Install the `pynput` library if you haven't already (see Requirements section). |
| 62 | +4. Run the script: |
| 63 | + |
| 64 | + ``` |
| 65 | + python keylogger.py |
| 66 | + ``` |
| 67 | +
|
| 68 | +6. The script will start logging all key presses and releases. The log will be saved in a file named `logs.txt`. |
| 69 | +7. To stop the keylogger, press the `Esc` key. |
0 commit comments