1
00:00:01,150 --> 00:00:08,470
Hi and welcome to a series of lectures on how to control the muscles and the keyboards via a Python

2
00:00:08,470 --> 00:00:13,760
script so you can move the mouse, you can do clicks and copy text.

3
00:00:14,260 --> 00:00:20,080
Drag the mouse and you can also type in text via Python script.

4
00:00:20,470 --> 00:00:26,890
Move the mouse and type in text in program, which is currently open on the screen.

5
00:00:29,450 --> 00:00:36,160
We're going to do all that using a third party library, which is very popular for these kinds of automations,

6
00:00:36,430 --> 00:00:39,310
and it's called PI, also gooey.

7
00:00:41,470 --> 00:00:50,320
So the first thing we want to do is open the terminal and do pipe install p y all to one word.

8
00:00:54,570 --> 00:01:01,680
That's should to try out a movie, and I'm going to create a Python script where I'll show you some.

9
00:01:03,710 --> 00:01:09,470
Over the most used features on how to control the mouse with Python.

10
00:01:10,130 --> 00:01:17,660
So I'm going to name this mouse, that's why I'm literally going to practice what you learn in these

11
00:01:17,660 --> 00:01:18,110
lectures.

12
00:01:18,230 --> 00:01:22,160
We're going to practice with a series of practical programs.

13
00:01:22,460 --> 00:01:24,410
So now let me take you through.

14
00:01:25,750 --> 00:01:32,350
Pie, ultimately for controlling the malls, so imports p y all to agree.

15
00:01:36,630 --> 00:01:44,490
And now we're going to write a series of instructions which will control the cursor on the screen.

16
00:01:45,900 --> 00:01:52,740
We're going to write some codes, which gives us the current position of the cursor in pixel coordinates.

17
00:01:53,130 --> 00:01:58,290
So depending on the screen resolution, where the pixel, where the cursor is and then we're going to

18
00:01:58,290 --> 00:02:02,010
move the cursor to a specific position.

19
00:02:02,020 --> 00:02:06,030
So if we want to move it to this file, we're going to move it to that file.

20
00:02:06,630 --> 00:02:10,229
And then I'll show you how to click that file, how to double click it as well.

21
00:02:10,800 --> 00:02:15,840
Also, how to perform a right click so that you see the context menu here.

22
00:02:17,660 --> 00:02:25,010
And I'll show you how to drag the cursor, which could be useful if you wanted to throw something on

23
00:02:25,010 --> 00:02:29,870
a canvas on a painting app via Python.

24
00:02:31,700 --> 00:02:34,790
So let's start, let's get the position.

25
00:02:36,840 --> 00:02:39,900
The current position would be p y all to go.

26
00:02:41,040 --> 00:02:45,510
That's position, and basically every other methods is like this.

27
00:02:45,720 --> 00:02:53,580
You just access the methods directly from p y el to green and just print out the position and you see

28
00:02:54,510 --> 00:02:57,890
where this is on right clicking here and run my file.

29
00:02:59,010 --> 00:03:03,540
And that's the position of the moment of the script execution.

30
00:03:03,840 --> 00:03:09,960
So, for example, if I have my cursor somewhere near the left.

31
00:03:11,510 --> 00:03:18,350
All-Pro corner in here and now I'm going to use the keyboard shortcut to run my code so that I don't

32
00:03:18,350 --> 00:03:26,330
move the cursor to the round bottom, so I'm going to use in my case, you see, that's my shortcut

33
00:03:26,330 --> 00:03:29,540
is control or to exit my my python script.

34
00:03:30,080 --> 00:03:37,160
So I'm going to place the cursor somewhere here and press control or to execute Python.

35
00:03:37,340 --> 00:03:39,290
And so these are the positions.

36
00:03:39,560 --> 00:03:43,690
X is thirty five and Y is thirty six.

37
00:03:43,700 --> 00:03:50,270
Let's go a little bit down somewhere in here executes the script again.

38
00:03:51,410 --> 00:03:55,490
So X is fifty five, Y is two to seven.

39
00:03:57,380 --> 00:03:59,990
And so the position changes with the cursor.

40
00:04:00,350 --> 00:04:09,030
You see, that's 55 minutes from the vertical sites in here, up to here and then 227 from here to here.

41
00:04:09,050 --> 00:04:10,370
So those are the coordinates.

42
00:04:10,550 --> 00:04:13,700
Now why do we need these coordinates?

43
00:04:13,970 --> 00:04:22,760
Well, if you want to click over a particular position now, for example, let me create a text file.

44
00:04:24,120 --> 00:04:26,330
Simple text and just write.

45
00:04:26,330 --> 00:04:29,060
Some pears are good.

