I'll write a simple and easy code that you can run to lock your pc's screen. 

How can you lock your Computer screen?? Simple way is to press the 'windows' + 'l' combination key. And there's a lot of other ways out there.

I do coding in python and made some codes to recognize or identify faces or objects. Then I thought what if I can control my pc based on those codes conditions?? Suppose to lock your pc if it detects someone else other than you? [ Many of you might think "I need this", I'll make that for you too.]

Turned out python has a library called pyautogui which is very powerful and yet easy to use. pyautogui works both for pyton2 and python3.

So today I'll write a simple and easy code that you can run to lock your pc's screen. You might also insert those few lines in your code to make an option to lock the pc.

LET'S HOP IN!!

Update: I already have started working on this project ChikonEye find it - https://github.com/ashraf-minhaj/ChikonEye_a-third-eye-app-which-protects-your-work-from-peepers.

Step 1: Things You'll Need Beforehand

If no error shows then congrats you have successfully installed pyautogui.

First of all you'll need to install python. I assume you already have the latest version of python which is python3.7.1 by now. If not, there's a lot of tutorial out there, just google it.

After that you'll need to install pyautogui library. I did pip install like this- open windows search then type command prompt or powershell then type this

Then some hodgepodge writings will come and then type python on your prompt and hit 'ENTER'. Then import pyautogui library (see image 2).

If no error shows then congrats you have successfully installed pyautogui.

Step 2: The CODE

That's it now run the code. You can use an editor to edit the code as you want or can run it by command prompt or powershell ( see picture 1). Remember the path where you have saved the code (picture 2).

Although we can just lock our pc by keyboard combo WIN + L but for some reason that doesn't work using pyautogui. But pyautogui can write, click etc. I took advantage of that. If we type "rundll32.exe user32.dll, LockWorkStation" in the command prompt or powershell then it will lock the pc too. So I wrote the code so when you run the code it opens cmd (command prompt) then types that line and hits 'ENTER'. I used the sleep function to wait for some mili seconds so that the code does't run too faster than the system.

 Download Code from below or from github https://github.com/ashraf-minhaj/pyautogui-Automatic-Computer-Lock-


That's it now run the code. You can use an editor to edit the code as you want or can run it by command prompt or powershell ( see picture 1). Remember the path where you have saved the code (picture 2).

Step 3: Finsh!

So run the code and see the magic. It actually ain't any magic as you know what it does but it seems pretty "osthir" (cool).I'll make codes to lock screen if some1 elses face is detected in the next. Stay tuned and pray for me. Thanks a lot.

Ashraf Minhaj
A Computer Science and Engineering student and a Hobbyist. Love to make robots, DIY gadgets. Interested in robotics and AI. 

Tags