Python Keylogger Tutorial - Windows

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video tutorial demonstrates how to create a Python keylogger for Windows using the pynput module, which allows users to record and store key presses in a text file. The tutorial covers the installation of the pynput module, importing necessary modules, defining functions to handle key presses, and storing the recorded keys in a text file.

Full Transcript

in this video I'm going to be showing you how to log user key presses and save those key presses in a meaningful way in a text file now the module that we're going to be using for this is PI input you can see right here this is what it's called it is a third party module not included with the default installation of Python which means our first step in this video is going to be installing PI input so what we need to do is obviously we have to go to our command prompt because rich can use pip to do this pretty straightforward bring up your command prompt simply type pip install and then PI and input okay so just forget the I there wait for that to run and once I goes through you should have pi input insult for some reason your pip is not working reinstall Python and when you reinstall python make sure you add or check the box that says add python to path and install pip okay and that should fix your issue if you're having any other problems leave a comment down below okay so we're first what we need to do is we're gonna set up a we need to actually bring these imports in so we need to import PI input and then from PI input keyboard we're gonna import key and we're gonna input listener now listener is what's gonna listen for our key events so let's start by writing that so what we need to do is we're just going to say with and then listener then we're gonna have some brackets like this and we're gonna say on underscore press equals on underscore press just follow along with this for one second and then on underscore release is gonna equal on underscore release like that now these on press and on release are what we're gonna code in just a second and these are gonna be the functions that are called when a key is pressed and when a key is released feel free to change these names if you'd like to okay and we're just gonna say as now listener except this is gonna be lower case and then in this loop we're simply gonna do listener dot join okay and what this is going to do is just constantly keep running this loop until we break out of it and you'll see that in a second so now I need to create two functions on press and on release so we're gonna do is we're gonna say define on press this is going to take a key and it will just pass in here and then we're going to define on release same thing in here a key and we will pass now actually what we're gonna do on on press is we're just gonna print out the key just so we can see this working first of all and then we'll move into some other stuffs we're just gonna print and then this is a fancy way to print this key out you don't have to do it this way if you don't want to but we're just gonna dot format this with key so that it throws it in there like that okay so this is just simply gonna put key in our string okay so on release we're gonna add one bit of functionality just to make sure that we're able to break out of this loop and you will see that in one second so we're gonna say if key equals equals key dot and then ESC which stands for escape we're going to return false and all this is gonna do is break out of this loop if we hit the Escape key so now let's test our program and see if everything is working okay so we can see that we get the window up here and if I start typing some stuff you can see it says jsj SS and then it just tells me exactly what keys I hit now if I hit like the shift key or the caps lock key it'll tell me all those as well okay so now that we have that that's great but we want to actually do something meaningful with this so right now it's just giving us like a bunch of letters and these keys like this we want to save this into probably a text file or something that we're gonna be able to look at because most likely if you're creating a key logger you probably want to do something with the keys that are being pressed so what I'm gonna do is I'm gonna implement a few variables here and explain what they do in just a second so I'm just gonna start by saying count equals zero Keys equals a blank list and then inside of on press I'm going to say global keys and count now I'm also gonna create another function here and this one is gonna be called write underscore file and what this is gonna do is exactly what it says it's going to write to a file so what we're gonna say is going to say with open and then a text file name whatever you want in this case mines log txt and then we're gonna put a mode here okay now if the first time you're running this you don't have a text file created and you're too lazy to make one cold for whatever you want to call it just put this as W because what W it means right and if that file does not exist it's simply going to create one so the first time you run it W works fine but after that you have to use a okay we're gonna do with open log dot txt it in append mode that's what that stands for as F and sorry this needs to take keys and all we're gonna do in here is we're gonna loop through all the keys and we're just gonna write them into the file so for key and keys and then we'll simply do F right key like that and this is going to write all of our keys into the file for us now you might ask well okay what's the point of this keys list and this count variable well the thing is if our user somehow able is able to break the program or like quit out of the program we want to make sure that we're not just writing this at the end because say for example the users on the computer for like an hour and they're typing stuff and we're storing all their keys in this key list and then all of a sudden they quit the program somehow without hitting the Escape key none of that is gonna be written to the text file so what this count variable is gonna allow us to do when I implemented it in just a second is every so many keys we're going to update the text file so this way we're not constantly rewriting it every second but after the user hits a certain amount of keys then we're going to load that or write all that data into the text file so I guess I should probably add to our keys list if we're gonna be using that so whenever the user hits a key we're just gonna do keys dot append that key we're gonna add one to count so count plus equals count plus equals one like that and we can continue to print this if you want although it's not gonna be necessary because everything is gonna go in our log txt file now what I'm also going to do in fear is I'm just gonna throw a little if statement I'm gonna say if count is greater than or equal to and you can pick what number you want to do here I'm just gonna pick 10 so this means every 10 keys is how often we're gonna update the file now obviously if you think this can be running for a long time we I'm gonna make this longer or larger you could make it one you could not have the count variable if you want do whatever you want for this okay now I say we're gonna reset our count equals zero we're going to write to our file passing it our keys list which is up here and it's gonna have stored all these keys and then we're just gonna reset keys and keys egle to blink like this okay so that is I think at least should be working let's try this out oh wow the whole time I was typing that was recording my keys okay anyways let's see here if I start typing hello my name is oh and we get an issue right argument must be a string not key code I forgot about that so pretty much in here just getting mad at us because we don't have string so just throw string in there and then it should work okay let's try this again okay and hit escape and then we're gonna go in long txt okay and here we go so that was from previous runs and now look at this well it did what we wanted it to do it printed all the things into our text file but this is not meaningful information and we probably want it to look a little bit better than this and not have quotations and not have backspace keys so I'm gonna show you how we can do that right now so let's just clear this file save that and now let's deal with right file here so we're not writing these ridiculous characters into our text file so what I want to do is I want to write each word that the user types into one line so the way I'm gonna do that is every time the user hits the space key I'm going to add a new line to the file so first of all what I want to do is every time we're looping through keys I'm going to say K is equal to STR key and I'm gonna say dot not remove replace and then quotation comma blank space now what this does is removes the quotation marks so if you remember int and here we had something that looked like actually was single quotation marks my bad like three like if you hit that that key would come up and go taste remark so this is just gonna remove that for us I don't know why it shows up in quotation marks but anyways so that's gonna turn it into a string replace the quotation marks now that we have it in a form that we can read properly or first of all just going to check if this is a space character so if the user hit the spacebar so the way that we can do this we can say if K dot find this is kind of the cheap way to do it but we're just going to do this dot Speight space in here equals equals we're actually is greater than zero so what this fine is gonna do is it's gonna look in the key because whenever we hit the spacebar it doesn't like this does like key dot space okay that's what's returned does key so we're gonna look for space in our string we're gonna find that if it occurs more than one are more than zero times so once then we're going to write a space character to our line so in this case we're actually just gonna add a new line the way we add a new line in a file is just backslash n this just a notes like go to the next line okay all right after this we're also gonna check so it could space that's fine but if it is any of the other keys like command shift backspace we don't want to write that into our file we don't want it to say like backspace or command so I'm just gonna say if and then when I say L if K dot find and then key equals equals negative one now what happens in find is if it doesn't find the string that you're looking for and I guess I better actually put a capital cakes I'm pretty sure it's capital then it returns us a negative one value so if key does not exist meaning that we hit a key like QW like any of the letters of the number keys then we will simply write that into the phone so we'll say F thought right and then whatever K was so that key okay and I will just show you this working now and then explain it really quickly so if we run this I start typing hello my name is and I'll backspace a few times space add some stuff okay and get rid of all of that and close the file or close the program sorry you can see we get why hello my name is space space space space space space hello and then we get a bunch of our spaces and that is because whenever you hit the space key right it's adding a new line so we could now check if the line before that is blank then let's not continue to do that or whatever like stuff like that and you can continue in checking all these different keys and seeing what they are but this is all I'm gonna show you for right now knowing this you have a basis for how to get keys how to store them in a text file and you guys can play around with creating a better key logger I don't to make this video too long so that's all I'm going to show you today if this helped you out please make sure you leave a like and subscribe I'll see you again in another tutorial [Music]

Original Description

How to create a python keylogger for windows. I will show how to get user keypresses and store them in a text file. This tutorial uses the pynput module which is a third party module not included with the default python installation. ************************************************************** Support the Channel: https://www.patreon.com/techwithtim Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Python Tutorials - Keylogger in python - Python keylogger tutorial - How to make a keylogger in python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches how to create a Python keylogger for Windows, allowing users to record and store key presses in a text file. The keylogger utilizes the pynput module and provides a basic understanding of key logging and text file manipulation. By following this tutorial, users can gain hands-on experience with Python programming and keylogger development.

Key Takeaways
  1. Install the pynput module using pip
  2. Import the pynput and keyboard modules
  3. Define the on_press and on_release functions
  4. Print key presses to the console
  5. Store key presses in a text file
  6. Create a text file named 'log.txt'
  7. Use 'with open' in append mode to add keys to the file
  8. Reset the 'keys' list and 'count' variable after updating the file
💡 The pynput module provides an easy-to-use interface for monitoring and controlling user input, making it a valuable tool for keylogger development.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →