I'm trying to prevent the screensaver from kicking-in. I've tried Sending keystrokes but they all require to grab a window by title. If there is no open window on the desktop, there is nothing to grab.
So I have found something that might work but I'm no C# wizard. It's based on Simulating a keypress AND keyrelease in another application?
The code below is supposed to send a 1 but I'm missing something. I get no errors until I call the new type from Powershell. After this works, I want to make it send an F15 to reset the screensaver countdown but not modify stuff on the screen. (But I gotta crawl first by sending 1s)
Add-Type @"
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
public static class PressKeyForMe
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
//public static void Main(string[] args)
public static void Main()
{
//This code will press and hold the '1' button for 3 secs, and then will release for 1 second
//VK_F15 0x7E
keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
Thread.Sleep(3000);
keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
Thread.Sleep(1000);
}
}
}
"@
cls
#all of these give me: Unable to find type
[void] [PressKeyForMe]::Main()
[void] [ConsoleApplication1]::PressKeyForMe()
[void] [PressKeyForMe.Main]
[void] [ConsoleApplication1.Main]
[void] [ConsoleApplication1.PressKeyForMe]::Main()