CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Mouse Click

  1. #1
    Join Date
    Feb 2009
    Posts
    112

    Mouse Click

    Hi, I'm trying to make a porgram that will simulate a mouse click. A lot of the answers that I found scouring the internet said use the SendInput API. I'm very new to API's and don't know much about them. I went to MSDN's website to look up how to do it and I was able to import the dll and declare the fuction, but I'm not sure how to implement it into my program.

    http://msdn.microsoft.com/en-us/libr...10(VS.85).aspx

    Code:
            [DllImport("User32.dll")]
            UINT SendInput(UINT nInputs, LPINPUT pInputs, int cbSize);
            private void button2_Click(object sender, EventArgs e)
            {
    
                SendInput(); //This is where I'm not sure what I'm supposed to do
                
            }
    When I call the fuction, I'm not sure what I'm supposed to put for the parameters of it. Any help would be greatly appreciated.

    Thanks in advance.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    here's everything you need http://www.pinvoke.net/default.aspx/...SendInput.html just read the article and do copy&paste ;]
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Feb 2009
    Posts
    112

    Re: Mouse Click

    I'm still really confused. Where does the actual mouse click occur in the code?
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    you'll need this parts:
    Code:
    struct INPUT 
    {
         public int type;
         public MOUSEKEYBDHARDWAREINPUT mkhi;      
    }
    
    [StructLayout(LayoutKind.Explicit)]
    struct MOUSEKEYBDHARDWAREINPUT
    {
         [FieldOffset(0)]  
         public MOUSEINPUT mi;
    
         [FieldOffset(0)]
         public KEYBDINPUT ki;
    
         [FieldOffset(0)]  
         public HARDWAREINPUT hi;
    }
    
    struct MOUSEINPUT 
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
    
    public enum Win32Consts
    {
    	// For use with the INPUT struct, see SendInput for an example
    	public const int INPUT_MOUSE = 0;
    	public const int INPUT_KEYBOARD = 1;
    	public const int INPUT_HARDWARE = 2;
    }
    	
    INPUT structInput;
    structInput = new INPUT();
    structInput.type = Win32Consts.INPUT_MOUSE;
    
    // now you have to setup the MOUSEINPUT stucture
    structInput.mi...
    intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
    (I didn't acutally test it but it's almost complete I think)

    and you should read the SendInput documentation if not already done that so that you understand all the parameters.
    Last edited by memeloo; January 12th, 2010 at 12:27 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Feb 2009
    Posts
    112

    Re: Mouse Click

    I dont' really understand the parameters for sendInput.
    Code:
            [DllImport("user32.dll", SetLastError = true)]
            static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
    I don't understand how "uint nInputs", "ref INPUT pInputs", or "int cbSize" is utilized. Another thing that I can't seem to grasp is where the actual mouse moving is occuring.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  6. #6
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Mouse Click

    Parameters

    nInputs
    [in] Number of structures in the pInputs array.
    pInputs
    [in] Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.
    cbSize
    [in] Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function fails.
    Return Value

    The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread. To get extended error information, call GetLastError.
    The link memeloo gave is to msdn, which describes exactly what each parameter does.




    And to answer your question about how the mouse is moved/clicked:

    INPUT structInput;
    structInput = new INPUT();
    structInput.type = Win32Consts.INPUT_MOUSE;

    // now you have to setup the MOUSEINPUT stucture
    structInput.mi...
    memeloo started the struct initialization for you. You have to figure out the rest. The links in this thread point to exactly what you need and examples of how to use them.
    Last edited by mariocatch; January 12th, 2010 at 11:45 AM. Reason: More examples.

  7. #7
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    Quote Originally Posted by vandel212 View Post
    I dont' really understand the parameters for sendInput.
    Code:
            [DllImport("user32.dll", SetLastError = true)]
            static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
    I don't understand how "uint nInputs", "ref INPUT pInputs", or "int cbSize" is utilized. Another thing that I can't seem to grasp is where the actual mouse moving is occuring.
    it's unbelievable how lazy you are. you didn't even bother to read the links that I had posted.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  8. #8
    Join Date
    Feb 2009
    Posts
    112

    Re: Mouse Click

    Quote Originally Posted by memeloo View Post
    it's unbelievable how lazy you are. you didn't even bother to read the links that I had posted.
    I would like to state before this little rant that I do appreicate the help offered by everyone at codeguru.com. (That quote didn't help me out to much though...)

    I did look at the link you have posted. In fact I have read the msdn link 10 times before I have posted to this forum. I've looked at the pinvoke.net link at least three times since you have pointed it out to me, and tried to implement it into a c# program (unsuccessfully I might add). You see, unlike a lot of people I do research before I go to the forum to ask questions. So please do not make assumptions about me before you know all of the facts. I do not appreciate being insulted just because I'm not making the connection in my head. It happens to everyone. I'm sure all the information is available to me on the links, but it's just not clicking... SORRY. So rather than insulting me either help me or shut the hell up because, as it turns out, calling me lazy doesn't actually make my code work (I would perfer to use other words instead of "hell" but I would like to comply with forum rules as much as I can).

    Now on to my next question.

    Where is cbsize defined in the code? Whenever I try to use Marshal.SizeOf in my code I get an error. I'm not really sure where it comes from. Do I have to define the Marshal class in my code before I call the SendInput?

    Also I run into a problem with this block of code:

    Code:
        public enum Win32Consts
        {  //Says "Identifier expected; 'public' is a keyword"
            // For use with the INPUT struct, see SendInput for an example
            public const int INPUT_MOUSE = 0; //gives an error here as well stating "identifier expected".
            public const int INPUT_KEYBOARD = 1;
            public const int INPUT_HARDWARE = 2;
        }
    Last edited by vandel212; January 13th, 2010 at 10:15 AM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  9. #9
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Mouse Click

    You have visibility and identifiers in your enum values. This is against the c# coding rules, thus why it has no idea what pubilc is in the context of an enum value (nor does it know what const or int are). Also you don't use semicolons after each value of an enum. You use commas.

    Code:
    public enum Win32Consts
    {
        INPUT_MOUSE = 0,
        INPUT_KEYBOARD,
        INPUT_HARDWARE
    }

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    Quote Originally Posted by vandel212 View Post
    I did look at the link you have posted. In fact I have read the msdn link 10 times before I have posted to this forum. I've looked at the pinvoke.net link at least three times since you have pointed it out to me, and tried to implement it into a c# program (unsuccessfully I might add). You see, unlike a lot of people I do research before I go to the forum to ask questions.
    you should have told it before, in your first post, that you did research and that you have read this and this websites... but you don't understand some particular part of code etc. it would have been so much easier to help you

    Quote Originally Posted by vandel212 View Post
    So please do not make assumptions about me before you know all of the facts.
    and whos fault is it that we do now know all the facts?

    Quote Originally Posted by vandel212 View Post
    I do not appreciate being insulted just because I'm not making the connection in my head.
    agian. you didn't told us anything.

    Quote Originally Posted by vandel212 View Post
    So rather than insulting me either help me or shut the hell up because, as it turns out, calling me lazy doesn't actually make my code work (I would perfer to use other words instead of "hell" but I would like to comply with forum rules as much as I can).
    I really wanted to help you but you don't want to cooperate. you don't tell us what you don't understand and if I show you something you say you already know that or read about it.

    what do you exacly expect? should we explain something or do you just want to have a complete code? I have good intentions. and sorry for having insulted you.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  11. #11
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    what about the InputSimulator linked on the pinvoke site that you apparently have read? did you try to run it or to analyze its code?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  12. #12
    Join Date
    Feb 2009
    Posts
    112

    Re: Mouse Click

    Quote Originally Posted by memeloo View Post
    you should have told it before, in your first post, that you did research and that you have read this and this websites... but you don't understand some particular part of code etc. it would have been so much easier to help you
    I didn't just understand some particular part of the code. I didn't understand any of it. Didn't seem like mentioning that would help becuase I would be starting from nothing whether I mentioned it or not.

    Quote Originally Posted by memeloo View Post
    and whos fault is it that we do now know all the facts?
    You accused me of being lazy for not using the links you had posted. Shouldn't you assume I would look at a link after you posted it?

    Here is a tip:
    Ask "Did you look at the links?" before calling a person lazy.

    Quote Originally Posted by memeloo View Post
    I really wanted to help you but you don't want to cooperate. you don't tell us what you don't understand and if I show you something you say you already know that or read about it.
    I don't want to cooperate? You make it sound as if I'm making an active effort to not understand this. That's Ridiculous. Before I posted this I did not understand anything about SendInput. Didn't seem necessary to mention that since a normal person would think that from the start anyway. If you would read through my previous posts you would actually see that I do say what I don't understand.


    Quote Originally Posted by memeloo View Post
    what do you exacly expect? should we explain something or do you just want to have a complete code?
    I know that I'm not a great programmer, but I'm trying to understand this stuff. I do not expect you to write my program for me, I would prefer to get it working on my own, but if you did I would make dam.n sure that I use it to help understand the code better. Not use it then forget about it.

    Quote Originally Posted by memeloo View Post
    I have good intentions. and sorry for having insulted you.
    It's cool. I'll try to be more detailed about what I write.
    Last edited by vandel212; January 13th, 2010 at 04:09 PM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  13. #13
    Join Date
    Feb 2009
    Posts
    112

    Re: Mouse Click

    Quote Originally Posted by memeloo View Post
    what about the InputSimulator linked on the pinvoke site that you apparently have read? did you try to run it or to analyze its code?
    Yes, I did look at it. It seemed interesting, but I could not figure out what I was supposed to do with it.I tried importing the .dll that was in the zip file but that didn't seem to do much. I didn't get past

    Code:
    [DllImport("InputSimulator.dll")]
    Last edited by vandel212; January 14th, 2010 at 10:30 AM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  14. #14
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    it won' work this way. you must add a reference to this dll in your project. right click on the project in the solution explorer and choose Add Reference, then choose the Browse tab and find the dll. now you can use it as any other .net class.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  15. #15
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Mouse Click

    I've build for you a complete example that moves the mouse to the left top corner of the screen and displays the right mouse button menu. hopefully it gives you some idea how to start experimenting for yourself.

    Code:
    class MouseInput
    {
    	public static void RunTest()
    	{ 
    		INPUT[] inputList = new INPUT[3];
    
    		inputList[0].type = (int)INPUT_MOUSE;
    		inputList[0].mi = new MOUSEINPUT()
    		{ 
    			dwFlags = MOUSEEVENTF_ABSOLUTE |  MOUSEEVENTF_MOVE,
    			dx = 0,
    			dy = 0,
    			mouseData = 0,
    			time = 0
    		};
    
    		inputList[1].type = (int)INPUT_MOUSE;
    		inputList[1].mi = new MOUSEINPUT()
    		{
    			dwFlags = MOUSEEVENTF_RIGHTDOWN,
    			dx = 0,
    			dy = 0,
    			mouseData = 0,
    			time = 0
    		};
    
    		inputList[2].type = (int)INPUT_MOUSE;
    		inputList[2].mi = new MOUSEINPUT()
    		{
    			dwFlags = MOUSEEVENTF_RIGHTUP,
    			dx = 0,
    			dy = 0,
    			mouseData = 0,
    			time = 0
    		};
    
    		UInt32 result = SendInput((UInt32)inputList.Length, inputList, Marshal.SizeOf(inputList[0].GetType()));
    		int err = Marshal.GetLastWin32Error();
    	}
    
    	[DllImport("User32.dll", SetLastError=true)]
    	static extern UInt32 SendInput(UInt32 nInputs, INPUT[] pInputs, Int32 cbSize);
    
    	[StructLayout(LayoutKind.Explicit)]
    	struct INPUT
    	{
    		[FieldOffset(0)]
    		public int type;
    
    		[FieldOffset(4)]
    		public MOUSEINPUT mi;
    		
    		[FieldOffset(4)]
    		public KEYBDINPUT ki;
    
    		[FieldOffset(4)]
    		public HARDWAREINPUT hi;
    	}
    
    	[StructLayout(LayoutKind.Sequential)]
    	struct MOUSEINPUT
    	{
    		public int dx;
    		public int dy;
    		public uint mouseData;
    		public uint dwFlags;
    		public uint time;
    		public IntPtr dwExtraInfo;
    	}
    
    	[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;
    	}
    
     
               const int INPUT_MOUSE = 0;
    	const int    INPUT_KEYBOARD = 1;
    	const int INPUT_HARDWARE = 2;
    
    	const uint MOUSEEVENTF_MOVE = 0x0001;
    	const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    	const uint MOUSEEVENTF_LEFTUP = 0x0004;
    	const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    	const uint MOUSEEVENTF_RIGHTUP = 0x0010;
    	const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    	const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
    	const uint MOUSEEVENTF_XDOWN = 0x0080;
    	const uint MOUSEEVENTF_XUP = 0x0100;
    	const uint MOUSEEVENTF_WHEEL = 0x0800;
    	const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
    	const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
    
    }
    if you need help to understand it let us know which code parts are unclear to you.
    Last edited by memeloo; January 14th, 2010 at 01:07 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured