12

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
  • 905

3 Answers3

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
  • 11,543
  • 8
  • 60
  • 70
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.

Eric
  • 221
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