Welcome back everybody.
Today we're going to go ahead and continue making our simple and primitive keylogger.
Now thus far, we have explored the option with "GetAsyncKeyState"
That is the function that we will be using for this section anyway.
but if there is enough interest and if enough people wanted
we will also make one with a hook, which is a more sophisticated version of a key logger
which is basically not scanning the keyboard input non-stop
rather instead it's based on hooks
and it's event driven.
That is an advanced subject
for which we first need to understand some of the basics that we see here.
So if this works out well, then we will move into that most likely.
Anyway, before we actually get into today's tutorial
let me post a reminder, so to say, in regard to discussion section
that I do try to answer all of the inquiries.
There are about 50 of them per day.
Please if I have missed yours...
I haven't answered yours in, let's say 24 hours or something of kind
please do not hesitate to send me a private message
just send a link and I will go directly to that discussion
as soon as I see it and try to do my best to help you out.
What you should not do definitely
is just send the pm stating that I didn't answer the discussion
because there are a lot of them and I won't be able to find them just from your name.
That will be a little bit difficult for me.
So just post the link there
It's not that hard
You just open up your discussion and copy the URL.
That is literally it
and I will attend to it.
Anyway, let's go ahead and jump into today's lesson.
Today we will be doing a little bit of filtering with the tab, caps lock, shift, alt keys, arrow keys
and so on and so forth
Mouse keys as well and one
and importantly than that, I would like to introduce you today to lowercase and uppercase letters
This will be very important for us
as we do need to differentiate between the two.
How do we differentiate between the two?
Well, we use at the state of the shift key
and we will also be able to use the state of the caps lock key so
So if either of these two keys is pressed, then please write capital letters.
and that's what we want to tell the program
but if they're not pressed, then please do not write in capital letters
Then write lowercase letters.
Now by default, it's going to write to uppercase letters.
So we just need to define the states for lowercase letters.
That is pretty much it.
First off, we have a key here
Well, not the key. Sorry
I have modified the code somewhat
Don't immediately grab your heads and say "Oh no!"
I will explain. No worries.
So the things that I have changed is the name of the character in which we place our keystrokes
So I've just named it "key" for the sake of convenience
I have implemented this sleep function, but I have commented it out for the time being.
We will use it to prevent the maxing out of CPU usage.
and, well let me put it to you this way
"sleep" will pause the program for the specified amount of milliseconds
Now you might find it strange that I had used 0 here
Okay, so 1 2 3 4 5 etc. will tell the program to sleep for X amount of milliseconds
and 0 will simply tell it okay
Whenever another program wants to use the CPU
you will relinquish control.
Kind of convenient if you don't want it interfering with other processes.
If you don't want it slowing the computer down
but this is still a patchwork, so to say
not a real solution.
What else have I changed?
I have commented out the shift statement from my test runs
and you can see this is from my test runs
I apparently like letters R and D
and I've commented it out, so we will most likely be using it further on
but for the time being, I don't want to use it.
I wanted to show you how you can differentiate between uppercase and lowercase letters.
So you have an "if" statement here
and let's make this prettier
because we like our code pretty and shiny and so on.
Anyway, we have an "if" statement
and basically says if the value of key is greater than 64
and if the value of key is lesser than 91
and if this function returns true with the shift key pressed
this is a hexadec notation for the shift key
I'll show you where you can get those in a moment
So if the shift key is pressed
This is basically the function that determines that
Sorry, not if it IS pressed
State no. Not true.
I've shown you this before.
So, if this function says that the shift key is pressed
and if we put the not sign in front of it
we can say basically "Okay if shift key is not pressed, please add 32 to these values"
Okay, so this might not be the best of explanations that I have given you
and it might not be the clearest of explanations, but let me rectify that really quick.
Let us have a look at our ASCII table
and there we go.
So the search engine is kind enough to provide it for us without have to adhere to any websites.
So what have we stated?
64 and 91
So let's find the decimal values of 64 and 91
There we go
So it says greater than 64, not equal to 64
therefore 64 will not be taken into consideration and it will immediately go into 65
and we will go to 91, which is here
91 is also not take into consideration because it says less than 91
and that's the letter Z
so 91 is not taking into consideration.
The condition states that if the user has pressed any of the letters on the keyboard
and we notice by checking their ASCII values.
Please check the status of the shift key.
So if the shift key is not pressed
then obviously it should be a lowercase letter, unless caps lock is pressed as well
but we will deal with that in a moment
then it should be a lowercase letter
and please add the value of 32
What is the value of 32 do?
Well, take a look at this --
How much is 65 + 32? It's 97
So let's take a look at the value of 97
97 is lowercase
Hey, guess what? It's the lowercase of this one
and of course, if you add 32 to 66, it's just going to be 98
Yep, there we go.
So B is right underneath it, and so on and so forth
So you just keep adding 32 to these numbers, in order to convert them into lowercase.
Simple as that. No big deal.
You keep checking with the status of the shift key
and you either leaving them as they are or converting them to lowercase letters.
Very convenient
So let's go back into those
and keep in mind that this statement that we have placed in here now
will only record letters and nothing else.
So we will need to rectify this in the foreseeable future.
Basically let's go through the statement one more time.
So check whether it's a letter within it, because letters are within this ASCII range.
It says the if key is greater than 64 and is lesser than 91
So this condition has to be met.
This whole condition
and I'm probably better off, just for the sake of safety
I mean, I don't need to, but you know
to prevent errors later on.
Not a bad idea to do this
to enclose it in a separate casing
and then there's another condition.
Check if the shift key is not pressed.
If it is not pressed, then please convert the letters to lowercase.
So it says key + ...
You know, I'm pretty sure that you know this by now what this means.
This statement is an equivalent of key equals key + 32
and then we're going write that to a file
and then we're going to close the file
and during these test runs, I've simply written break here
so don't check for anything else.
We might remove that later on, but we'll see how it works out
and down below it says
Okay, if the value is within this range
and if you obviously have not stated that this is true above it
then I know that shift is not pressed, so there is no need to check for anything else.
Just go ahead and write it to a file.
So let's go ahead and compile and see how this works.
Okay, so CTRL F11, if you remember
Press ENTER
It's working, it's working
There we go
So let's go ahead and open up our terminal and type in "heydoyouworkornot"
Remember I am typing this all together
because I didn't tell the system how to handle a space or anything else
and I don't want any confusion now, but we will implement that shortly
and there you go.
It says "HEYDOYOUWORKornot"
So you have uppercase and lowercase letters that are being recognized without any difficulties of whatsoever
So let's just go ahead and stop the program.
Go back and cleanse
Clean this file
You don't have to clean it but I like to clean it
because it's easier for you when you take a look at the new file
so you see what the input is
so that you can make sure that it is the expected input
that it all works.
So this would be way to implement the differentiation between lowercase and uppercase letters.
There are a hundred different ways to do this.
Some of them are probably better than this one.
Feel free to experiment.
If you do experiment, and whether it works out or not
I would appreciate it if you just shared with everybody else what you did
in such a way we help to further knowledge
and to help each other out.
If not, it doesn't matter
but I still encourage you to do some test runs, experiment with it
and so on and so forth.
So I hope that you can figure it out from this file what I have written.
I have explained every step
but if you fail to, there are always discussions
I'm always there, so just come and ask.
You have a 99.9% chance that I will answer in a reasonable amount of time.
Anyway, I bid you all farewell
until the follow-up tutorial
where we shall deal further with filtering our input.