11

Right now my win 10 PC is set to turn off the display after 30 minutes of inactivity. I'm wondering if I can have that same function on-demand. I often use my PC at night before going to sleep and want to be able to turn off the display from my wireless keyboard. I know this keyboard has a sleep button, but sometimes I do not want to put my computer to sleep due to downloads or other processes still running. So I'm wondering if there is anyway I can have some .bat file, or other method of just turning off the screen without having to get up and press the button on my monitor. It would be awesome if I could wire it up to a button on my keyboard too, just like sleep is.

Thanks!

intA
  • 423

3 Answers3

11

Based on this Windows 7 answer which links to this script page, and checking this MSDN library page, that the codes used are correct you can run the powershell script below to switch the screen off. I ran it on my Windows 10 laptop and it worked fine. Moving the mouse after running the script wakes up the screen like you normally do when the screen has turned off from the power save feature.

The 2 second delay before it turns off the screen is so that you can have time to let go of the mouse/keyboard before Windows starts checking for movement for turning the screen back on.

Using this powershell script, you do not need any third party tool or need to mess with your existing settings. Just save it in a file with a .ps1 file extension and run it with powershell.

# Turn display off by calling WindowsAPI.

SendMessage(HWND_BROADCAST,WM_SYSCOMMAND, SC_MONITORPOWER, POWER_OFF)

HWND_BROADCAST 0xffff

WM_SYSCOMMAND 0x0112

SC_MONITORPOWER 0xf170

POWER_OFF 0x0002

Add-Type -TypeDefinition ' using System; using System.Runtime.InteropServices;

namespace Utilities { public static class Display { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam );

  public static void PowerOff ()
  {
     SendMessage(
        (IntPtr)0xffff, // HWND_BROADCAST
        0x0112,         // WM_SYSCOMMAND
        (IntPtr)0xf170, // SC_MONITORPOWER
        (IntPtr)0x0002  // POWER_OFF
     );
  }

} } ' start-sleep 2 [Utilities.Display]::PowerOff()

BeowulfNode42
  • 2,022
  • 2
  • 20
  • 25
4

I'm wondering if I can have that same function on-demand.

Use nircmd from nirsoft (if it is supported by your monitor):

nircmd monitor off

monitor [action]

Changes the state of the display monitor. The [action] parameter may contain the following values:

  • off: Turn off the monitor
  • on: Turn on the monitor
  • low: Set the monitor to low power state.

This command only works in systems that support this feature. If you have a problem that NirCmd remains in memory when using this command, you may try to use async_off, async_on and async_low actions instead of on/off/low actions.

Source NirCmd Command Reference - monitor


Disclaimer

I am not affiliated with nirsoft in any way, I am just an end user of their software.

DavidPostill
  • 162,382
1

I'm working on a small startup company specializing in Windows utilities to fix issues like this (for the tech- and not tech-savvy). Disclosure: I am the owner and probably biased! :)

We have an application called Lights Out with pretty robust code that forces the display off, temporarily overrides Windows sleep settings to keep the computer awake, and maintains that state until the next mouse or keyboard input. It's C++ packaged as an exe that works with one click, similar to the example in another answer but with a lot more built into it.

Anyway, I think it will do exactly what you need.

https://sparkbyte.ca/products/lights-out

Journeyman Geek
  • 133,878
Michael
  • 11