Advertisement
Guest User

NoSleep.ps1

a guest
Jan 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $code = @"
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5.  
  6. public static class KBEmulator {    
  7.    public enum InputType : uint {
  8.        INPUT_MOUSE = 0,
  9.        INPUT_KEYBOARD = 1,
  10.        INPUT_HARDWARE = 3
  11.    }
  12.  
  13.    [Flags]
  14.    internal enum KEYEVENTF : uint
  15.    {
  16.        KEYDOWN = 0x0,
  17.        EXTENDEDKEY = 0x0001,
  18.        KEYUP = 0x0002,
  19.        SCANCODE = 0x0008,
  20.        UNICODE = 0x0004
  21.    }
  22.  
  23.    [Flags]
  24.    internal enum MOUSEEVENTF : uint
  25.    {
  26.        ABSOLUTE = 0x8000,
  27.        HWHEEL = 0x01000,
  28.        MOVE = 0x0001,
  29.        MOVE_NOCOALESCE = 0x2000,
  30.        LEFTDOWN = 0x0002,
  31.        LEFTUP = 0x0004,
  32.        RIGHTDOWN = 0x0008,
  33.        RIGHTUP = 0x0010,
  34.        MIDDLEDOWN = 0x0020,
  35.        MIDDLEUP = 0x0040,
  36.        VIRTUALDESK = 0x4000,
  37.        WHEEL = 0x0800,
  38.        XDOWN = 0x0080,
  39.        XUP = 0x0100
  40.    }
  41.  
  42.    // Master Input structure
  43.    [StructLayout(LayoutKind.Sequential)]
  44.    public struct lpInput {
  45.        internal InputType type;
  46.        internal InputUnion Data;
  47.        internal static int Size { get { return Marshal.SizeOf(typeof(lpInput)); } }            
  48.    }
  49.  
  50.    // Union structure
  51.    [StructLayout(LayoutKind.Explicit)]
  52.    internal struct InputUnion {
  53.        [FieldOffset(0)]
  54.        internal MOUSEINPUT mi;
  55.        [FieldOffset(0)]
  56.        internal KEYBDINPUT ki;
  57.        [FieldOffset(0)]
  58.        internal HARDWAREINPUT hi;
  59.    }
  60.  
  61.    // Input Types
  62.    [StructLayout(LayoutKind.Sequential)]
  63.    internal struct MOUSEINPUT
  64.    {
  65.        internal int dx;
  66.        internal int dy;
  67.        internal int mouseData;
  68.        internal MOUSEEVENTF dwFlags;
  69.        internal uint time;
  70.        internal UIntPtr dwExtraInfo;
  71.    }
  72.  
  73.    [StructLayout(LayoutKind.Sequential)]
  74.    internal struct KEYBDINPUT
  75.    {
  76.        internal short wVk;
  77.        internal short wScan;
  78.        internal KEYEVENTF dwFlags;
  79.        internal int time;
  80.        internal UIntPtr dwExtraInfo;
  81.    }
  82.  
  83.    [StructLayout(LayoutKind.Sequential)]
  84.    internal struct HARDWAREINPUT
  85.    {
  86.        internal int uMsg;
  87.        internal short wParamL;
  88.        internal short wParamH;
  89.    }
  90.  
  91.    private class unmanaged {
  92.        [DllImport("user32.dll", SetLastError = true)]
  93.        internal static extern uint SendInput (
  94.            uint cInputs,
  95.            [MarshalAs(UnmanagedType.LPArray)]
  96.            lpInput[] inputs,
  97.            int cbSize
  98.        );
  99.  
  100.    }
  101.  
  102.    internal static uint SendInput(uint cInputs, lpInput[] inputs, int cbSize) {
  103.        return unmanaged.SendInput(cInputs, inputs, cbSize);
  104.    }
  105.  
  106.    public static void SendScanCode(short scanCode) {
  107.        
  108.        lpInput[] KeyInputs = new lpInput[1];
  109.        lpInput KeyInput = new lpInput();
  110.  
  111.        // Generic Keyboard Event
  112.        KeyInput.type = InputType.INPUT_KEYBOARD;
  113.        KeyInput.Data.ki.wScan = 0;
  114.        KeyInput.Data.ki.time = 0;
  115.        KeyInput.Data.ki.dwExtraInfo = UIntPtr.Zero;
  116.  
  117.  
  118.        // Push the correct key
  119.        //KeyInput.Data.ki.wVk = scanCode;
  120.        //KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYDOWN;
  121.        //KeyInputs[0] = KeyInput;
  122.        //SendInput(1, KeyInputs, lpInput.Size);
  123.  
  124.        // Release the key
  125.        KeyInput.Data.ki.dwFlags = KEYEVENTF.KEYUP;
  126.        KeyInputs[0] = KeyInput;
  127.        SendInput(1, KeyInputs, lpInput.Size);
  128.  
  129.        return;
  130.    }
  131.  
  132. }
  133. "@
  134.  
  135. if(([System.AppDomain]::CurrentDomain.GetAssemblies() | ?{$_ -match "KBEmulator"}) -eq $null) {
  136.     Add-Type -TypeDefinition $code
  137. }
  138.  
  139. $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
  140. add-type -name win -member $t -namespace native
  141. [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
  142.  
  143. while( $true ) {
  144.   Start-Sleep -Seconds 60
  145.   [KBEmulator]::SendScanCode(0x07)
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement