how to use SendInput in .net?

mc 4,981 Reputation points
2024-07-30T05:27:45.38+00:00

How to use SendInput to send mouse click event?

this is my code:

[DllImport("user32.dll")]
public extern static uint SendInput(uint inputs, INPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
    public nint type;
    //public MOUSEINPUT mi;
    //public KEYBDINPUT ki;
    //public HARDWAREINPUT hi;
    public InputUnion U;
}
[StructLayout(LayoutKind.Explicit)]
public struct InputUnion
{
    [FieldOffset(0)]
    public MOUSEINPUT mi;
    [FieldOffset(0)]
    public KEYBDINPUT ki;
    [FieldOffset(0)]
    public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
    public long dx;
    public long dy;
    public nint mouseData;
    public nint dwFlags;
    public nint time;
    public nint dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
    public nint wVk;
    public nint wScan;
    public nint dwFlags;
    public nint time;
    public nint dwExtraInfo;
}
public struct HARDWAREINPUT
{
}
_ = SendInput(1, input, Marshal.SizeOf(input[0]));
var input = new INPUT[1];
input[0].type = 0;
input[0].U.mi.dwFlags = 0x0002;
await Task.Delay(300);


but nothing happend. I have moved the cursor to textbox of webbrowser but it not clicked.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,654 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,009 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
340 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,182 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,441 Reputation points Microsoft Vendor
    2024-07-30T08:52:25.3+00:00

    Hi @mc , Welcome to Microsoft Q&A,

    You need to set the appropriate dwFlags in the MOUSEINPUT structure for mouse down and mouse up events. Additionally, ensure the dx and dy fields are set to 0 if you don't want to move the mouse cursor.

    using System;
    using System.Runtime.InteropServices;
    using System.Threading.Tasks;
    
    public class Program
    {
        [DllImport("user32.dll")]
        public extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
    
        [StructLayout(LayoutKind.Sequential)]
        public struct INPUT
        {
            public int type;
            public InputUnion U;
        }
    
        [StructLayout(LayoutKind.Explicit)]
        public struct InputUnion
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public KEYBDINPUT ki;
            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct HARDWAREINPUT
        {
            public uint uMsg;
            public ushort wParamL;
            public ushort wParamH;
        }
    
        private const int INPUT_MOUSE = 0;
        private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const uint MOUSEEVENTF_LEFTUP = 0x0004;
    
        public static async Task Main(string[] args)
        {
            // Simulate a left mouse button click
            var input = new INPUT[2];
    
            // Mouse down
            input[0] = new INPUT
            {
                type = INPUT_MOUSE,
                U = new InputUnion
                {
                    mi = new MOUSEINPUT
                    {
                        dwFlags = MOUSEEVENTF_LEFTDOWN,
                    }
                }
            };
    
            // Mouse up
            input[1] = new INPUT
            {
                type = INPUT_MOUSE,
                U = new InputUnion
                {
                    mi = new MOUSEINPUT
                    {
                        dwFlags = MOUSEEVENTF_LEFTUP,
                    }
                }
            };
    
            // Send the input
            SendInput((uint)input.Length, input, Marshal.SizeOf(typeof(INPUT)));
    
            // Optional delay
            await Task.Delay(300);
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.