SendKeys.Send instead SendKeys.SendWait
-
Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you
namespace Send_Key
{
class Program
{
[DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
static IntPtr hwnd;static void Main(string\[\] args) { Process notePad = new Process(); notePad.StartInfo.FileName = "notepad.exe"; notePad.Start(); System.Threading.Thread.Sleep(1000); SetForegroundWindow(hWnd); System.Threading.Thread.Sleep(1000); SendKeys.Send("Hello World!"); } }
}
-
Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you
namespace Send_Key
{
class Program
{
[DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
static IntPtr hwnd;static void Main(string\[\] args) { Process notePad = new Process(); notePad.StartInfo.FileName = "notepad.exe"; notePad.Start(); System.Threading.Thread.Sleep(1000); SetForegroundWindow(hWnd); System.Threading.Thread.Sleep(1000); SendKeys.Send("Hello World!"); } }
}
You are not setting NotePad as the ForegroundWindow correctly. Try:
SetForegroundWindow(notePad.MainWindowHandle);
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
Hi, I am trying to send some text to notepad using SendKeys.Send("Hello World!") instead SendKeys.SendWait("Hello World!"). But I could not send and facing problem. Error is: "SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method." Thank you
namespace Send_Key
{
class Program
{
[DllImport("user32.dll", SetLastError = true, EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetForegroundWindow")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "ShowWindow")]
static extern bool ShowWindow(IntPtr hWnd, int cmCommand);
static IntPtr hwnd;static void Main(string\[\] args) { Process notePad = new Process(); notePad.StartInfo.FileName = "notepad.exe"; notePad.Start(); System.Threading.Thread.Sleep(1000); SetForegroundWindow(hWnd); System.Threading.Thread.Sleep(1000); SendKeys.Send("Hello World!"); } }
}
Your
Thread.Sleep
lines can be removed. You don't need them. Also, the numbers you have in there are just arbitrary and make certain assumptions about the performance if the system. A better way to wait for Notepad to launch is:notePad.WaitForInputIdle();
A guide to posting questions on CodeProject[^]
Dave Kreskowiak