Prevent Windows screen lock when playing with joystick

12

2

My Windows 7 system is set to lock the screen after 20 minutes of inactivity (this is a workplace-enforced policy; I cannot change this setting).

When I am using the keyboard/mouse, Windows treats them as user inputs, and thus the screen remains unlocked. However, when I am playing any game using only joystick, Windows treats that as no activity, and thus the screen gets locked after the timeout.

Is there a way to prevent screen lock while using joystick-based games?

As a workaround, I am currently playing some random video (muted) in the background using VLC, which prevents screen lock. But there should be a better way to handle this problem...

I could possibly write a script, which would simulate some keystrokes like {NUMLOCK}{NUMLOCK} via sendkeys. However, there is always a possibility of an unintended side-effect of this, such as preventing me from using these keys in my game options.

anishsane

Posted 2015-02-23T08:47:46.913

Reputation: 845

Yes it can be achieved in way you suggest. But did you check whether you can switch power plans? (Instead of changing power plan settings.) Sometimes High Performance power plan has energy-saving features disabled. And also: if they locked your power plan settings, did they leave unlocked the ability to install or run software not authorized by admins? – miroxlav – 2015-02-23T09:10:52.750

"Sometimes High Performance power plan has energy-saving features disabled" Good idea.. However, this setting is enforced as policy in screen-saver settings, not power-plan settings. "did they leave unlocked the ability to install or run software not authorized by admins" In general, yes, but those softwares cannot change the settings enforced by policy. – anishsane – 2015-02-23T09:51:24.693

Does anyone know of any windows API, to prevent screen lock, the one that media players like VLC use, while playing videos? – anishsane – 2015-02-23T09:52:43.693

Answers

1

Whenever you press a key on the keyboard, or move/click the mouse, Windows resets its idle timer. There is a Windows API function you can call that resets the idle timer in exactly the same way. By calling the function at regular intervals, the screen-saver will never activate and the computer will never lock. This is what VLC and other applications do.

The function name is SetThreadExecutionState and is found in kernel32.dll. In VB the actual call looks like this:

SetThreadExecutionState(ES_SYSTEM_REQUIRED Or ES_DISPLAY_REQUIRED)

The two constants being:

ES_SYSTEM_REQUIRED = &H1 and ES_DISPLAY_REQUIRED = &H2

You can't make these calls from VBScript, so you'd need something more advanced to code this up with.

Incidentally, the above is the technically correct way of doing this. Many utilities I've seen on the web use silly tricks like simulating keystrokes or jiggling the mouse. That's bad in my opinion, as it can interfere with your work.

Anyway, I've digressed too much. This kind of stuff belongs on Stack Overflow.

misha256

Posted 2015-02-23T08:47:46.913

Reputation: 10 292

0

You can use the SetThreadExecutionState function, as provided by Python's ctypes module, as part of a standalone Python application you can run in the background and exit with Ctrl+C. (Thanks to the answer by @misha256 for explaining the use of this function!)

import ctypes
from time import sleep

ES_CONTINUOUS        = 0x80000000
ES_SYSTEM_REQUIRED   = 0x00000001

# CONTINUOUS repeats the action until the application closes,
# SYSTEM_REQUIRED 'forces the system to be in the working state by resetting the system idle timer.'
ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)

# wait until broken
while True:
    # attempt to run the following code
    try:
        sleep(1) # thanks @anishsane
    except(KeyboardInterrupt): # catch a Ctrl+C
        break # stop waiting

I've built this script for Windows. Visit the GitHub release page here.

HewwoCraziness

Posted 2015-02-23T08:47:46.913

Reputation: 91

1Instead of 0 as placeholder, the standard way is pass. – anishsane – 2018-12-21T03:19:32.077

1Also, it is better to keep some sleep() instead of pass. Otherwise, this will busy-wait and consume CPU. You can even sleep for hours there. – anishsane – 2018-12-22T05:49:50.320

0

Just run a file named: no_sleep.vbs. This text file no_sleep.vbs should contain the text matter below.
Please google if not knowing how to create a blank text file and rename its extension.

set wsc = CreateObject("WScript.Shell")
Do
   'Five minutes
    WScript.Sleep(5*60*1000)
    wsc.SendKeys("{F13}")
Loop

The key F13 should not interfere with game options

sahara trip

Posted 2015-02-23T08:47:46.913

Reputation: 1

Hmm. Could work. – anishsane – 2019-09-03T04:46:22.213