我创建了一个小的C#控制台应用程序来移动屏幕上的指针,希望这样可以防止屏幕在几分钟后进入睡眠/锁定状态。不幸的是,屏幕仍然会在几分钟后进入睡眠状态。
有人知道是否实际上可以编写一些C#代码来模拟用户输入(鼠标或键盘),并防止屏幕自动进入睡眠/锁定状态吗?
这是我所拥有的,我认为它可能会奏效:
class Program
{
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
static Random rnd = new Random();
static void Main(string[] args)
{
Rectangle screenRes = Screen.PrimaryScreen.Bounds;
int widtMax = screenRes.Width;
int heighMax = screenRes.Height;
int w;
int h;
do
{
while (!Console.KeyAvailable)
{
w = rnd.Next(1, widtMax);
h = rnd.Next(1, heighMax);
SetCursorPos(w, h);
System.Threading.Thread.Sleep(1000);
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
}
}
SetThreadExecutionState
。更多细节请参见这里。 - Damien_The_Unbeliever