none
imitate mouseclick RRS feed

  • Question

  • I have searched and could not find an answer to my question. Is there a way to send a left or right mouse click in code? I DON'T want to check if there has been a click. I just want to send a left click in code without having to physically click the left mouse button. Thanks
    Sunday, August 16, 2009 8:45 PM

Answers

    • Marked as answer by Harry Zhu Friday, August 21, 2009 6:44 AM
    Sunday, August 16, 2009 9:50 PM
  • using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
    	class Program
    	{
    		public class SendInputClass
    		{
    
    			//C# signature for "SendInput()"
    			[DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
    			static extern uint SendInput(
    				uint nInputs, 
    				INPUT[] pInputs, 
    				int cbSize);
    
    			//C# signature for "GetMessageExtraInfo()"
    			[DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
    			static extern IntPtr GetMessageExtraInfo();
    
    			private enum InputType
    			{
    				INPUT_MOUSE = 0,
    				INPUT_KEYBOARD = 1,
    				INPUT_HARDWARE = 2,
    			}
    
    			[Flags()]
    			private enum MOUSEEVENTF
    			{
    				MOVE = 0x0001,  // mouse move 
    				LEFTDOWN = 0x0002,  // left button down
    				LEFTUP = 0x0004,  // left button up
    				RIGHTDOWN = 0x0008,  // right button down
    				RIGHTUP = 0x0010,  // right button up
    				MIDDLEDOWN = 0x0020,  // middle button down
    				MIDDLEUP = 0x0040,  // middle button up
    				XDOWN = 0x0080,  // x button down 
    				XUP = 0x0100,  // x button down
    				WHEEL = 0x0800,  // wheel button rolled
    				VIRTUALDESK = 0x4000,  // map to entire virtual desktop
    				ABSOLUTE = 0x8000,  // absolute move
    			}
    
    			[Flags()]
    			private enum KEYEVENTF
    			{
    				EXTENDEDKEY = 0x0001,
    				KEYUP = 0x0002,
    				UNICODE = 0x0004,
    				SCANCODE = 0x0008,
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct MOUSEINPUT
    			{
    				public int dx;
    				public int dy;
    				public int mouseData;
    				public int dwFlags;
    				public int time;
    				public IntPtr dwExtraInfo;
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct KEYBDINPUT
    			{
    				public short wVk;
    				public short wScan;
    				public int dwFlags;
    				public int time;
    				public IntPtr dwExtraInfo;
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct HARDWAREINPUT
    			{
    				public int uMsg;
    				public short wParamL;
    				public short wParamH;
    			}
    
    			[StructLayout(LayoutKind.Explicit)]
    			private struct INPUT
    			{
    				[FieldOffset(0)]
    				public int type;
    				[FieldOffset(4)]
    				public MOUSEINPUT mi;
    				[FieldOffset(4)]
    				public KEYBDINPUT ki;
    				[FieldOffset(4)]
    				public HARDWAREINPUT hi;
    			}
    
    			
    			// This function simulates a simple mouseclick at the current cursor position.
    			public static uint Click()
    			{
    				INPUT input_down = new INPUT();
    				input_down.mi.dx = 0;
    				input_down.mi.dy = 0;
    				input_down.mi.mouseData = 0;
    				input_down.mi.dwFlags = (int)MOUSEEVENTF.LEFTDOWN;
    
    				INPUT input_up = input_down;
    				input_up.mi.dwFlags = (int)MOUSEEVENTF.LEFTUP;
    
    				INPUT[] input = { input_down, input_up };
    
    				return SendInput(2, input, Marshal.SizeOf(input_down));
    			}
    		}   
    		 
    		
    	   static void Main(string[] args)
    	   {
    //send click SendInputClass.Click(); } } }
    Hope that helps :)
    Life would have been much easier if I had the source-code !!
    • Proposed as answer by sanky38 Monday, August 17, 2009 4:30 AM
    • Marked as answer by Harry Zhu Friday, August 21, 2009 6:44 AM
    Monday, August 17, 2009 4:09 AM

All replies

    • Marked as answer by Harry Zhu Friday, August 21, 2009 6:44 AM
    Sunday, August 16, 2009 9:50 PM
  • using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
    	class Program
    	{
    		public class SendInputClass
    		{
    
    			//C# signature for "SendInput()"
    			[DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
    			static extern uint SendInput(
    				uint nInputs, 
    				INPUT[] pInputs, 
    				int cbSize);
    
    			//C# signature for "GetMessageExtraInfo()"
    			[DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
    			static extern IntPtr GetMessageExtraInfo();
    
    			private enum InputType
    			{
    				INPUT_MOUSE = 0,
    				INPUT_KEYBOARD = 1,
    				INPUT_HARDWARE = 2,
    			}
    
    			[Flags()]
    			private enum MOUSEEVENTF
    			{
    				MOVE = 0x0001,  // mouse move 
    				LEFTDOWN = 0x0002,  // left button down
    				LEFTUP = 0x0004,  // left button up
    				RIGHTDOWN = 0x0008,  // right button down
    				RIGHTUP = 0x0010,  // right button up
    				MIDDLEDOWN = 0x0020,  // middle button down
    				MIDDLEUP = 0x0040,  // middle button up
    				XDOWN = 0x0080,  // x button down 
    				XUP = 0x0100,  // x button down
    				WHEEL = 0x0800,  // wheel button rolled
    				VIRTUALDESK = 0x4000,  // map to entire virtual desktop
    				ABSOLUTE = 0x8000,  // absolute move
    			}
    
    			[Flags()]
    			private enum KEYEVENTF
    			{
    				EXTENDEDKEY = 0x0001,
    				KEYUP = 0x0002,
    				UNICODE = 0x0004,
    				SCANCODE = 0x0008,
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct MOUSEINPUT
    			{
    				public int dx;
    				public int dy;
    				public int mouseData;
    				public int dwFlags;
    				public int time;
    				public IntPtr dwExtraInfo;
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct KEYBDINPUT
    			{
    				public short wVk;
    				public short wScan;
    				public int dwFlags;
    				public int time;
    				public IntPtr dwExtraInfo;
    			}
    
    			[StructLayout(LayoutKind.Sequential)]
    			private struct HARDWAREINPUT
    			{
    				public int uMsg;
    				public short wParamL;
    				public short wParamH;
    			}
    
    			[StructLayout(LayoutKind.Explicit)]
    			private struct INPUT
    			{
    				[FieldOffset(0)]
    				public int type;
    				[FieldOffset(4)]
    				public MOUSEINPUT mi;
    				[FieldOffset(4)]
    				public KEYBDINPUT ki;
    				[FieldOffset(4)]
    				public HARDWAREINPUT hi;
    			}
    
    			
    			// This function simulates a simple mouseclick at the current cursor position.
    			public static uint Click()
    			{
    				INPUT input_down = new INPUT();
    				input_down.mi.dx = 0;
    				input_down.mi.dy = 0;
    				input_down.mi.mouseData = 0;
    				input_down.mi.dwFlags = (int)MOUSEEVENTF.LEFTDOWN;
    
    				INPUT input_up = input_down;
    				input_up.mi.dwFlags = (int)MOUSEEVENTF.LEFTUP;
    
    				INPUT[] input = { input_down, input_up };
    
    				return SendInput(2, input, Marshal.SizeOf(input_down));
    			}
    		}   
    		 
    		
    	   static void Main(string[] args)
    	   {
    //send click SendInputClass.Click(); } } }
    Hope that helps :)
    Life would have been much easier if I had the source-code !!
    • Proposed as answer by sanky38 Monday, August 17, 2009 4:30 AM
    • Marked as answer by Harry Zhu Friday, August 21, 2009 6:44 AM
    Monday, August 17, 2009 4:09 AM
  • Hello. I have been searching for some time now to find a way to send mouse clicks to another application not using mouse x;y coordinates, and so far I have not yet arrived at any solution (either that posts/examples are not even compilable w/o info to proceed, nor the info is not there to get me started).  I am, like the user who posted this question, would like to send mouse clicks to another application (is not a .net application).  I am not all that C# guru, my background is embedded systems.

    I am starting the application that I want to send mouse clicks to via
    startInfo = new ProcessStartInfo(@"C:\Code\abc.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    hence, the application receiving the mouse clicks does not have focus and is running as hidden. I am also using WinID program to get info about the buttons I want to send mouse clicks.  Can anyone provide an end-to-end solution for this problem? I have tried many different ways for a few days now and have not reached a conclusion. Your help or direction is greatly appreciated, and if you happened to be in the Detroit area, I am willing to buy you lunch :D

    Is my current way even possible? i.e running application in hidden style and sending mouse events.
    (btw. I am using Visual Express2008 with .NET 3.5 SP1. )

    thanks
    Wednesday, September 30, 2009 6:33 PM