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.
Loading
Sam HobbsPosted Dec 14, 2009, 2:00 PM
It might be easier and more reliable to send the data more directly.
emilioPosted Dec 14, 2009, 2:42 PM
thanks everyone for the help
Aleksandar IlioskiPosted Dec 13, 2009, 11:17 AM
[DllImport("USER32.DLL",EntryPoint = "FindWindow")]
public static extern IntPtr NajdiProzorec(string lpClassName,
string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
IntPtr wH = NajdiProzorec("BaseWindow_RootWnd", "Player Window");
if (wH== IntPtr.Zero)
{
MessageBox.Show("Winamp is not running.");
System.Diagnostics.Process.Start("C:\\Program Files\\Winamp\\winamp.exe");
return;
}
SetForegroundWindow(wH);
SendKeys.SendWait("v");
SendKeys.SendWait("nul");
SendKeys.SendWait("{ESC}");
SendKeys.SendWait("l");
SendKeys.SendWait("{ESC}");
SendKeys.SendWait("soft");
SendKeys.SendWait("x");
}
the winamp shoud start bilnk at the ritham..
Aleksandar IlioskiPosted Dec 13, 2009, 11:12 AM
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern byte VkKeyScan(char ch);
const uint WM_KEYDOWN = 0x100;
private void button2_Click(object sender, EventArgs e)
{
Process[] procs = Process.GetProcessesByName("Notepad");
foreach (Process proc in procs)
{
// look for untitled notepad window
if (proc.MainWindowTitle == "Untitled - Notepad")
{
// get handle to Notepad's edit window
IntPtr hWnd = FindWindowEx(proc.MainWindowHandle, IntPtr.Zero, "edit", null);
// post "hello" to notepad
string s = "hello";
for (int i = 0; i < s.Length; i++)
{
PostMessage(hWnd, WM_KEYDOWN, VkKeyScan(s[i]), 0);
}
break;
}
}
}
both of them work fine for me.. i don't know why you saing that it is not working..