• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

C# how can I make the keyboard to type without having any user intereaction?

I am writing a data port tool where the only way to get the old images out of the old app is by exporting them to a physical location.  I found a MouseAPI and in my c# app, I  basically call the MouseAPI and have the click to occur on certain window location so the issue is when I get to the export screen, the pop up dialog comes up and I need to give the image a name before exporting ... the cursor is in the textbox, but I have to dynamically put something in the textbox.  Is there a KEYBOARDAPI that I can call that can dynamically type ????

So basically how can I make the keyboard to type without having any user intereaction?

1 Solution
Have you tried SendKeys.Send(...)?
SendKeys.Send(...) will work only for YOUR application not for other application, in that case you need to use API functions
To be precise, SendKeys.Send( ... ) on its own will work only with your application and in order to make it working with other applications you have to use API functions...

Before we get to coding, a couple of words. In the example below, you'll have to have your notepad open, no documents - nothing, just double-click it, that's it. The code will place two lines into notepad and close it, saving a new file into "c:\test.txt" - that should give you a fair idea of how to implement your task.

You will need to get class name and windows name of the application your trying to get access of. If you don't know it yet, the easiest way would be using Spy++ in VS.NET - let me know if your'e not sure how to and I'll give you a short guide...

Anyway, the coding:

using System.Runtime.InteropServices;

// in your class declarations
// 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 );

// anywhere you need it and want it, i linked it to a button pressed
private void button1_Click(object sender, System.EventArgs e)
   // get a handle to the notepad application.
   IntPtr notepadHandle = FindWindow( "Notepad", "Untitled - Notepad" );

   // verify that calculator is a running process
   if( notepadHandle == IntPtr.Zero )
      MessageBox.Show( "notepad is not running" );

   // make notepad the foreground application and send it
   // a set of calculations
   SetForegroundWindow( notepadHandle );                      
   SendKeys.SendWait( "Typing some message in here" );  // type the fist line
   SendKeys.SendWait( "{ENTER}" );                                // press enter
   SendKeys.SendWait( "And another line in here" );          // type the next line
   SendKeys.SendWait( "%{F4}" );                                   // press ALT + F4 to close
   SendKeys.SendWait( "{ENTER}" );                                // YES to save
   SendKeys.SendWait( @"c:\temp.txt" );                          // specify location
   SendKeys.SendWait( "{ENTER}" );                                // press enter the last time

You may also want to check the solution:


which has an example of some keyboard functions implemented in C# using standard Win32 API..

Caner ELCI
Jenny12345Author Commented:

Sendkeys worked to enter a name for the image.  - thank you!

  Now I am facing the issue of how can I get the info from the location that I clicked on.  I need to get the user first name, last name when I click on the grid control.  Also I need to loop through each image to export the image, and how would I know that there are no more images left to export same issue with the user names how would I know that I got all the user names?  Basically I need some sort of a message back from my click actions that I can check to see if I am at the end my loop.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now