Our corporate BOFH imposes the screen lock setting with a ridiculously short delay. It's frustrating and counterproductive.
Is there a way to prevent the automatic screen lock? I would assume there is no way to override the policy-enforced setting, but maybe there is a software that mimics user activity.
Just asking before I set up a perpetual mouse wheel. (get it?)
1118 Answers
Yet another option is freeware Caffeine program. It is free for commercial use as well. From the program's homepage:
If you have problems with your PC locking or going to sleep, caffeine will keep it awake. It works by simulating a key-press once every 59 seconds, so your machine thinks you're still working at the keyboard, so won't lock the screen or activate the screensaver.
Caffeine works by simulating an F15 key up event every 59 seconds. Of all the key presses available, F15 is probably the least intrusive (I've never seen a PC keyboard with that key!), and least likely to interfere with your work.
This off-the-shelf solution also allows you to control when to enable it and disable it:
8Double-clicking the program icon empties the coffee pot, which is what the icon represents, and temporarily disables the program. Double-clicking it again refills the pot, and will keep your machine awake.
If Windows Media Player is still installed, you can play a video on loop and minimize it (the sample "Wildlife" videos work fine for this). By default, as long as a video is playing, the screen won't lock.
17I use a script I title idle.vbs:
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
Do While True objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}") Wscript.Sleep (6000)
LoopEvery six seconds, this quickly toggles numlock on the keyboard, causing Windows to believe that someone is interacting with the keyboard, preventing screen lock. This runs on vanilla windows, you don't need development or scripting tools to use it, just make a text file with .vbs as the extension and double-click it (or place it in your startup items).
Edit: you can put this script in your startup items with
choco install IdleVbs -source For more information on the Choclatey (choco) CLI installer please see:
A lot of these answers are old, and only keep alive through inputs/mouse movement. Compile this in C or C++, this will keep the session alive by setting thread execution state (Windows Only ofcourse)
#include <windows.h>
//
#define ES_CONTINUOUS 0x80000000
#define ES_DISPLAY_REQUIRED 0x00000002
#define ES_SYSTEM_REQUIRED 0x00000001
int main()
{ int result = 0; while(1) { result = SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS); Sleep(30 * 1000); } //unreachable return 0;
} 3 You can create an AutoIt script to either continually press an unused key (e.g. make it toggle the num lock, scroll lock), sleep for a minute or so, and repeat. Alternatively, if you use the keyboard a lot, you could make it move the mouse by a pixel or so in any direction.
If you don't want it continually running, you could also launch the script as a scheduled task (if you have access) to launch after the computer has been inactive for some time.
And this is a very simple script to perform an invisible mouse move, if you don't want to get into AutoIt syntax:
While True Local $pos = MouseGetPos() MouseMove($pos[0]-1, $pos[1]-1, 0) MouseMove($pos[0], $pos[1], 0) Sleep(540000)
WEndThis script moves mouse cursor by one pixel in the up-left direction and after that returns it back, then sleeps for 9 minutes (540000 milliseconds). When script is running, you can see AutoIt icon in the tray. You can stop it right-clicking this icon and choosing the corresponding option.
To make a script, install AutoIt, right-click in any folder and choose New > AutoIt v3 Script, name it, right-click this new script, choose Edit, paste the code provided above and save. You can even compile it to .exe (again, from context menu) to start, for example, from Windows Scheduler.
I like to use easy and integrated options (no additional software), like a powershell script (thanks ) that uses the "f15" as the key to success (thx to caffeine. It's indeed least interfering)
param($minutes = 180)
write "... screen will be awake for $minutes" $myshell = New-Object -com "Wscript.Shell" for ($i = 0; $i -lt $minutes; $i++) { write "... screen will be awake for" ($minutes-$i) Start-Sleep -Seconds 60 $myshell.sendkeys("{F15}")
}Put this into a myScriptName.ps1 and start it via desktop shortcut or commandline:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nop "C:\Users\myUser\Desktop\myScriptName.ps1"
UPDATE:Maybe there was some change from the administrator, but this doesn't work for me anymore Now I have to use an autohotkey-script from NBirnel: - this work perfect, because it moves the mouse (without distracting any work)
3There is an android app called "Timeout Blocker" that vibrates at an interval and you can put your mouse on it. It says not to use it at work though.
1Compile this in Visual Studio or C# Express and run it from a command prompt (or double click it). Requires .NET 4.0 or above. It does everything you are looking for.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ImWorkin
{ class Program { [FlagsAttribute] public enum EXECUTION_STATE : uint { ES_SYSTEM_REQUIRED = 0x00000001, ES_DISPLAY_REQUIRED = 0x00000002, ES_CONTINUOUS = 0x80000000 } public SYSTEMTIMEOUTS TimeOuts { get { return sysTimeouts; } } public struct SYSTEMTIMEOUTS { public int BATTERYIDLETIMEOUT; public int EXTERNALIDLETIMEOUT; public int WAKEUPIDLETIMEOUT; } [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE flags); [DllImport("user32.dll", SetLastError = true, EntryPoint ="SystemParametersInfo")] internal static extern int SystemParametersInfo(int uiAction, int uiParam, ref int pvParam, int fWinIni); private static System.Threading.Timer preventSleepTimer = null; public const int SPI_GETBATTERYIDLETIMEOUT = 252; public const int SPI_GETEXTERNALIDLETIMEOUT = 254; public const int SPI_GETWAKEUPIDLETIMEOUT = 256; public static int Counter = 0; public static int timeOutinMS = 0; public static int batteryIdleTimer; public static int externalIdleTimer; public static int wakeupIdleTimer; public static SYSTEMTIMEOUTS sysTimeouts; static void Main(string[] args) { Console.WriteLine("You are about to be workin!! Just a moment...I need to calculate a few values."); string dots = string.Empty; for (int i =2; i < 60; i++) { dots = ""; for (int ii = 0; ii < i; ii++) { dots = dots + "."; } Thread.Sleep(100); Console.Clear(); Console.WriteLine("You are about to be workin!! Just a moment...I need to calculate a few values."); Console.WriteLine(dots); } GetSystemTimeOuts(); if (timeOutinMS < sysTimeouts.BATTERYIDLETIMEOUT) timeOutinMS = sysTimeouts.BATTERYIDLETIMEOUT; if (timeOutinMS < sysTimeouts.EXTERNALIDLETIMEOUT) timeOutinMS = sysTimeouts.EXTERNALIDLETIMEOUT; if (timeOutinMS < sysTimeouts.WAKEUPIDLETIMEOUT) timeOutinMS = sysTimeouts.WAKEUPIDLETIMEOUT; if (timeOutinMS == 0) timeOutinMS = 30; DisableDeviceSleep(); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("OK. I have calculated your computers timeout periods and set the "); Console.WriteLine("necessary hooks. Your computer will not shut off the monitor, will"); Console.WriteLine("show active in any chat programs,the screensaver is disabled and "); Console.WriteLine("the computer will not lock! Anyone looking at you eaither locally "); Console.WriteLine("or remotely will think you are hard at work."); Console.WriteLine(""); Console.WriteLine("Now go do something fun...I got your back ;)"); Console.WriteLine("Oh yeah....if you close this window OR press `q' in this "); Console.WriteLine("window you are going to have to actually work."); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("This text will disappear in a 30 seconds. Just in case someone comes "); Console.WriteLine("by and reads your screen!"); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Need custom coding? "); while (Console.KeyAvailable == false) { Thread.Sleep(250); ConsoleKeyInfo cki = Console.ReadKey(true); if (cki.KeyChar == 'q') break; } } public static void DisableDeviceSleep() { SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); preventSleepTimer = new System.Threading.Timer(new TimerCallback(PokeDeviceToKeepAwake), null, 0, timeOutinMS * 1000); } public static void EnableDeviceSleep() { preventSleepTimer.Dispose(); preventSleepTimer = null; } private static void PokeDeviceToKeepAwake(object extra) { Counter++; try { SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); IntPtr Handle = FindWindow("SysListView32", "FolderView"); if (Handle == IntPtr.Zero) { SetForegroundWindow(Handle); SendKeys.SendWait("%1"); } if (Counter > 1) Console.Clear(); } catch { } } public static void GetSystemTimeOuts() { sysTimeouts.BATTERYIDLETIMEOUT = -2; sysTimeouts.EXTERNALIDLETIMEOUT = -2; sysTimeouts.WAKEUPIDLETIMEOUT = -2; if (SystemParametersInfo(SPI_GETBATTERYIDLETIMEOUT, 0, ref batteryIdleTimer, 0) == 1) sysTimeouts.BATTERYIDLETIMEOUT = batteryIdleTimer; else sysTimeouts.BATTERYIDLETIMEOUT = -1; if (SystemParametersInfo(SPI_GETEXTERNALIDLETIMEOUT, 0, ref externalIdleTimer, 0) == 1) sysTimeouts.EXTERNALIDLETIMEOUT = externalIdleTimer; else sysTimeouts.EXTERNALIDLETIMEOUT = -1; if (SystemParametersInfo(SPI_GETWAKEUPIDLETIMEOUT, 0, ref wakeupIdleTimer, 0) == 1) sysTimeouts.WAKEUPIDLETIMEOUT = wakeupIdleTimer; else sysTimeouts.WAKEUPIDLETIMEOUT = -1; } }
} 6 Mouse Jiggler might be an option:
1Extending on Cherona's answer, here is an implementation in PowerShell, using SetThreadExecutionState().
As long the script is running, screen saver and screen lock wouldn't start.
Set-StrictMode -version 3.0
# Definition of SetThreadExecutionState from -
#
#
# "EXECUTION_STATE" changed to "uint", added "public" modifier.
$Signature = @'
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint SetThreadExecutionState(uint esFlags);
'@
$ES_AWAYMODE_REQUIRED = 0x00000040L
$ES_CONTINUOUS = 0x80000000L
$ES_DISPLAY_REQUIRED = 0x00000002L
$ES_SYSTEM_REQUIRED = 0x00000001L
# Details on Add-Type in
$Kernel32 = Add-Type -MemberDefinition $Signature -Name 'Kernel32' -Namespace 'Kernel32' -PassThru
echo "Forcing system to not go to sleep (disable screen saver)."
echo "Close the window to allow sleep again."
$result = $Kernel32::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_DISPLAY_REQUIRED -bor $ES_SYSTEM_REQUIRED)
# Loop until script is forcibly stopped.
While ($true) { Start-Sleep (60 * 60 * 24) # 24 hours.
} 2 I have created a tiny tray icon application CoffeeBean which uses a much better and reliable way to simulate user's activity by using SetThreadExecutionState WinApi function which is designed almost exactly for this type of use case (for instance, it is the function which is used by video players to keep your screen on while you are watching a film). If you are familiar with Autohotkey, as an alternative, you can use this Autohotkey script.
3I've been using Mouse Move this for ages now. Advantage over Caffeine, options are customisable via GUI.
The Windows 10 version seems new, needs to be installed from Windows Store. Recommend the older one since is portable.
This is a solution which works for Win10 and with MS Office (that is, PowerPoint) installed:
MS PowerPoint (if MS Office is installed). Hit F5 to start "presentation mode". Now move to a different "desktop" (so that the "presentation" doesn't interfere with your actual work) by pressing Windows-logo + Tab, and you're good to go :)
PS: it might also work for Win7 without multiple "desktops", e.g. with Alt + Tab.
1In my case just this one line did the trick:
SendKeys.Send("{CAPSLOCK}{CAPSLOCK}");Just put it in the Timer_Tick event and set timer interval to e.g. 60000ms.
I love @JoshRivers 's solution of VB script. And I updated a little bit to stop it with duration time.
Dim objResult
Dim intervalSec
Dim durationSec
Dim cnt
intervalSec = 60
durationSec = 3600
cnt = 0
Set objShell = WScript.CreateObject("WScript.Shell")
Do While True objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}") Wscript.Sleep (intervalSec*1000) cnt = cnt + 1 If durationSec <= (intervalSec * cnt) Then Exit Do End If
Loop Mouse Jiggler was the right option for me*
the mouse does not actually move using the zen switch.
a working download link (2022/01):
one file, 984Ko, no registry key.
Unzip, run. Use the swithes below from a console or a shortcut to autorun :
MouseJiggler.exe -j -m -z -s 50this will start MouseJiggler activated (j), minimized (m), with no visible moves (j), triggered every 50 secs.
sources, MouseJiggler switches, chocolatey installation :
(*)
thanks @kkr for your answer. Cafeine was not working for me using the -stes arg (I use putty a lot)
Everyone else are writing scripts to fake being at the keyboard.
The correct way to handle it is to:
- go into the registry where the policy is
- set the value to whatever you want
- and alter the registry key permissions
- only Deny write access to anyone except yourself
This lets you block the policy from taking effect on your PC. But be aware that you need to have Administrator access to the registry.
2You should disable the "screen lock"/"sleep mode" from control panel > power options > change plan settings. Her in click the drop down for "Put the computer to sleep" and select "never".
1