ARTICLE

Send Keys to Application Programmatically Using C#

Posted by Jean Paul Articles | SharePoint 2010 December 12, 2012
In this article we can explore a scenario I have encountered while working with SharePoint.
Reader Level:

In this article we can explore a scenario I have encountered while working with SharePoint.

Image 1.jpg

Scenario

My client had a Production issue where the Request Tracking report is too huge in size and cannot be downloaded. Although the problem is fixed, we need to test this in our UAT environment.

To test this we need 1000 requests logged in the SharePoint site. How to create the 1000 requests?

I had the following interesting ideas & solutions.

Idea 1: Create a SQL Stored Procedure that inserts 1000 records to the Content Database.

Problems: Content Database access issue, direct modification is not recommended.

Idea 2: Create a web part that inserts records into the Audit Log using the Server Object Model.

Problems: Some of the fields will remain null.

Idea 3: Click the browser refresh button 1000 times.

Problems: Too hectic for a human to perform

Idea 4: Place a lock on the F5 key.

Problems: The site requires 3 seconds to load, so skips all the key requests if continuous.

Idea 5: Create a Web Client automation.

Problems: The site is under security proxies. So the URL automation is not possible.

Idea 6: Send Keys to Browser.

Action: Feasible

I selected the Idea 6 as the site is authenticated in a browser window. Through a separate windows application we can send Refresh keys (F5) at regular intervals using the Windows API.

The Windows API invocation can be done in .Net using the Platform Invoke infrastructure.

The Steps

The following are the steps involved in the application.

  1. Find the Window Handle using the Title

    a. For example: "Bing - Windows Internet Explorer" is the title of Internet Explorer when opening the website http://www.bing.com
     
  2. Make the Window as the Active Window

    a. Use the SetForegroundWindow API method to make the window active, so that we can send keys to the active window
     
  3. Send the key to the window

    a. Send Key (F5) to the active window
     
  4. Repeat the loop

    a. Continue the loop with an interval specified by the user.

The Code

The following are the Windows API methods required by our application.

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]

public static extern IntPtr FindWindow (IntPtr ZeroOnly, string lpWindowName);

 

[DllImport("user32.dll")]

public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);

 

[DllImport("user32.dll")]

[return: MarshalAs(UnmanagedType.Bool)]

static extern bool SetForegroundWindow(IntPtr hWnd);

If the _pointer has a value greater than 0, we have a valid Handle.

Note

A handle (HWND) is the return value from CreateWindowEx which the Windows Operating System uses to identify a window.

The UI

Create a Windows Forms application and set the following controls.

Image 2.jpg

On the start button click add the following code.
 

private void StartButton_Click(object sender, EventArgs e)

{

    Start();

}

 

private bool _start;

private int _count;

 

private void Start()

{

    _start = true;

    StartButton.Enabled = false;

    StopButton.Enabled = true;

 

    SetForegroundWindow(_pointer);

 

    while (_start)

    {

        SendKeys();

 

        Thread.Sleep(int.Parse(textBox3.Text) * 1000);

        Application.DoEvents();

    }

}

 

public void SendKeys()

{

    int key = int.Parse(KeyText.Text);

 

    const int KEYEVENTF_KEYUP = 0x0002;

 

    keybd_event((byte)key, 0, 0, IntPtr.Zero);

    keybd_event((byte)key, 0, KEYEVENTF_KEYUP, IntPtr.Zero);

}

 

// The keybd_event() method SENDS the key

 

The Implementation

Open a browser and browse to our SharePoint site. Ensure you have logged in correctly.

Image 3.jpg

Now execute our application and enter the title of the Browser in the application. In this case it is: "Reports - All Documents - Windows Internet Explorer"

Image 4.jpg

Enter the Key value. For F5 key, it is ASCII 116. Set the Interval as 4 seconds which will create around 900 requests per hour.

After setting the values click the Start button. You will see that the browser is made the foreground window and the Refresh process repeats every 4 seconds. (Observe the Refresh of browser status bar)

References

http://tinyurl.com/sp2010-winapi

Summary

In this article we have explored a Send Key scenario with the Windows API as the solution. You can comment on better ideas for the problem.

The source code for the article is attached along with.

Article Extensions
Contents added by vijay penmetsa on May 06, 2013
can I have source code for this article please, Thanks.
COMMENT USING