Skip to content

Instantly share code, notes, and snippets.

@magicsih
Created October 4, 2019 01:40
Show Gist options
  • Save magicsih/955bd8cf46d51b3c95d94d278ded7f1d to your computer and use it in GitHub Desktop.
Save magicsih/955bd8cf46d51b3c95d94d278ded7f1d to your computer and use it in GitHub Desktop.
This gist contains code snippets that let you view IdleTime managed by the Windows OS and initialize it with mouse input.
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsIdleTime
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[DllImport("user32.dll", SetLastError = false)]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT p);
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public uint cbSize;
public int dwTime;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[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;
}
struct MouseInputData
{
public int dx;
public int dy;
public uint mouseData;
public MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[Flags]
enum MouseEventFlags : uint
{
MOUSEEVENT_MOVE = 0x0001,
MOUSEEVENT_LEFTDOWN = 0x0002,
MOUSEEVENT_LEFTUP = 0x0004,
MOUSEEVENT_RIGHTDOWN = 0x0008,
MOUSEEVENT_RIGHTUP = 0x0010,
MOUSEEVENT_MIDDLEDOWN = 0x0020,
MOUSEEVENT_MIDDLEUP = 0x0040,
MOUSEEVENT_XDOWN = 0x0080,
MOUSEEVENT_XUP = 0x0100,
MOUSEEVENT_WHEEL = 0x0800,
MOUSEEVENT_VIRTUALDESK = 0x4000,
MOUSEEVENT_ABSOLUTE = 0x8000
}
enum SendInputEventType : int
{
InputMouse,
InputKeyboard,
InputHardware
}
private static DateTime LastInput
{
get
{
DateTime boolTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
DateTime lastInput = boolTime.AddMilliseconds(LastInputTicks);
return lastInput;
}
}
private static TimeSpan IdleTime
{
get
{
return DateTime.UtcNow.Subtract(LastInput);
}
}
private static int LastInputTicks {
get
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
GetLastInputInfo(ref lii);
return lii.dwTime;
}
}
static void Main(string[] args)
{
Random r = new Random();
while (true)
{
MouseMove(0, 0);
Console.WriteLine(Math.Round(IdleTime.TotalSeconds));
Thread.Sleep(r.Next(100, 10000));
}
}
private static void ClickLeftMouseButton()
{
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = SendInputEventType.InputMouse;
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_LEFTUP;
SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
}
private static void ClickRightMouseButton()
{
INPUT mouseDownInput = new INPUT();
mouseDownInput.type = SendInputEventType.InputMouse;
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTDOWN;
SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT()));
INPUT mouseUpInput = new INPUT();
mouseUpInput.type = SendInputEventType.InputMouse;
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_RIGHTUP;
SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT()));
}
private static void MouseMove(int dx, int dy)
{
INPUT mouseMove = new INPUT();
mouseMove.type = SendInputEventType.InputMouse;
mouseMove.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENT_MOVE;
mouseMove.mkhi.mi.dx = dx;
mouseMove.mkhi.mi.dy = dy;
SendInput(1, ref mouseMove, Marshal.SizeOf(new INPUT()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment