Control Your Computer with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm about to show you how you can control your mouse and your keyboard just with some simple python code as a bonus I'm also going to show you how you can convert your python script into an executable file so you don't have to run it from a command line all the time I'm going to assume that you have python installed if not just check the video in one of these corners I'll wait for you or you should probably pause this all right in order to control your computer we're going to need at least one python package but we're going to install two because we're going to use two in this video you really only need one because you can kind of do both or do everything with one but we're going to use two all right so all you're going to have to do is do pip install Pi Auto GUI and then press enter I already have it installed and then pip install uh keyboard which again I already have it installed great so let's start out by controlling our Mouse so what you can see here is that we have Pi Auto GUI which is the first package we're going to be using and we're going to say it's going to say move to 100 500. this is an x-coordinate of your monitor and then this is the y coordinate for your monitor and duration means how long is it going to take to do this then we have imported time as well and this is going to allow us to sleep basically wait for two seconds then we're going to do PI Auto gui.move2500 which as you can see is just an x coordinate and we're passing none for the y coordinate that means it's just going to move just in the X and it's not going to move at all in the Y so let's go ahead and run this and see what happens look no hands all right so you can see that it actually did it and I'm not not lying this isn't movie Magic all right so this is great but what if we want to click our Mouse so I have this code right here so you can do PI Auto GUI dot click and it will click wherever it's wherever it's focused wherever the mouse is you can also tell it to click a specific location so let's give this a try I'll run this again no hands just kind of maybe I should speed this up a little bit next time but there we go we clicked we almost have full control of our computer already but what if we want to type something I don't want to use my hands for that so let's fix that so to type text we can just delete some code here and I have some examples here so here I will get rid of these comments using control forward slash you can get rid of comments within vs code most most things that uh most Ides so here we can see we have Pi Auto gen typewrite hello world this is going to type this over to 0.2 seconds then we're also going to press enter we're going to hold shift down we're going to write uppercase and then we're going to let go of shift if we didn't let go of shift the program as long as the program is running the system is going to think shift is is being pressed so be aware make sure you key up shift all right so let's let's go ahead and scroll down a little bit so that it's gonna ideally write it somewhere around here based off of our our other stuff all right so let's just comment this stuff out here so it goes a little faster and change this to 500 so it actually clicks around here I just tested this and it clicked in here and messed things up a little bit all right so let's go ahead and run this no hands click types out hello world uppercase excellent and if I start typing it is not an uppercase I think I said this was point over it types everything over 0.2 seconds but it actually types each character in 0.2 seconds so my bad there I'm going to go ahead and delete this because we don't need random text in our python file right so what if we want some kind of input from the user right maybe we want them to be alerted of something or something like that well we can do that too let's get ahead and delete all this code here and let's uncrement this so basically what's going to happen now is I'm going to get rid of these types so that we don't actually have to do that anymore because otherwise it might mess up our code a bit because it'll type in here so instead we're gonna actually just print out some things that are going to be asked of the user or in this case me okay let's run this here so we're moving our Mouse again and then this time we get text this is coming from this one right here once I click OK you'll be able to see what we see in the command line okay all right and we get another pop-up I either have the option to okay or cancel if I click OK we get OK again this time I get some kind of text input so I can type subscribe what a good idea all right now if I click OK we get subscribe this time I actually have password text so it's it's using a mask basically I mean it's hiding the input so this time I'm going to type Shake is cool because I think so and I'll click OK and you've seen my secret password because we're printing it out right here all right this is awesome but this also all all requires that I'm doing something actively right like it's gonna move things for me it's not really waiting for me to do something so let's use the keyboard library to kind of fix that so we're going to go ahead and delete some code here and we're gonna use keyboard I'm not going to import this because I actually imported this up at the top here and we're gonna do this here I'm going to uncomment everything yeah so what are we doing here we are using the keyboard uh program and we are adding a hotkey so that anytime I press space then we're going to run this code here which is to save a screenshot it's also has a counter so that when I press this the counter will be Set This Global uh means that we're referencing a global counter outside of this this function here and then we're going to create a file name which is just screenshot Dash whatever the counter number is dot PNG and then we're going to tell hi Auto GUI which is a separate package from keyboard the one that we've been using pi Auto GUI so take a screenshot and save it with this file name in the current file in the current directory or folder that we're running our scripting cool so so we're done here we have counter equals counter plus one so that we don't continually name them screenshot Dash zero and yeah we should be good now you might notice we also have keyboard dot weight so keyboard.weight basically means that it will pause and it'll wait for things to happen if we didn't do this we just set up everything and then we'd immediately exit the program which isn't exciting because well then we're not doing anything I'm going to just comment out these things right here because if I don't then we're going to have our Mouse Moving and stuff again so let's go ahead and see what happens so if I go ahead and run this nothing is happening which is okay because we're not printing anything but it hasn't errored so let's type Jake space oh look at that we got a screenshot is space cool space subscribe space all right if we go ahead and kill our program using Ctrl C I can go ahead and delete this here but if I go and look here we actually have screenshots of each time I pressed space and this works even if I go and do other things so if I wanted it to be every time I press the letter Q when I'm playing League of Legends in my awesome gold tier then we take a take a screenshot every time I press Q or maybe R because that's probably more exciting than Q if you if you play league which if you don't good for you if you want to with pi Auto GUI you can even take a screenshot of a specific area of the screen so maybe every time you press a button it'll take a screenshot of the bottom or of the top left or wherever you want now as promised I'm going to show you how you can turn your script into an executable file I'm only actually going to show you how to do this on Windows because that's what I'm coding on right now but I'll leave a link in the description for how you can do it for a Mac as well all we really need is something called Auto pi to exe which is a pip package and what happens is when we install this package we can just type uh uh Auto pi to exe within our command line and it will actually uh well it'll tell us to pick something so let's see here Auto pi to exe enter I already have this installed all right great so we can see here we have a script location so we're gonna browse and I'm going to go ahead and pick this script here so this is the script control sorry control and click open then just saying that it's a single directory or a one file uh I don't think this really matters to be honest with this case and we're going to say it's a console based thing because there's no windows that we're opening up here you can provide it with an icon if you want like an Ico file we're not going to do that because I don't really care what it looks like and we're done so let's go ahead and click convert dot pi to EXE hold on wow that was fast it actually did take a little bit of time but it wasn't too bad all right so if I go ahead and open the output folder here we can see that we're going to get this control so I actually have this under this computer control file or directory uh which is you can see where our screenshots are so this was a control uh python script that we're running here or we were writing here and if I go ahead and look into output we have control which matches the name for our script and within here there's a whole bunch of stuff but the one thing we really care about is this control application which is basically an EXE so what I can actually do is if I go ahead and send this to the desktop I can get rid of this stuff get rid of that and we can see that I have this control on my desktop now let me open uh this back up and go here because I believe where this is actually going to save a screenshot to is here but let's see it go ahead and take a look so if I double click this we can minimize this or whatever if we want to but as you can notice we didn't have to type or open up a console and type Pi control dot PI right it just did it for us so great so we can go ahead and minimize this but now if I press space it was a little confused where the screenshots went here I thought they were going to be in the other directory but it turns out they're in the directory where the exe is so my bad oh there they are within this this directory here you can see that we now have two screenshots so if I go ahead and open this there we go it's now taken screenshots pretty cool I hope you enjoyed this tutorial as you can imagine you can do a lot of really cool things with your computer because well you can just program some stuff and you no longer have to touch it pretty neat if you did like this please drop a like down below it really helps out the channel and consider subscribing for more videos like this in the future maybe while you're down there you can even drop a comment for what you'd like to see next there's so many things you can do with programming so the sky is kind of the limit unless you build Rockets then I guess it's further than that but you know I'm Jake you're awesome and this has been controlling your computer with python till next time cheers
Info
Channel: JakeEh
Views: 29,855
Rating: undefined out of 5
Keywords: Control computer with python, controll computer with python, control computer iwth python, controll cmoputer iwth python, control cmoputer with python, python, keyboard, mouse, control, automate, tasks, boring tasks, repetitive tasks, automate your life, automate the boring stuff with python, python for beginners, python tutorial, python tutorial for beginners, python basics, python programming
Id: ByIdnfMFG3E
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Sun Aug 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.