for more videos like this feel free to
check out my website tech with Tim net
and subscribe to my channel tech with
them 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
installed 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
actions on press and on release so we're
gonna do is we're going to 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 stuff so
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
gonna 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 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 gonna do with open log
dot txt it in a penned 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 four 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 implement 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 counts
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 is gonna be equal to
blank 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 log
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 gonna 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 remove the quotation
marks so if you remember int and here we
had something that looked like actually
what single quotation marks my bad like
three like if you hit that that key
would come up and go Taoiseach mark 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 we're 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 kay
don't find this is kind of the cheap way
to do it but we're just going to do this
dot spate a 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 space bar it does something 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 if it's 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 it 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
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 file so we'll say F dot 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 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 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 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 and I'll
see you again in another tutorial
[Music]