Hello everybody and welcome back to the third part of the Q blogger tutorial making right now.
What we want to actually do is you might have noticed that once we actually run our key logger to the
pipe and we type something in it actually prints out as we type.
Now for example if we do not want to get these printed out as we type we actually want to either print
this every 20 seconds 30 seconds or we actually want to write this to a file at any certain period of
time.
So first of all let us see how we can print this every five seconds as well.
So let us now know our key logger to that pi.
Now let us set a function that will actually print this every 10 seconds for example so we type in our
keyboard and every 10 seconds in our terminal we will get printed out everything that we have typed
in our keyboard.
So in order to do that let us make a function called for example the fine print we do not specify anything
in that function since we actually don't need to.
And once we want to use is the global variable that we use so global variable keys which is this variable
right here and all we want to do right here is print he's loops keys.
Now basically what this will do is if we call it it will basically perform the same as the in the previous
tutorial.
Now in order for us to actually make it run every 10 seconds we need to import the library which we
use for the first time or now which is called threading.
So just go up here and below your PI and put keyboard import threading.
Now basically we're threading allows us is in this case to use a function called timer which will actually
perform the function to specify in the brackets every certain period of time that we also specify in
the brackets.
So let us see what I actually mean.
So we print the keys we print our current state of the keys we then want to set the keys to be nothing.
Since we do know we do not want to print every time everything that we type.
For example if we type Kelo world in the first 10 seconds we do not want to type this.
We do not want to get the word prompted in the second to 10 seconds as well.
Now I will show you what I mean in a second but before we do that let's set a variable called timer
on which we will use to actually use our threading library and actually use our print function in order
to make it run every 10 seconds.
So timer equals and now we use this syntax right here so threading that timer and then here in the brackets
we need to specify two things which function we want to use and at how much period of time we want to
run it.
So we want to run every 10 seconds then press comma and then we which function we want to use we want
to use the print function.
So basically what this means is as I said it will run the print function every 10 seconds and while
the print function does is it just brings the keys.
So all we have to do right now is type your timer not start.
And we also in order for this all of this to work what we call that right here we want to actually call
the print function somewhere.
Now we will call the print function in our would keep our listener and write after it.
We want to call a print function right here what this will do is to call the print function.
It will go right here.
Then this will start the timer which is trending which allows us basically this threading right here
in this library that here allows us to run the action while the rest of the code is running itself.
So what this does it creates a separate thread for this function which will run every 10 seconds and
it allows us that at the same time our keyboard listener will run and listen for the input keystrokes.
So all we have to do right now is actually delete the print key since we do not need it here anymore
we created a function that will actually print the keys and let us save this.
Now let's see how this actually works.
If we run the key logger to dot you wipe we can get the invalid syntax so let us see what actually invalidates
right here where we actually specify something wrong.
Print keys global keys
let's see what actually is wrong here.
Can't really seem to see it.
Print log.
So we use the global keys function the global keys variable okay.
We print the keys then we set them to be nothing and then we call our function.
Import threading so this all should be good.
Let me just check it out.
It says invalid syntax in this function right here.
So something right here is not coded properly.
This global case is good
maybe the invalid syntax is because we actually use the name print for our function which is already
something that is in Python so let's change this to Freeport.
We also use the report right here.
We also need to call the report here or actually write here we actually want to use the print.
Yeah that was actually the problem since here in this statement right here the the code didn't know
if we actually call the function or use the print inbuilt you know that to print all the keys.
That's why we got the incorrect syntax so make sure to rename your function to anything else you want
except product the print option set for the print syntax.
Make sure to rename it here as well and also make sure to rename it here as well.
So let us go down here the letters and type your report then controller to save this and let us see
how this will run right now as we can see we don't see anything if I just type you.
Hello world we can see right after five seconds which changed from ten to five.
We actually get printed out hello world and now in the next five seconds we will not get printed out
the a world since we said the keys variable to be nothing right after we print it right.
Just type your what is up question mark in the five seconds we will get what is up.
Key shift and then the question mark.
So our code works well let us know it.
And let us add a few more things to it.
So what we manage to do is actually print every five seconds our our keystrokes.
And right now what we want to do is actually implement some of the keys that we also need to use right
here so this statement right here will become the L Steve statement so l If l if for example or let's
leave this to be l so for any other key and we will code the L if right here so right after this L if
key so are key equals equals two key.
Enter for example.
Since entering something that people press a lot we want to add nothing to the fire to the two the keys
actually so what we want to actually do is type here.
Keys equals keys.
Plus nothing.
So same thing that we did in the previous video I believe we specify something like that right now.
Keys equal keys plus nothing.
We don't specify any space right here we just leave it like this then we run the another L statement
for for example key equals key dot right.
Which is for the right arrow on our keyboard.
So what we want to do right now we want to add key keys.
That's the name of our variable so don't confuse this.
I should have named it something else since Keys and Key could be confusing but it doesn't really matter
right now.
Make sure to recognize that this key is something that we get from our function and that keys is the
variable that we actually created.
So keys equals.
Same as before so keys plus nothing.
We want to do that for all of the arrows so else if he equals equals keep the left keys equal keys nothing
else.
If key equals equals key dug up keys equal keys plus nothing at all we have to do is for the down arrow
so key equals equals keyed up down keys equal.
He's lost nothing.
So what is specified right here is if someone presses the arrows this pace the enter basically it will
not print those characters so now all we have to do right now is actually test for this.
If it works so let us front the program and let us use some of the arrows right here.
Let's use helo.
You can see that we only get printed out helo.
We don't can we don't get printed now tentatively don't get printed out.
Upper the lower left and right arrow so we only get printed out the string.
We type so let us use.
Hello world we can see we get printed out it with me mistyped right here but doesn't matter if we could
print it out.
Hello world.
Now let's see now that we created this.
Let's see in the next video how we can actually write this to a file and implement it finally in our
back door.
So I hope you enjoyed this video and I hope I see you in the next one by.