46
00:04:29,060 --> 00:04:30,380
Some dummy text in here.

47
00:04:31,070 --> 00:04:38,690
Now, if you want python to go to this file, we want to know the position of the cursor first and then

48
00:04:38,690 --> 00:04:44,900
we want to apply the next method of P y all to gooey.

49
00:04:46,160 --> 00:04:49,290
So we'll try to do is I'll learn all new knowledge.

50
00:04:49,310 --> 00:04:54,080
The position of symbol that C by running the scripts with control are.

51
00:04:55,440 --> 00:05:00,180
So 139 hundreds and now I can use.

52
00:05:01,730 --> 00:05:05,480
The P y ought to agree that the move.

53
00:05:07,410 --> 00:05:13,650
Two methods to move the mouse to a pair of absolutes coordinates.

54
00:05:14,550 --> 00:05:19,410
So these are absolute coordinates, the absolute origin is that this corner here?

55
00:05:19,800 --> 00:05:23,400
So we want to move the cursor to a hundred thirty nine.

56
00:05:25,680 --> 00:05:31,650
280, so it doesn't matter now where my current position of the cursor is.

57
00:05:31,920 --> 00:05:37,220
I can just run this script and Python will move the cursor to sample the text.

58
00:05:37,230 --> 00:05:39,220
You see now the cursor is here.

59
00:05:39,810 --> 00:05:47,910
You can make this process more natural by adding a duration attachment and set it to a number of seconds.

60
00:05:48,120 --> 00:05:51,120
For example, one second and see what's going to happen now.

61
00:05:51,330 --> 00:05:54,690
So I look around the script and Python will drag.

62
00:05:54,690 --> 00:06:00,630
The mouse will move the mouse to vet's position during the length of one second.

63
00:06:01,800 --> 00:06:04,740
So the transition was smoother this time.

64
00:06:05,790 --> 00:06:09,150
If you want to click that file, you apply.

65
00:06:13,580 --> 00:06:20,450
But clearly, comfort, just like that's sort of run the script as it is, Python will move the cursor

66
00:06:20,450 --> 00:06:21,980
to that file and it will click.

67
00:06:21,980 --> 00:06:25,940
It now notes that this was not a double click.

68
00:06:25,940 --> 00:06:32,750
If you want to perform a double click, then you could do clicks equal to two.

69
00:06:33,590 --> 00:06:34,810
So we want to do two clicks.

70
00:06:36,480 --> 00:06:39,690
And the file is open here in the editor.

71
00:06:40,800 --> 00:06:48,900
Alternatively, you can also use PUOi all to with the double click, and you don't have to write that

72
00:06:49,320 --> 00:06:50,600
clicks eagles to to that.

73
00:06:50,610 --> 00:06:52,170
So that will do the same thing.

74
00:06:52,680 --> 00:06:53,970
I can demonstrate that.

75
00:06:55,100 --> 00:06:56,870
Double and the file opens.

76
00:06:59,490 --> 00:07:00,780
So let's keep that one.

77
00:07:02,280 --> 00:07:11,720
If you want to click over a particular position, we can do that by using coordinates so people to get

78
00:07:11,730 --> 00:07:17,550
that click and then we provide the coordinates, for example, 139 280.

79
00:07:17,760 --> 00:07:23,340
So in that case, we don't have to move the models to a position first.

80
00:07:24,210 --> 00:07:29,430
All you have to do is just provide the coordinates to the click method and the click will happen.

81
00:07:29,760 --> 00:07:34,860
So when you don't provide coordinates, Python will click the current position.

82
00:07:34,860 --> 00:07:38,610
So it's clicked the current position, which was this one in here.

83
00:07:38,760 --> 00:07:44,430
So the move to method move the cursor to the current position of that position and the click methods,

84
00:07:44,430 --> 00:07:45,840
click the vets current position.

85
00:07:46,140 --> 00:07:54,690
But if you want to just click there and you see clicks equal to the same thing will happen, of course.

86
00:07:54,810 --> 00:07:57,450
So the sample data file is opened.

87
00:07:57,990 --> 00:08:05,010
And of course, you didn't see any movement of the mouse because we don't have obviously a move to method

88
00:08:05,010 --> 00:08:05,250
now.

89
00:08:05,250 --> 00:08:10,980
It's commented, No, you know, that's a mouse as a left button and a right button.

90
00:08:11,250 --> 00:08:13,410
It also has a middle button insertion.

91
00:08:13,410 --> 00:08:14,970
Morsi's answer?

92
00:08:14,970 --> 00:08:19,470
You need the right click a button if you want to see the context menu.

