Good day! I am writing a program that should emulate user activity. Those. if the user is inactive for some time, then perform some actions, for example, move the mouse.

Found the code that gives in seconds how much the user is inactive

using System.Runtime.InteropServices; .... [StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO)); [MarshalAs(UnmanagedType.U4)] public UInt32 cbSize; [MarshalAs(UnmanagedType.U4)] public UInt32 dwTime; } [DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); static int last() { int t = 0; LASTINPUTINFO l = new LASTINPUTINFO(); l.cbSize = (UInt32)Marshal.SizeOf(l); l.dwTime = 0; int e = Environment.TickCount; if (GetLastInputInfo(ref l)) { int inp = (Int32)l.dwTime; t = e - inp; } return ((t > 0) ? (t / 1000) : 0); } 

And the code that can move the mouse cursor

 [DllImport("user32.dll")] public static extern void SetCursorPos(int x, int y); 

The time of inactivity is given to the norm, the mouse is shifted relative to the old position by one point - but the user activity does not emulate it. What's wrong?

Tell me the direction!

    1 answer 1

    If you need to reset the user "inactivity" timer, then perhaps this is for you:

      [DllImport("CoreDll.dll")] public static extern void SystemIdleTimerReset(); 

    If, nevertheless, it is necessary to precisely move the mouse cursor so as to deceive the activity detector, then you can do it with the help of SendInput , for which there is a ready-made C # open source wrapper . Well, or take an example of calling SendInput for C #:

     public class MouseSimulator { [DllImport("user32.dll", SetLastError = true)] static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); [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 { MOUSEEVENTF_MOVE = 0x0001, MOUSEEVENTF_LEFTDOWN = 0x0002, MOUSEEVENTF_LEFTUP = 0x0004, MOUSEEVENTF_RIGHTDOWN = 0x0008, MOUSEEVENTF_RIGHTUP = 0x0010, MOUSEEVENTF_MIDDLEDOWN = 0x0020, MOUSEEVENTF_MIDDLEUP = 0x0040, MOUSEEVENTF_XDOWN = 0x0080, MOUSEEVENTF_XUP = 0x0100, MOUSEEVENTF_WHEEL = 0x0800, MOUSEEVENTF_VIRTUALDESK = 0x4000, MOUSEEVENTF_ABSOLUTE = 0x8000 } enum SendInputEventType : int { InputMouse, InputKeyboard, InputHardware } public static void ClickLeftMouseButton() { INPUT mouseDownInput = new INPUT(); mouseDownInput.type = SendInputEventType.InputMouse; mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN; SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT())); INPUT mouseUpInput = new INPUT(); mouseUpInput.type = SendInputEventType.InputMouse; mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP; SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT())); } public static void ClickRightMouseButton() { INPUT mouseDownInput = new INPUT(); mouseDownInput.type = SendInputEventType.InputMouse; mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN; SendInput(1, ref mouseDownInput, Marshal.SizeOf(new INPUT())); INPUT mouseUpInput = new INPUT(); mouseUpInput.type = SendInputEventType.InputMouse; mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP; SendInput(1, ref mouseUpInput, Marshal.SizeOf(new INPUT())); } } 

    PS Wonderful thing - Google.

    • Thank! Everything is working! - user3216530
    • @ user3216530 If it works, accept the answer. - Zverev Evgeniy