[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • 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?

-thanks
0
Jenny12345
Asked:
Jenny12345
1 Solution
 
ptmcompCommented:
Have you tried SendKeys.Send(...)?
0
 
YurichCommented:
SendKeys.Send(...) will work only for YOUR application not for other application, in that case you need to use API functions
0
 
YurichCommented:
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" );
      return;
   }

   // 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
}

regards
0
 
caner_elciCommented:
You may also want to check the solution:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20943540.html#10856081

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

Caner ELCI
0
 
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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