4
Reply

Sending key strokes to another application

emilio

emilio

Dec 13 2009 9:57 AM
8.6k


hi i wanted to write a windows application that sends keyboard strokes to another application something like a virtual keyboard.

i researched the subject and read few articles.
i managed to find a code that partially works.

in the sample code above i'm trying to send strokes to a word file.
it workes partially in the first send and then it just stopps doing what it supposed to.

the code is:
// Get a handle to an application window.

[
DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[
DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
IntPtr wordHandle = FindWindow(null, "New Microsoft Word Document.doc - Microsoft Word");
// Verify that word is a running process.
if (wordHandle == IntPtr.Zero)
{
MessageBox.Show("word is not running.");
return;
}

SetForegroundWindow(wordHandle);
SendKeys.SendWait("{a}");
 
any help will be appreciated.

Answers (4)