93
00:08:20,640 --> 00:08:26,480
Now you can perform a right click by doing P y all target.

94
00:08:26,490 --> 00:08:30,510
That's click Give the position where you want to click again.

95
00:08:32,309 --> 00:08:36,600
And then specify what button do you want to press?

96
00:08:37,650 --> 00:08:39,059
So by default?

97
00:08:40,460 --> 00:08:46,250
Of a clique, methods presses the left button, so when you press the left button, all your double

98
00:08:46,250 --> 00:08:52,330
press, it's then you are doing a common click with a red button.

99
00:08:52,340 --> 00:08:59,660
You will want to specify right here if you want to change the default behavior of the click method.

100
00:09:00,170 --> 00:09:02,510
So if you run this script now.

101
00:09:05,470 --> 00:09:07,150
And so nothing happened.

102
00:09:08,940 --> 00:09:14,820
Because I am on a Mac computer here, if you are on Windows, these probably worked.

103
00:09:17,220 --> 00:09:19,710
On Mac, you have to change this slightly.

104
00:09:20,220 --> 00:09:22,590
So this was about right.

105
00:09:24,920 --> 00:09:29,210
Click on Windows on Mac.

106
00:09:38,480 --> 00:09:42,590
You have to write P one, all too gooey and then use the track two methods.

107
00:09:43,340 --> 00:09:46,910
Honestly, I'm not sure why the Drag two method works in this case.

108
00:09:49,050 --> 00:09:57,240
And if this is unexpected behavior of Toyota gooey or just the bug, but it works so.

109
00:09:59,770 --> 00:10:00,790
But it works.

110
00:10:01,210 --> 00:10:04,480
So again, the coordinates two hundred eighty.

111
00:10:04,900 --> 00:10:15,070
And then the buttons should be rights also for Track two and also also you want to first do a normal

112
00:10:15,070 --> 00:10:15,700
click.

113
00:10:18,080 --> 00:10:19,340
In those coordinates.

114
00:10:19,880 --> 00:10:23,660
So a left click and then do the track to methods.

115
00:10:23,930 --> 00:10:26,060
So this is only for Mac users.

116
00:10:26,450 --> 00:10:28,660
If I run this script now.

117
00:10:32,150 --> 00:10:34,490
You'll see the context menu showing of.

118
00:10:35,900 --> 00:10:36,530
This one in here.

119
00:10:39,740 --> 00:10:41,390
So that's something to keep in mind.

120
00:10:41,450 --> 00:10:50,090
Depending on your operating system, you can also move your cursor relatively to the current position.

121
00:10:50,420 --> 00:10:56,150
So let me call on these old maybe advocates in here.

122
00:10:56,270 --> 00:11:03,770
So why ultimately move is the new method so we used to move to and then we can use move.

123
00:11:04,550 --> 00:11:12,350
So let's move the cursor to the absolute position first of this position and then move it 100 pixels

124
00:11:12,500 --> 00:11:16,870
to the right and perhaps zero vertically.

125
00:11:17,780 --> 00:11:19,610
And adds integration.

126
00:11:22,170 --> 00:11:24,180
All for one and execute.

127
00:11:25,660 --> 00:11:31,450
So, yeah, as you see, the cursor went to the absolute position first, and then it went right.

128
00:11:32,230 --> 00:11:38,080
So that could be handy in certain occasions when you want to do something depending on the previous

129
00:11:38,080 --> 00:11:44,530
position of the cursor and the lost methods I want to show you is the drag method, and we're going

130
00:11:44,560 --> 00:11:51,610
to draw something using that method on a painting app for your convenience, also suggested to open

131
00:11:51,610 --> 00:11:55,480
your browser and go to just paint that app.

132
00:11:56,140 --> 00:11:59,050
That's an app where you can draw simple figures.

133
00:12:06,570 --> 00:12:16,140
So I have opened my browser here on the sides, so I suggest you to keep your I.D. on one side and the

134
00:12:16,140 --> 00:12:17,610
browser on the other side.

135
00:12:20,060 --> 00:12:23,690
So that's James paint that app.

136
00:12:25,150 --> 00:12:29,110
You can expand this drawing area if you want.

137
00:12:29,620 --> 00:12:37,420
And then you can just drag your cursor like that so you press its you hold it and you drag it and you

138
00:12:37,420 --> 00:12:39,490
normally will draw something.

139
00:12:40,000 --> 00:12:45,250
Now how can we perform the same behavior through our Python script?

140
00:12:45,670 --> 00:12:48,850
Well, for that, we're going to use the drag method.

141
00:12:49,570 --> 00:12:54,130
But first, let's find out the starting position of the cursor.

142
00:12:54,460 --> 00:12:58,600
So let's say we want to draw a line starting from here up to here.

143
00:12:59,470 --> 00:13:02,980
So what is the position in this point?

144
00:13:03,400 --> 00:13:12,040
Well, now without moving my cursor I want to use on a Mac is the context switcher where I press command

145
00:13:12,040 --> 00:13:19,720
on top and I go to point charm without moving the cursor and then I press control or to execute the

146
00:13:19,720 --> 00:13:23,980
script on windows, you should press the Windows key.

147
00:13:24,430 --> 00:13:33,820
As far as I remember and the tab to switch between applications without using your models of use, control

148
00:13:33,820 --> 00:13:39,820
or whatever button combination you have to run your script.

149
00:13:40,630 --> 00:13:45,940
So I forgot to print out of the positions, so let me print the position here and maybe comment this

150
00:13:45,940 --> 00:13:46,450
old.

151
00:13:46,900 --> 00:13:49,690
I just want to know the position of the cursor at this point.

152
00:13:49,930 --> 00:13:56,440
So I'm going to repeat the process again, go to point charm without using the mouse control or to run

153
00:13:56,440 --> 00:13:58,900
the script position.

154
00:13:59,320 --> 00:14:01,030
And then I want to repeat the process.

155
00:14:01,030 --> 00:14:09,550
So take the cursor in here and go to charm using the context switch without using your cursor, your

156
00:14:09,550 --> 00:14:15,550
mouse and van control or to run the script, and that will give me the position of the cursor.

157
00:14:15,890 --> 00:14:22,540
So now I want to say p y all target, don't move two.

158
00:14:23,560 --> 00:14:26,920
So we first take the cursor to that place.

159
00:14:27,310 --> 00:14:28,060
Twenty six.

160
00:14:30,570 --> 00:14:33,370
And five, six, seven.

161
00:14:34,410 --> 00:14:36,180
Those are the coordinates, in my case.

162
00:14:39,630 --> 00:14:43,650
Let's set a duration of one and then you want to.

163
00:14:44,440 --> 00:14:45,630
You want to click there.

164
00:14:48,600 --> 00:14:55,530
You know, you could also avoid the move to methods and just put those coordinates directly to the click.

165
00:14:56,900 --> 00:15:01,550
Methods, if you don't want to see the movement of the cursor, it's up to you.

166
00:15:02,930 --> 00:15:10,880
And then finally, we use the track methods, so we want to see, for example, one hundred fifty zero.

167
00:15:12,560 --> 00:15:18,410
Duration is while second bottom is left.

168
00:15:19,340 --> 00:15:22,040
If this doesn't work, try with the right bottom.

169
00:15:23,260 --> 00:15:29,960
So sometimes it depends on the operating system, but I'm going to execute this now because it goes

170
00:15:29,960 --> 00:15:33,590
there and the line is thrown by Python.

171
00:15:34,790 --> 00:15:43,730
And just like that, you can duplicate that line and say zero here, minus 150.

172
00:15:47,230 --> 00:15:49,150
You know, the line is drawn again.

173
00:15:49,420 --> 00:15:50,080
Go up.

174
00:15:51,850 --> 00:15:55,960
And then maybe adds another line minus a hundred.

175
00:15:58,660 --> 00:15:59,440
Zero.

176
00:15:59,660 --> 00:16:01,660
Let's see what we will get this time.

177
00:16:06,260 --> 00:16:12,200
Up to there, and maybe you want to go down or maybe change that to minus 50.

178
00:16:15,080 --> 00:16:18,590
And then duplicate again and say zero four, that's.

179
00:16:20,400 --> 00:16:21,760
Under 50 for that.

180
00:16:29,460 --> 00:16:31,650
Oh, there we drill a square.

181
00:16:32,310 --> 00:16:34,740
That's what I wanted to teach you in this.

182
00:16:36,150 --> 00:16:36,930
Lecture.

183
00:16:38,160 --> 00:16:45,110
So this should give you a kick starts off controlling the malls with P y o Tiggy or Pi.

184
00:16:45,120 --> 00:16:51,360
Ultimately, if you like now, we're going to practice this further by writing some more real world

185
00:16:51,360 --> 00:16:52,050
programs.

186
00:16:52,440 --> 00:16:59,310
But in the next video, I'll show you also how to control the keyboard of your computer via Python so

187
00:16:59,310 --> 00:17:06,880
that you can type in text in an area where text can be typed and then use holds keys.

188
00:17:06,880 --> 00:17:14,339
So combinations of keyboard shortcuts to perform actions on your computer and so on.

189
00:17:14,579 --> 00:17:16,290
So I'll talk to you in the next video to.

