Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made an exe file that puts the screens on standby for when I want darkness in the room to sleep but not to have to put the PC to sleep or hibernate it as this sometimes ruins my Winstep Nexus Ultimate bars and moves them to different screens and the individual windows open and it takes a long time to wake up from hibernation, but when the mouse moves the movement wakes them up, I want to force Windows to not wake up when the mouse moves as this could happen accidentally, I want to use some C# lines of code that running them would make Windows not wake up when the mouse moves (maybe also prevent the keyboard from waking up the standby mode of the screens).

What I have tried:

C#
static void Main()
       {
           System.Threading.Thread.Sleep(2000);
           SendMessage((new Form()).Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
       }


       static private int SC_MONITORPOWER = 0xF170;
       static private uint WM_SYSCOMMAND = 0x0112;

       [DllImport("user32.dll")]
       static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
Posted
Updated 7-Dec-19 1:48am
v3
Comments
phil.o 7-Dec-19 5:04am    
Just a thought: if you are planning on disabling mouse and keyboard, how do you plan on recovering control over your computer?
john1990_1 7-Dec-19 7:46am    
By the power button. No in fact I would need another way since the PC doesn't sleep. Dunno, so we need to disable only the mouse.

1 solution

A simple way would be to set the MouseMove event for the form to execute your SendMessage() again. Also see: c# - MouseMove Event in Form - Stack Overflow[^]
 
Share this answer
 
Comments
john1990_1 7-Dec-19 8:33am    
I know global mouse and keyboard hooks, but how to leave my exe running till a keyboard key is pressed?
RickZeeland 7-Dec-19 8:36am    
If you mean for a Console Application, you can use Console.ReadKey()
john1990_1 7-Dec-19 8:38am    
I'm more familiar with WinForms... also I don't want a console window. Also I need the key to be detected globally and not only on the console window. can I make: "while (true) Thread.Sleep(2000);" in void Main() ?
RickZeeland 7-Dec-19 8:54am    
Sure, but your Winforms application would not be very responsive, you need to use Application.Doevents() for that ...
john1990_1 7-Dec-19 14:07pm    
Thx a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900