7

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!

7

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()
1
  • Someone is suggesting I change the two SendMessage functions to be PostMessage functions. The only change needed is the name of the function. Doing some testing I see no difference in the user experience between the two for this code. In both cases the script ends a fraction of a second before the screens go blank, despite the PostMessage function supposed to be adding the message to a queue and immediately finishing, while the SendMessage waits for the message to actually be acknowledged or handled. As an FYI, this still works with Windows 10 20H2 update. – BeowulfNode42 Dec 3 '20 at 10:02
2

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.

1
  • This, unfortunately, locks the session instead of allowing to continue working with "monitor off". – tejasvi88 Dec 14 '20 at 5:04
-1

"Nircmd monitor off" puts the system to sleep, or "locks the session," if there is a distinction. A few old utilities I have that used to work do the same -- "shutter.exe," "shutdown101," and "DisplayPowerOff." It is clear that some genius at Microsoft decided to change the behavior of the API. Nor does there seem to be any replacement. I can't believe the number of quirky, time-wasting, hair-pulling, %^%$$# bugs Microsoft has introduced into its wretched operating system since Windows 7.

New contributor
Spiffy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.