powershell mouse move does not prevent idle mode

后端 未结 8 1778
猫巷女王i
猫巷女王i 2021-01-30 09:36

Before I start, here is my very first little code I wrote in PowerShell :)

[System.Windows.Forms.Cursor]::Position = `
    New-Object System.Drawing.Point($pos.X         


        
8条回答
  •  终归单人心
    2021-01-30 10:12

    I created a PS script to check idle time and jiggle the mouse to prevent the screensaver.

    There are two parameters you can control how it works.

    $checkIntervalInSeconds : the interval in seconds to check if the idle time exceeds the limit

    $preventIdleLimitInSeconds : the idle time limit in seconds. If the idle time exceeds the idle time limit, jiggle the mouse to prevent the screensaver

    Here we go. Save the script in preventIdle.ps1. For preventing the 4-min screensaver, I set $checkIntervalInSeconds = 30 and $preventIdleLimitInSeconds = 180.

    Add-Type @'
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace PInvoke.Win32 {
    
        public static class UserInput {
    
            [DllImport("user32.dll", SetLastError=false)]
            private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    
            [StructLayout(LayoutKind.Sequential)]
            private struct LASTINPUTINFO {
                public uint cbSize;
                public int dwTime;
            }
    
            public static DateTime LastInput {
                get {
                    DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
                    DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
                    return lastInput;
                }
            }
    
            public static TimeSpan IdleTime {
                get {
                    return DateTime.UtcNow.Subtract(LastInput);
                }
            }
    
            public static double IdleSeconds {
                get {
                    return IdleTime.TotalSeconds;
                }
            }
    
            public static int LastInputTicks {
                get {
                    LASTINPUTINFO lii = new LASTINPUTINFO();
                    lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
                    GetLastInputInfo(ref lii);
                    return lii.dwTime;
                }
            }
        }
    }
    '@
    
    Add-Type @'
    using System;
    using System.Runtime.InteropServices;
    
    namespace MouseMover
    {
        public class MouseSimulator
        {
            [DllImport("user32.dll", SetLastError = true)]
            static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetCursorPos(out POINT lpPoint);
    
            [StructLayout(LayoutKind.Sequential)]
            struct INPUT
            {
                public SendInputEventType type;
                public MouseKeybdhardwareInputUnion mkhi;
            }
            [StructLayout(LayoutKind.Explicit)]
            struct MouseKeybdhardwareInputUnion
            {
                [FieldOffset(0)]
                public MouseInputData mi;
    
                [FieldOffset(0)]
                public KEYBDINPUT ki;
    
                [FieldOffset(0)]
                public HARDWAREINPUT hi;
            }
            [StructLayout(LayoutKind.Sequential)]
            struct KEYBDINPUT
            {
                public ushort wVk;
                public ushort wScan;
                public uint dwFlags;
                public uint time;
                public IntPtr dwExtraInfo;
            }
            [StructLayout(LayoutKind.Sequential)]
            struct HARDWAREINPUT
            {
                public int uMsg;
                public short wParamL;
                public short wParamH;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct POINT
            {
                public int X;
                public int Y;
    
                public POINT(int x, int y)
                {
                    this.X = x;
                    this.Y = y;
                }
            }
            struct MouseInputData
            {
                public int dx;
                public int dy;
                public uint mouseData;
                public MouseEventFlags dwFlags;
                public uint time;
                public IntPtr dwExtraInfo;
            }
    
            [Flags]
            enum MouseEventFlags : uint
            {
                MOUSEEVENTF_MOVE = 0x0001
            }
            enum SendInputEventType : int
            {
                InputMouse
            }
            public static void MoveMouseBy(int x, int y) {
                INPUT mouseInput = new INPUT();
                mouseInput.type = SendInputEventType.InputMouse;
                mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE;
                mouseInput.mkhi.mi.dx = x;
                mouseInput.mkhi.mi.dy = y;
                SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));
            }
        }
    }
    '@
    
    $checkIntervalInSeconds = 30
    $preventIdleLimitInSeconds = 180
    
    while($True) {
        if (([PInvoke.Win32.UserInput]::IdleSeconds -ge $preventIdleLimitInSeconds)) {
            [MouseMover.MouseSimulator]::MoveMouseBy(10,0)
            [MouseMover.MouseSimulator]::MoveMouseBy(-10,0)
        }
        Start-Sleep -Seconds $checkIntervalInSeconds
    }
    

    Then, open Windows PowerShell and run

    powershell -ExecutionPolicy ByPass -File C:\SCRIPT-DIRECTORY-PATH\preventIdle.ps1
    

提交回复
热议问题