Hi and welcome to a series of lectures on how to control the muscles and the keyboards via a Python
script so you can move the mouse, you can do clicks and copy text.
Drag the mouse and you can also type in text via Python script.
Move the mouse and type in text in program, which is currently open on the screen.
We're going to do all that using a third party library, which is very popular for these kinds of automations,
and it's called PI, also gooey.
So the first thing we want to do is open the terminal and do pipe install p y all to one word.
That's should to try out a movie, and I'm going to create a Python script where I'll show you some.
Over the most used features on how to control the mouse with Python.
So I'm going to name this mouse, that's why I'm literally going to practice what you learn in these
lectures.
We're going to practice with a series of practical programs.
So now let me take you through.
Pie, ultimately for controlling the malls, so imports p y all to agree.
And now we're going to write a series of instructions which will control the cursor on the screen.
We're going to write some codes, which gives us the current position of the cursor in pixel coordinates.
So depending on the screen resolution, where the pixel, where the cursor is and then we're going to
move the cursor to a specific position.
So if we want to move it to this file, we're going to move it to that file.
And then I'll show you how to click that file, how to double click it as well.
Also, how to perform a right click so that you see the context menu here.
And I'll show you how to drag the cursor, which could be useful if you wanted to throw something on
a canvas on a painting app via Python.
So let's start, let's get the position.
The current position would be p y all to go.
That's position, and basically every other methods is like this.
You just access the methods directly from p y el to green and just print out the position and you see
where this is on right clicking here and run my file.
And that's the position of the moment of the script execution.
So, for example, if I have my cursor somewhere near the left.
All-Pro corner in here and now I'm going to use the keyboard shortcut to run my code so that I don't
move the cursor to the round bottom, so I'm going to use in my case, you see, that's my shortcut
is control or to exit my my python script.
So I'm going to place the cursor somewhere here and press control or to execute Python.
And so these are the positions.
X is thirty five and Y is thirty six.
Let's go a little bit down somewhere in here executes the script again.
So X is fifty five, Y is two to seven.
And so the position changes with the cursor.
You see, that's 55 minutes from the vertical sites in here, up to here and then 227 from here to here.
So those are the coordinates.
Now why do we need these coordinates?
Well, if you want to click over a particular position now, for example, let me create a text file.
Simple text and just write.
Some pears are good.
Some dummy text in here.
Now, if you want python to go to this file, we want to know the position of the cursor first and then
we want to apply the next method of P y all to gooey.
So we'll try to do is I'll learn all new knowledge.
The position of symbol that C by running the scripts with control are.
So 139 hundreds and now I can use.
The P y ought to agree that the move.
Two methods to move the mouse to a pair of absolutes coordinates.
So these are absolute coordinates, the absolute origin is that this corner here?
So we want to move the cursor to a hundred thirty nine.
280, so it doesn't matter now where my current position of the cursor is.
I can just run this script and Python will move the cursor to sample the text.
You see now the cursor is here.
You can make this process more natural by adding a duration attachment and set it to a number of seconds.
For example, one second and see what's going to happen now.
So I look around the script and Python will drag.
The mouse will move the mouse to vet's position during the length of one second.
So the transition was smoother this time.
If you want to click that file, you apply.
But clearly, comfort, just like that's sort of run the script as it is, Python will move the cursor
to that file and it will click.
It now notes that this was not a double click.
If you want to perform a double click, then you could do clicks equal to two.
So we want to do two clicks.
And the file is open here in the editor.
Alternatively, you can also use PUOi all to with the double click, and you don't have to write that
clicks eagles to to that.
So that will do the same thing.
I can demonstrate that.
Double and the file opens.
So let's keep that one.
If you want to click over a particular position, we can do that by using coordinates so people to get
that click and then we provide the coordinates, for example, 139 280.
So in that case, we don't have to move the models to a position first.
All you have to do is just provide the coordinates to the click method and the click will happen.
So when you don't provide coordinates, Python will click the current position.
So it's clicked the current position, which was this one in here.
So the move to method move the cursor to the current position of that position and the click methods,
click the vets current position.
But if you want to just click there and you see clicks equal to the same thing will happen, of course.
So the sample data file is opened.
And of course, you didn't see any movement of the mouse because we don't have obviously a move to method
now.
It's commented, No, you know, that's a mouse as a left button and a right button.
It also has a middle button insertion.
Morsi's answer?
You need the right click a button if you want to see the context menu.
Now you can perform a right click by doing P y all target.
That's click Give the position where you want to click again.
And then specify what button do you want to press?
So by default?
Of a clique, methods presses the left button, so when you press the left button, all your double
press, it's then you are doing a common click with a red button.
You will want to specify right here if you want to change the default behavior of the click method.
So if you run this script now.
And so nothing happened.
Because I am on a Mac computer here, if you are on Windows, these probably worked.
On Mac, you have to change this slightly.
So this was about right.
Click on Windows on Mac.
You have to write P one, all too gooey and then use the track two methods.
Honestly, I'm not sure why the Drag two method works in this case.
And if this is unexpected behavior of Toyota gooey or just the bug, but it works so.
But it works.
So again, the coordinates two hundred eighty.
And then the buttons should be rights also for Track two and also also you want to first do a normal
click.
In those coordinates.
So a left click and then do the track to methods.
So this is only for Mac users.
If I run this script now.
You'll see the context menu showing of.
This one in here.
So that's something to keep in mind.
Depending on your operating system, you can also move your cursor relatively to the current position.
So let me call on these old maybe advocates in here.
So why ultimately move is the new method so we used to move to and then we can use move.
So let's move the cursor to the absolute position first of this position and then move it 100 pixels
to the right and perhaps zero vertically.
And adds integration.
All for one and execute.
So, yeah, as you see, the cursor went to the absolute position first, and then it went right.
So that could be handy in certain occasions when you want to do something depending on the previous
position of the cursor and the lost methods I want to show you is the drag method, and we're going
to draw something using that method on a painting app for your convenience, also suggested to open
your browser and go to just paint that app.
That's an app where you can draw simple figures.
So I have opened my browser here on the sides, so I suggest you to keep your I.D. on one side and the
browser on the other side.
So that's James paint that app.
You can expand this drawing area if you want.
And then you can just drag your cursor like that so you press its you hold it and you drag it and you
normally will draw something.
Now how can we perform the same behavior through our Python script?
Well, for that, we're going to use the drag method.
But first, let's find out the starting position of the cursor.
So let's say we want to draw a line starting from here up to here.
So what is the position in this point?
Well, now without moving my cursor I want to use on a Mac is the context switcher where I press command
on top and I go to point charm without moving the cursor and then I press control or to execute the
script on windows, you should press the Windows key.
As far as I remember and the tab to switch between applications without using your models of use, control
or whatever button combination you have to run your script.
So I forgot to print out of the positions, so let me print the position here and maybe comment this
old.
I just want to know the position of the cursor at this point.
So I'm going to repeat the process again, go to point charm without using the mouse control or to run
the script position.
And then I want to repeat the process.
So take the cursor in here and go to charm using the context switch without using your cursor, your
mouse and van control or to run the script, and that will give me the position of the cursor.
So now I want to say p y all target, don't move two.
So we first take the cursor to that place.
Twenty six.
And five, six, seven.
Those are the coordinates, in my case.
Let's set a duration of one and then you want to.
You want to click there.
You know, you could also avoid the move to methods and just put those coordinates directly to the click.
Methods, if you don't want to see the movement of the cursor, it's up to you.
And then finally, we use the track methods, so we want to see, for example, one hundred fifty zero.
Duration is while second bottom is left.
If this doesn't work, try with the right bottom.
So sometimes it depends on the operating system, but I'm going to execute this now because it goes
there and the line is thrown by Python.
And just like that, you can duplicate that line and say zero here, minus 150.
You know, the line is drawn again.
Go up.
And then maybe adds another line minus a hundred.
Zero.
Let's see what we will get this time.
Up to there, and maybe you want to go down or maybe change that to minus 50.
And then duplicate again and say zero four, that's.
Under 50 for that.
Oh, there we drill a square.
That's what I wanted to teach you in this.
Lecture.
So this should give you a kick starts off controlling the malls with P y o Tiggy or Pi.
Ultimately, if you like now, we're going to practice this further by writing some more real world
programs.
But in the next video, I'll show you also how to control the keyboard of your computer via Python so
that you can type in text in an area where text can be typed and then use holds keys.
So combinations of keyboard shortcuts to perform actions on your computer and so on.
So I'll talk to you in the next video to.