SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4.  
  5. namespace WindowsIdleTime
  6. {
  7.     class Program
  8.     {
  9.         [DllImport("user32.dll", SetLastError = true)]
  10.         static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
  11.  
  12.         [DllImport("user32.dll", SetLastError = false)]
  13.         private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  14.  
  15.         [DllImport("user32.dll")]
  16.         [return: MarshalAs(UnmanagedType.Bool)]
  17.         public static extern bool GetCursorPos(out POINT p);
  18.  
  19.         [StructLayout(LayoutKind.Sequential)]
  20.         struct LASTINPUTINFO
  21.         {
  22.             public uint cbSize;
  23.             public int dwTime;
  24.         }
  25.  
  26.         [StructLayout(LayoutKind.Sequential)]
  27.         public struct POINT
  28.         {
  29.             public int x;
  30.             public int y;
  31.         }
  32.  
  33.         [StructLayout(LayoutKind.Sequential)]
  34.         struct INPUT
  35.         {
  36.             public SendInputEventType type;
  37.             public MouseKeybdhardwareInputUnion mkhi;
  38.         }
  39.         [StructLayout(LayoutKind.Explicit)]
  40.         struct MouseKeybdhardwareInputUnion
  41.         {
  42.             [FieldOffset(0)]
  43.             public MouseInputData mi;
  44.  
  45.             [FieldOffset(0)]
  46.             public KEYBDINPUT ki;
  47.  
  48.             [FieldOffset(0)]
  49.             public HARDWAREINPUT hi;
  50.         }
  51.  
  52.         [StructLayout(LayoutKind.Sequential)]
  53.         struct KEYBDINPUT
  54.         {
  55.             public ushort wVk;
  56.             public ushort wScan;
  57.             public uint dwFlags;
  58.             public uint time;
  59.             public IntPtr dwExtraInfo;
  60.         }
  61.  
  62.         [StructLayout(LayoutKind.Sequential)]
  63.         struct HARDWAREINPUT
  64.         {
  65.             public int uMsg;
  66.             public short wParamL;
  67.             public short wParamH;
  68.         }
  69.  
  70.         struct MouseInputData
  71.         {
  72.             public int dx;
  73.             public int dy;
  74.             public uint mouseData;
  75.             public MouseEventFlags dwFlags;
  76.             public uint time;
  77.             public IntPtr dwExtraInfo;
  78.         }
  79.  
  80.         [Flags]
  81.         enum MouseEventFlags : uint
  82.         {
  83.             MOUSEEVENT_MOVE = 0x0001,
  84.             MOUSEEVENT_LEFTDOWN = 0x0002,
  85.             MOUSEEVENT_LEFTUP = 0x0004,
  86.             MOUSEEVENT_RIGHTDOWN = 0x0008,
  87.             MOUSEEVENT_RIGHTUP = 0x0010,
  88.             MOUSEEVENT_MIDDLEDOWN = 0x0020,
  89.             MOUSEEVENT_MIDDLEUP = 0x0040,
  90.             MOUSEEVENT_XDOWN = 0x0080,
  91.             MOUSEEVENT_XUP = 0x0100,
  92.             MOUSEEVENT_WHEEL = 0x0800,
  93.             MOUSEEVENT_VIRTUALDESK = 0x4000,
  94.             MOUSEEVENT_ABSOLUTE = 0x8000
  95.         }
  96.         enum SendInputEventType : int
  97.         {
  98.             InputMouse,
  99.             InputKeyboard,
  100.             InputHardware
  101.         }
  102.  
  103.  
  104.         private static DateTime LastInput
  105.         {
  106.             get
  107.             {
  108.                 DateTime boolTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
  109.                 DateTime lastInput = boolTime.AddMilliseconds(LastInputTicks);
  110.                 return lastInput;
  111.             }
  112.         }
  113.  
  114.         private static TimeSpan IdleTime
  115.         {
  116.             get
  117.             {
  118.                 return DateTime.UtcNow.Subtract(LastInput);
  119.             }
  120.         }
  121.  
  122.         private static int LastInputTicks {
  123.             get
  124.             {
  125.                 LASTINPUTINFO lii = new LASTINPUTINFO();
  126.                 lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
  127.                 GetLastInputInfo(ref lii);
  128.                 return lii.dwTime;
  129.             }
  130.         }
  131.  
  132.         static void Main(string[] args)
  133.         {
  134.             Random r = new Random();
  135.             while (true)
  136.             {
  137.                 MouseMove(0, 0);
  138.                 Console.WriteLine(Math.Round(IdleTime.TotalSeconds));
  139.                 Thread.Sleep(r.Next(100, 10000));                      
  140.             }
  141.         }
  142.  
  143.         private static void ClickLeftMouseButton()
  144.         {
  145.             INPUT mouseDownInput = new INPUT();
  146.             mouseDownInput.type = SendInputEventType.InputMouse;
  147.             mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTDOWN;
  148.             SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
  149.  
  150.             INPUT mouseUpInput = new INPUT();
  151.             mouseUpInput.type = SendInputEventType.InputMouse;
  152.             mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTUP;
  153.             SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
  154.         }
  155.         private static void ClickRightMouseButton()
  156.         {
  157.             INPUT mouseDownInput = new INPUT();
  158.             mouseDownInput.type = SendInputEventType.InputMouse;
  159.             mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTDOWN;
  160.             SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
  161.  
  162.             INPUT mouseUpInput = new INPUT();
  163.             mouseUpInput.type = SendInputEventType.InputMouse;
  164.             mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTUP;
  165.             SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
  166.         }
  167.  
  168.         private static void MouseMove(int dx, int dy)
  169.         {
  170.             INPUT mouseMove = new INPUT();
  171.             mouseMove.type = SendInputEventType.InputMouse;
  172.             mouseMove.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_MOVE;
  173.             mouseMove.mkhi.mi.dx = dx;
  174.             mouseMove.mkhi.mi.dy = dy;
  175.             SendInput(1, ref mouseMove, Marshal.SizeOf(new INPUT()));
  176.         }
  177.     }
  178. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top