Author Topic: How to: Simulate Mouse and Keyboard Events in Code  (Read 9086 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
How to: Simulate Mouse and Keyboard Events in Code
« on: February 12, 2009, 02:28:19 PM »
How to: Simulate Mouse and Keyboard Events in Code 


Windows Forms provides several options for programmatically simulating mouse and keyboard input. This topic provides an overview of these options.

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
Create a MouseEventArgs whose Button property is set to the System.Windows.Forms.MouseButtons.Right value.

Call the OnMouseClick method with this MouseEventArgs as the argument.

For more information on custom controls, see Developing Windows Forms Controls at Design Time.

There are other ways to simulate mouse input. For example, you can programmatically set a control property that represents a state that is typically set through mouse input (such as the Checked property of the CheckBox control), or you can directly call the delegate that is attached to the event you want to simulate.

Simulating Keyboard Input
Although you can simulate keyboard input by using the strategies discussed above for mouse input, Windows Forms also provides the SendKeys class for sending keystrokes to the active application.

Caution 
If your application is intended for international use with a variety of keyboards, the use of System.Windows.Forms.SendKeys.Send(System.String) could yield unpredictable results and should be avoided.
 

Note 
The SendKeys class has been updated for the .NET Framework 3.0 to enable its use in applications that run on Windows Vista. The enhanced security of Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected.

The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.

If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

Code: [Select]
<appSettings>

<add key="SendKeys" value="SendInput"/>

</appSettings>

To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.
 

To send a keystroke to the same application
Call the Send or SendWait method of the SendKeys class. The specified keystrokes will be received by the active control of the application. The following code example uses Send to simulate pressing the ENTER key when the user double-clicks the surface of the form. This example assumes a Form with a single Button control that has a tab index of 0.

Code: [Select]
// Send a key to the button when the user double-clicks anywhere
// on the form.
private void Form1_DoubleClick(object sender, EventArgs e)
{
    // Send the enter key to the button, which raises the click
    // event for the button. This works because the tab stop of
    // the button is 0.
    SendKeys.Send("{ENTER}");
}


To send a keystroke to a different application
Activate the application window that will receive the keystrokes, and then call the Send or SendWait method. Because there is no managed method to activate another application, you must use native Windows methods to force focus on other applications. The following code example uses platform invoke to call the FindWindow and SetForegroundWindow methods to activate the Calculator application window, and then calls SendWait to issue a series of calculations to the Calculator application.

Code: [Select]
// Get a handle to an application window.
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName,
    string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
    // Get a handle to the Calculator application. The window class
    // and window name were obtained using the Spy++ tool.
    IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");

    // Verify that Calculator is a running process.
    if (calculatorHandle == IntPtr.Zero)
    {
        MessageBox.Show("Calculator is not running.");
        return;
    }

    // Make Calculator the foreground application and send it
    // a set of calculations.
    SetForegroundWindow(calculatorHandle);
    SendKeys.SendWait("111");
    SendKeys.SendWait("*");
    SendKeys.SendWait("11");
    SendKeys.SendWait("=");
}


Example
The following code example is the complete application for the previous code examples.

Code: [Select]
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;

namespace SimulateKeyPress
{
    class Form1 : Form
    {
        private Button button1 = new Button();

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            button1.Location = new Point(10, 10);
            button1.TabIndex = 0;
            button1.Text = "Click to automate Calculator";
            button1.AutoSize = true;
            button1.Click += new EventHandler(button1_Click);

            this.DoubleClick += new EventHandler(Form1_DoubleClick);
            this.Controls.Add(button1);
        }

        // Get a handle to an application window.
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        // Send a series of key presses to the Calculator application.
        private void button1_Click(object sender, EventArgs e)
        {
            // Get a handle to the Calculator application. The window class
            // and window name were obtained using the Spy++ tool.
            IntPtr calculatorHandle = FindWindow("SciCalc", "Calculator");

            // Verify that Calculator is a running process.
            if (calculatorHandle == IntPtr.Zero)
            {
                MessageBox.Show("Calculator is not running.");
                return;
            }

            // Make Calculator the foreground application and send it
            // a set of calculations.
            SetForegroundWindow(calculatorHandle);
            SendKeys.SendWait("111");
            SendKeys.SendWait("*");
            SendKeys.SendWait("11");
            SendKeys.SendWait("=");
        }

        // Send a key to the button when the user double-clicks anywhere
        // on the form.
        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            // Send the enter key to the button, which raises the click
            // event for the button. This works because the tab stop of
            // the button is 0.
            SendKeys.Send("{ENTER}");
        }
    }
}

« Last Edit: February 12, 2009, 02:30:22 PM by admin »