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

Posted on 2005-04-12
Last Modified: 2007-12-19
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?

Question by:Jenny12345
    LVL 10

    Expert Comment

    Have you tried SendKeys.Send(...)?
    LVL 21

    Expert Comment

    SendKeys.Send(...) will work only for YOUR application not for other application, in that case you need to use API functions
    LVL 21

    Accepted Solution

    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

    LVL 4

    Expert Comment

    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

    Author Comment


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now