Placeholder Image

字幕列表 影片播放

  • For more videos like this, feel free to check out my website tech with tim dot net and subscribe to my channel Tech.

  • With in this video, I'm gonna 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 gonna be using for this is pie input.

  • You can see right here.

  • This is what it's called.

  • It is 1/3 party module not included with the default installation of python, Which means our first step in this video is gonna be installing pie input.

  • So what we need to do is obviously we have to go to our command prompt because risking use pip to do this pretty straightforward.

  • Bring up your command, prompt simply type pip in Seoul and then pi n put.

  • Okay, So just forget the either wait for that to run.

  • And once I go so you should have pie and put insult.

  • For some reason, your pip is not working reinstalled python.

  • And when you reinstall python, make sure you add check the box that says add python to path and install pip Okay, and that should fix your issue.

  • If you have any other problems, they've calmed 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.

  • Since we need to import pie input and then from pi import keyboard, we're gonna import key, and we're gonna input listener now, listeners, what's gonna listen for our key events?

  • So let's start by writing that.

  • So we need to do is we're just going to say, with on listener, we're gonna have some brackets like this and we're gonna say on underscore press equals on underscored press.

  • Just fall 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 he's gonna be the functions that are called when a key is pressed and when a key is released.

  • Feel for you to change these names if you'd like to.

  • OK, we're just going to say as now listener, except this is gonna be lower case and then in this loop, or simply do listening dot Join.

  • Okay, and what this is gonna do is just constantly keep running this loop until we break out of it and you'll see that a second.

  • So now we need to create two functions on pressing on release.

  • So we're gonna do is going to define on press.

  • This is gonna take a key, and we'll just pass in here and then we go 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 stuff.

  • So it's gonna print, and then this is a fancy way to print this key out.

  • You don't do it this way if you don't want to.

  • But we're just gonna dot form at this with a key so that it throws it in there like that.

  • Okay, so this is just simply gonna put key in our strength.

  • 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 seconds.

  • So we're going to say if key goes, eagle is key dot and then yes, C which stands for escape 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 J s J S s.

  • And then it just tells me exactly what keys I hit after hit like the shift key or the caps lock key.

  • It will tell me all of those as well.

  • Okay, so now that we have that, that's great.

  • But we want to actually do something meaningful with this right now.

  • It just giving us like a bunch of letters and these keys like this.

  • We want to save this into probably a tax filers, 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 going to implement a few variables here and explain what they do in just a second.

  • So I'm going to start by saying account equal zero keys equals a blank list and then inside of on press, say, global keys and count.

  • And I'm also gonna create another function here, and this one is gonna be called right underscore file on What this is gonna do is exactly what it says.

  • It's going to write to a file.

  • So what we're gonna say advancing with open and then a text file name Whatever you want in this case, minds of long dirty X t, 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 fellow created and you're too lazy to make one cold for whatever you wanna call it, just put this as W.

  • Because what w does is 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 going to do with open longer t X t in upend mode.

  • that's what that stands for as f and sorry.

  • This needs to take keys and all.

  • We're gonna do anything here because we're gonna look through all the keys and we're just gonna write them into the file.

  • So for key in keys, and then we'll simply f writes 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 users, somehow able, is able to, ah, 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 restoring all their keys in this key list.

  • And then all of a sudden they quit the program somehow without hitting the escapee.

  • None of that is going to go into the text flow.

  • So what this count variables gonna allow us to do when I implemented in just 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.

  • 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 thought depend that key.

  • We're gonna add one to count.

  • So count Plus equals countless eagles, one like that.

  • And we can continue to prentiss if you want, Although it's not gonna be necessary because everything is gonna go in our log dot t X T foe.

  • Now, what else gonna do in fears?

  • I'm just gonna throw if statement and I say if couch is greater than or equal to and you can pick what number you want to do here, I'm just gonna pick 10.

  • This means every 10 keys is how often we're gonna update the file.

  • Now.

  • Obviously, if you think this could be running for a long time Yeah, I'm gonna make this longer or larger.

  • Ah, 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 account's zero.

  • We're going to write to our file passing it Archies list, which is up here, and it's gonna have stored all these keys, and they're just gonna reset Keys and keys get equal 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.

  • Start typing.

  • Hello.

  • My name is Oh, and we get 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 strings.

  • So just throw a string in there, and then it should work.

  • Okay, let's try this again.

  • Okay.

  • And would escape.

  • And now we're gonna go in long dot t x t.

  • Okay.

  • And here we go.

  • So that was from previous runs.

  • And now look at this.

  • Well, it did what we wanted it to dio it printed all of 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 what?

  • 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.

  • 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 looking through keys, I'm gonna say K is equal to str key an MSC dot Not removed out.

  • Replace and then quotation Calm a blank space.

  • Now, what this does is removes the quotation marks.

  • So if you remember in 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 remarks.

  • 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 into a string Replaced the quotation marks Now that we have it in a form that we can read properly where first, we'll just gotta check if this is a space character.

  • So if the user hit the space bar So the way that we could do this when you say it okay, I don't find this is kind of the cheap way to do it, but we're just gonna do this, not spate space in here equals equals where actually is greater than zero.

  • So what this find is gonna do is it's gonna look in the key, because whenever we have the the space bar, it doesn't like this.

  • Does that key dot space?

  • Okay, that's what's returned as key.

  • So we're gonna look for space in our string.

  • We're gonna find that if it occurs more than our 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 had a new line and follows just backslash and this just the notes like Go to the next line.

  • Okay?

  • All right.

  • After this, we're also gonna check, so if it's space, that's fine.

  • But if it is any of the other keys like command shift back space, 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 where I say it's a l f k dot find, and then key equals equals negative one.

  • Now, what happens in find is if it doesn't find this 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 Q w like any of the letters of the number keys, Then we will simply write that into the phone.

  • So we'll say f dot right, And then whatever.

  • Kay waas.

  • So that key okay?

  • And I will just show you this working now and then explain it really quickly.

  • So if we run this typing Hello, my name is Donald backspace.

  • A few times space Add some stuff, okay and get rid of all that and close the file or close the program story.

  • You can see we get why Hello.

  • My name is Space base Base, huh?

  • Space race, various.

  • Hello.

  • And then we get a bunch more 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 and 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 make this video too long.

  • So that's all I'm gonna show you today if this helped you.

  • Oh, please make sure you leave a like and subscribe, and I will see you again.

For more videos like this, feel free to check out my website tech with tim dot net and subscribe to my channel Tech.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

用Python創建一個鍵盤記錄器 - 教程 (Create a Keylogger with Python - Tutorial)

  • 6 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字