3

I need to press any useless key (like F11) every 5 minutes or less to keep the windows active (if it became inactive for 5 minutes it will lock-out).

But I need the key to be pressed on the desktop (so it doesn't effect any open windows).

I have tried

Loop
{
   ControlSend, {F11}, WindowsApplication1
   Sleep, 100000
}

but doesn't seem to work.

thanks.

2 Answers2

2

Why do you want to send F11 to the desktop?
If it's just to keep your computer awake you can use the following script:

#SingleInstance, force
#Persistent
SetTimer, NoSleep, 30000
Return

NoSleep:
 DllCall( "SetThreadExecutionState", UInt,0x80000003 )
Return

From Microsoft:

SetThreadExecutionState function
Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.


It is also possible to emulate mouse-action to keep the system from sleeping
(if above script somehow doesn't work for you):

Loop {
    Sleep, 30000
    MouseMove, 1, 0, 1, R  ;Move the mouse one pixel to the right
    MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
}

So there would be no need to emulate actually pressing a key.

Rik
  • 13,565
1

In previous comment, @Rik proposed an AutoHotkey script. It works, but let me provide a simpler version:

#Persistent ; script will stay running after the auto-execute section (top part of the script) completes
#SingleInstance Force ; Replaces the old instance of this script automatically

; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
; 0x80000002 = ES_CONTINUOUS | ES_DISPLAY_REQUIRED
DllCall("SetThreadExecutionState", UInt, 0x80000002)

This script will make Windows think that there is an application running which shouldn't be interrupted by the screensaver (but you can still lock your screen manually by pressing a default Win+L key combination, for instance). It is the technique which is used by media players in order to prevent your screen from switching off while you are watching a video. This technique works even if there are corporate policies enforced which don't allow you to disable screen locking in Windows options.

PolarBear
  • 623