Skip to content

Instantly share code, notes, and snippets.

@ereli
Created April 9, 2015 10:15
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ereli/a0faf53e417403df8863 to your computer and use it in GitHub Desktop.
Save ereli/a0faf53e417403df8863 to your computer and use it in GitHub Desktop.
prevent screensaver and workstation lockout - vbs script that presses the numlock key twice every 10 seconds
# ref: http://superuser.com/a/836346
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
i = 0
Do While i = 0
objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
Wscript.Sleep (10000)
Loop
@MamadouAlphaDiallo
Copy link

@ereli Hello,
did this script work for you. Because I made a bash script that launches it but my screen locks unfortunately

@ereli
Copy link
Author

ereli commented Jun 9, 2022

@ereli Hello, did this script work for you. Because I made a bash script that launches it but my screen locks unfortunately

It did @MamadouAlphaDiallo , but it was 7 years ago. How are you running it?

@MamadouAlphaDiallo
Copy link

MamadouAlphaDiallo commented Jun 9, 2022

@ereli Thank you for your answer, I use an rdp connection from my workstation to a virtual machine (jenkins server) where I build my tests. But when the session is locked, my tests fail. I had used a batch script which launches before closing the session, the batch launches correctly but after around 20 to 30 minutes, when I restart the tests, failure and failure. FYI the application that I am automating is a desktop application.

@phreeKigeeky
Copy link

I had a similar problem but the double numberlock toggle on off every ~6 seconds would miss the repeat and would unexpectedly toggle the number lock on and off. Played incessantly with adding some type of wait / pause / sleep before between or after the numlock strokes to get a timing that would consistently toggle off and back to on reliably.
I finally found a suggestion somewhere on the internet that fixed the whole issue with trying to use Numlock as the keep alive keystroke. Instead of NumLock I used a single keystroke of the F13 key... which in Windows is not on the physical keyboard but is included as a defined key by the operating system (apparently there are F13 thru F24 keys similarly available although I have only tried this with the F13 keystroke).
So the keep alive script (that prevents lock and screensaver) that I finally got to work on my physical workstation ..
and the VM Ware Horizon virtual Win 11 desktop (Note: worked fine in Win10 version before we upgraded) is below...

Dim objResult

Set objShell = WScript.CreateObject("WScript.Shell")

Do While True
objResult = objShell.sendkeys("{F13}")
Wscript.Sleep (6000)
Loop

It probably could be more elegant ... or have more defining features (ie a timeout or run time parameter) but I basically stopped working on it once I found an incarnation that worked.
Oh and if I need to stop it a reboot will do it ...
or I will simply do a Ctrl Alt Delete and end task on Microsoft Windows Script Based Host wscript.exe that is running in Task Manager. Not a demanding task in terms of memory space use .. usually occupying/allocating .3 MB when running in Win 10 and ~1.0 MB running in the Win 11 virtual instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment