Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2)
@Ma-XX-oN
Copy link

Ma-XX-oN commented Oct 20, 2021

Why is this using SendMessage? Wouldn't PostMessage be a better fit as you don't have to wait for a response, especially since you're sending to all top level windows, where all of which may not play nice?

@EugeneLoy
Copy link
Author

EugeneLoy commented Oct 20, 2021

@Ma-XX-oN This piece of code originally came from stackoverflow. I copied it years back and I can't quite remember the source post. Anyway, it "just did the job" for me back then and I didn't give it much thought. If someone uses this, it is probably a good idea to take note of your comment.

@Ma-XX-oN
Copy link

Ma-XX-oN commented Oct 20, 2021

Well, if anyone wishes to have that code, here it is:

powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2)

I found that using SendMessage resulted in the command to stall because it was waiting for a response.

@EugeneLoy
Copy link
Author

EugeneLoy commented Oct 20, 2021

@Ma-XX-oN Thanks. I've updated the gist with your code.

@IVIJL
Copy link

IVIJL commented Nov 9, 2021

is there a way to tell it to which of the multimonitor setup to turn off ?

@Ma-XX-oN
Copy link

Ma-XX-oN commented Nov 13, 2021

Not that I'm aware of. AFAIK, it just turns off all of them.

@wilsonephillips
Copy link

wilsonephillips commented Apr 22, 2022

Very nice. I dropped it into my scripts folder and placed a shortcut on my desktop. I already had something similar on Linux and I am glad to see someone already did this for Windows.

@huydhoang
Copy link

huydhoang commented May 11, 2022

Great script! Just a note, .NET 3.5 is required on Windows 11 for this to function. Otherwise you'd get mscoree.dll error.

@vjsindhu-github
Copy link

vjsindhu-github commented Sep 9, 2022

Great script! Just wanted to add this here in case someone needs it, if you want the screen shutdown to be delayed by a certain amount of time after you run the .bat file, you can add this before the .bat file code:

@echo off
timeout /t x >nul

x is the number of seconds you want the delay for. Really helpful if you use chrome remote desktop :-)

@qwertynik
Copy link

qwertynik commented Sep 23, 2022

Any way to ensure that the screen is turned back on only when the mouse is clicked and not when moved?

@wilsonephillips
Copy link

wilsonephillips commented Sep 23, 2022

I am not aware of a windows setting for that. Any activity will bring it back. If there is YouTube video playing in the background, it can bring the screen back on too. So, if you want to just listen, open another tab in your browser so that the YouTube video is not in the active tab.

@qwertynik
Copy link

qwertynik commented Sep 24, 2022

Hmm, during times when listening to some audio on the computer and turning the screen off, it works and isn't turned back on.
Hopefully there are ways to turn the screen back on only when there is certain event - in this case a mouse click.

@zilpe
Copy link

zilpe commented Oct 23, 2022

Hmm, during times when listening to some audio on the computer and turning the screen off, it works and isn't turned back on. Hopefully there are ways to turn the screen back on only when there is certain event - in this case a mouse click.

Did you ever find a solution to this? Want to do the same thing. Thanks!

@Ma-XX-oN
Copy link

Ma-XX-oN commented Oct 24, 2022

This shuts off the screen and locks the desktop. Would be nice if there was a way to just shutoff the screen.

AFAIK, the Windows OS will wake the screen on any KB or mouse event, and will bring you to the unlock screen (or desktop if security is disabled).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment