none
Simulate Mouse and Keyboard Events in Code

    Question

  • Simulating Mouse Input

    The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate. This option is usually possible only within custom controls and forms, because the methods that raise events are protected and cannot be accessed outside the control or form. For example, the following steps illustrate how to simulate clicking the right mouse button in code.

    To programmatically click the right mouse button

    1. Create a MouseEventArgs whose Button property is set to the System.Windows.Forms.MouseButtons.Right value.

    2. Call the OnMouseClick method with this MouseEventArgs as the argument.

    I can`t create de MouseEventArgs ... a try ...

      TimeSpan t = new TimeSpan(DateTime.Now.Ticks);
                int timestamp  = (int) t.TotalSeconds;


                base.OnMouseDown(new MouseButtonEventArgs(mouse, timestamp, MouseButton.Left));
               
    How can I get de MouseDevice instance??
    Friday, April 25, 2008 10:09 PM

Answers

  •  Nicolas Sampietro wrote:

    Simulating Mouse Input

    The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate. This option is usually possible only within custom controls and forms, because the methods that raise events are protected and cannot be accessed outside the control or form. For example, the following steps illustrate how to simulate clicking the right mouse button in code.

    To programmatically click the right mouse button

    1. Create a MouseEventArgs whose Button property is set to the System.Windows.Forms.MouseButtons.Right value.

    2. Call the OnMouseClick method with this MouseEventArgs as the argument.

    I can`t create de MouseEventArgs ... a try ...

      TimeSpan t = new TimeSpan(DateTime.Now.Ticks);
                int timestamp  = (int) t.TotalSeconds;


                base.OnMouseDown(new MouseButtonEventArgs(mouse, timestamp, MouseButton.Left));
               
    How can I get de MouseDevice instance??

     

    Keyboard:

    Keyboard keyBr = new Keyboard ( );

    keyBr.SendKeys ( "{RIGHT}" );  // this will move the cursor in your TextBox (RichtextBox/Notepad) that is an Active Window) to the right and in effet will simulate the right arrow.

     

    You can lookup other keys (the code for simulation) at this enumeration page:

     

    http://msdn2.microsoft.com/en-us/library/microsoft.xna.framework.input.keys.aspx

     

    Also:

     

    http://support.microsoft.com/kb/301583

     

    SendKeys Class

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(vs.71).aspx

     

    or

    SendKeys.Send ( ) Method

     

     

    The code above will work only for Editors, to send a key to low level APIs you will have to use a KeyBoard hook I believe.

     

    I am not sure you need your Mouse to control for that. With the Keyboard class or SendKeys class you can move your cursor anywhere, however if you want to manipulate the Mouse cursor in Control panel for instance, then you can use this set of tricks.

     

    try

    {

    cmdm.ExecuteNonQuery ( );

    this.success++;

    string numb = this.success.ToString ( );

    Console.SetCursorPosition ( Console.CursorLeft, Console.CursorTop );

    Console.Write ( numb );

    Console.SetCursorPosition ( Console.CursorLeft - numb.Length, Console.CursorTop );

    }

    catch

    {

    this.failure += 1;

    string numb2 = this.failure.ToString ( );

    Console.SetCursorPosition ( Console.CursorLeft + 15, Console.CursorTop );

    Console.Write ("F: "+ numb2 );

    Console.SetCursorPosition ( Console.CursorLeft + 15 - numb2.Length, Console.CursorTop );

    }

     

    It is a part of a routine that sends records to a DB table. I want to know how many records get thru and how many will be regected (failed). The reason for failure is that they match a primary key in the table of Sql Server. This code prints a cumulative number (constantly updated in the process)  of all records that got thru and cumulative number of records that failed. They all will appear on the top line of the cmd prompt window and the new number will overlap the other one because the cursor is being constantly returned to the starting position. (separately for Successes and Failures: F); It is like watching the process in a motion picture.

     

    Saturday, April 26, 2008 12:39 AM

All replies

  •  Nicolas Sampietro wrote:

    Simulating Mouse Input

    The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate. This option is usually possible only within custom controls and forms, because the methods that raise events are protected and cannot be accessed outside the control or form. For example, the following steps illustrate how to simulate clicking the right mouse button in code.

    To programmatically click the right mouse button

    1. Create a MouseEventArgs whose Button property is set to the System.Windows.Forms.MouseButtons.Right value.

    2. Call the OnMouseClick method with this MouseEventArgs as the argument.

    I can`t create de MouseEventArgs ... a try ...

      TimeSpan t = new TimeSpan(DateTime.Now.Ticks);
                int timestamp  = (int) t.TotalSeconds;


                base.OnMouseDown(new MouseButtonEventArgs(mouse, timestamp, MouseButton.Left));
               
    How can I get de MouseDevice instance??

     

    Keyboard:

    Keyboard keyBr = new Keyboard ( );

    keyBr.SendKeys ( "{RIGHT}" );  // this will move the cursor in your TextBox (RichtextBox/Notepad) that is an Active Window) to the right and in effet will simulate the right arrow.

     

    You can lookup other keys (the code for simulation) at this enumeration page:

     

    http://msdn2.microsoft.com/en-us/library/microsoft.xna.framework.input.keys.aspx

     

    Also:

     

    http://support.microsoft.com/kb/301583

     

    SendKeys Class

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(vs.71).aspx

     

    or

    SendKeys.Send ( ) Method

     

     

    The code above will work only for Editors, to send a key to low level APIs you will have to use a KeyBoard hook I believe.

     

    I am not sure you need your Mouse to control for that. With the Keyboard class or SendKeys class you can move your cursor anywhere, however if you want to manipulate the Mouse cursor in Control panel for instance, then you can use this set of tricks.

     

    try

    {

    cmdm.ExecuteNonQuery ( );

    this.success++;

    string numb = this.success.ToString ( );

    Console.SetCursorPosition ( Console.CursorLeft, Console.CursorTop );

    Console.Write ( numb );

    Console.SetCursorPosition ( Console.CursorLeft - numb.Length, Console.CursorTop );

    }

    catch

    {

    this.failure += 1;

    string numb2 = this.failure.ToString ( );

    Console.SetCursorPosition ( Console.CursorLeft + 15, Console.CursorTop );

    Console.Write ("F: "+ numb2 );

    Console.SetCursorPosition ( Console.CursorLeft + 15 - numb2.Length, Console.CursorTop );

    }

     

    It is a part of a routine that sends records to a DB table. I want to know how many records get thru and how many will be regected (failed). The reason for failure is that they match a primary key in the table of Sql Server. This code prints a cumulative number (constantly updated in the process)  of all records that got thru and cumulative number of records that failed. They all will appear on the top line of the cmd prompt window and the new number will overlap the other one because the cursor is being constantly returned to the starting position. (separately for Successes and Failures: F); It is like watching the process in a motion picture.

     

    Saturday, April 26, 2008 12:39 AM
  •  

    I also like to know how to simulate a mouse event. To copy information from a help file and then continue about keystrokes is not helpful. Can somebody give a good programming example?
    Monday, July 14, 2008 1:12 AM
  • Precisely what kind of events do you want to emulate? What do you want mouse cursor to do and where? Is it a textbox or another control or a Console?

    Thursday, July 17, 2008 1:00 AM
  • Hi!

    I know, this thread is a year old, but I have been struggling with this exact problem the last days.
    This is my solution:
            public static MouseDevice GetMouseDevice()
            {
                MouseDevice mouseDevice = null;
                Thread thread = new Thread(() => mouseDevice = ((InputManager)GetConstructors(typeof(InputManager))[0].Invoke(new object[] { })).PrimaryMouseDevice);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                while (thread.IsAlive)
                    Thread.Sleep(10);
                return mouseDevice;
            }

    Pretty dirty but it does the trick for a unit test that doesn't care about the MouseDevice instance.
    Saturday, August 22, 2009 6:18 PM
  • Old thread I know, but try:

    Input.InputManager.Current.PrimaryMouseDevice
    Tuesday, March 02, 2010 5:56 AM