Hi, welcome back in this video, I'll show you how to control the computer keyboards via Python.
So basically, we will instruct Python to press keys and type in text automatically.
Let's do.
That's using PUOi all target.
So if you didn't follow the previous lecture on how to control the mouse, you should do that first,
because there are some things which are related, such as we want to get the position of the cursor
because what I be doing now practically is, you see, I have this simple text file here in my project
for you, so please make sure you have created a file and it's somewhere in here.
So this is just the text file or what I would be doing is, ah, we will instruct Python to go here.
Double click this file of press the down arrow key to go to the end of the line and then press the enter
key and then type in something in here.
So Python will do all of that.
Let me show you step by step how this can be done.
So the first action is to double click the sample.
The text file to often needs to be able to do that.
We need to know the position of this file here in the current view.
So for that, we would use.
P y all together that position.
And we print out the position two seats on the terminal to the vets file and then execute the codes
using your keyboard to notes your cursor.
So instead of going there, you should use.
In my case, the shortcuts control are so go to the file control or and I execute this keyboard.
That's beautiful.
And that's keyboard that I file with.
Tell me the current position of the cursor, which was in the sampled up to use default.
So 130 to 200, then I want to.
Double click.
A vet's position, 150 to 200, so if I run the script now.
It doesn't matter, either with keyboards or with a cursor by pressing the wrong button.
The screen now opens up samples of text here on my I.D. So the double click was successful, but the
next what we want to do is, well, sometimes the cursor could be here or here or there.
So we want to go to the end of the line of this file of this text one to go to the ends.
We can press that down.
Arrow key, right?
So let's instruct Python to press the down arrow key to go to the ends of the text file using.
The press button, the press button now expects from us to answer one of the keyboard's keys, for example,
Don, which means that Donald Arrow key, see what will happen now.
I run the script.
And Python goes to the file, it opens it and then goes to the ends.
Let's do it in a more slow motion fashion.
To do that, we can import time.
And then after we double click, we say time to sleep and we wait for a second.
For example, and then we President Arrow Key so that we can see what's going on.
But first, let me put the cursor in here after Pierce.
Of Van Gogh, two keyboards for Ronit.
The file opens up and the cursor then goes off one second, it goes to the end of the file.
Now let's.
Press enter after pressing the down arrow bottom.
Execute again.
Enter, a new line is created here now we can write some code, some text.
To write text, to type in text, you use the right methods, not the press methods.
So there is a distinction between press and rights with rights, you write texts or symbols of things
that you type in, normally in a text area and press these for pressing non character keys such as down
and to control shifts, etc. So with rights, we can write some texts such as Python is good too.
So let's see how simple is OK?
Do you leave this?
Enter this empty line and go here, then run the script.
Rides and tours and Python is good to.
If you want to improve it also just to make a brake line here like that.
So let me delete that again.
Go here.
Execute the script again.
And so a brake line is created after that.
So either you can press enter again or you can enter this symbol, which is a special character that's
as a brake line, so it creates a brick line.
Now how about if we want to copy all these text?
So we want to select all first and then copy it.
We do that by using the whole key method.
So using P Oil to guide that holds key, which means this now expects a combination of keys.
For example, if you are on windows, you do control and aid to select all the text on a Mac.
You do command an eight.
So two arguments divided by commas.
And given it strings, let's see what will happen if I execute this.
So all the tax is selected.
But how can we compete to make it available in the clipboards?
Again, using key, we provides the shortcut, the keyboard shortcut for copying, which is Control
C or Command C, depending on whether you are on Windows or Mac, Linux would also be control C.
Ron?
Yeah, it's selected and then it's copied silently, but it's available on the clipboard.
Now how can we access these texts?
How can we paste it?
Well, you can paste it using command or control v so you execute again.
And it looks, though, as nothing happened, but.
The copy paste actually worked.
I'll explain you why it looks like it didn't work.
So the chain of command source.
Selects all copy all.
And paced all so sudden, like it's all copy all and one paced all.
So the moment where the text was paced, everything was selected.
So basically the same text overall to the existing text.
If you want to see the results, you maybe you want to praise the down arrow key ones, a then paste.
So what I'm saying is after you select enough read copy, then perhaps say goodbye all to good press.
Don, and then you pay so that you get out of that selection.
So now I'll throw you again.
Yeah.
So, no, it worked.
We got this text paste, it's here, so now we have more text.
Now, if you want to apply the down key more times, you would want to make a list of how many donkeys
you want, so two or three or four.
But of course, we don't want to type that downstream.
Many times you can do this, let's say five times down.
It will give us basically, I can show you what that gives you.
So if you say five times the list, which has only one item which is the string, don't you get a list
of five items so run?
Copy paste.
You get the idea.
In this case, it was not necessary to press a donkey five times, but I wanted to include this in case
you want to repeat a key press multiple times.
And this is all you can control the computer keyboard.
Now regarding the copy paste.
In certain cases, you might want to use the cockpit text inside Python.
Currently, that is not possible with pilots of gum because when you copy the text, you are currently
in this view.
But what if you want to access that cockpit text and process?
It's inside your Python script in here, so that's not possible, but it is possible if you use a library
which is designed to process the text in the clipboard.
So the text that has been copied in order to learn how to process that copy text inside the python,
you need to take that next video.
There are explain how to get access to that copy text within Python.
See there.