Share via


C# SendKeys.Send problem

Question

Tuesday, September 23, 2008 9:15 PM

Hi,
I am having trouble using the SendKeys.Send() or SendKeys.SendWait() methods in my application.  It is not allowing me to send any strings that start with a lowercase letter, or send number keys at all.  I writing a GUI to return a grundle of options to a command line program, and the command line program requires lowercase letters, and numbers, for input. Here is the code I have right now:

private void button1_Click(object sender, EventArgs e)
       
     {
           
            String path = "CD C:\Path";
            Process newProc = Process.Start("cmd.exe");
            SendKeys.Send(path + "{Enter}");
            SendKeys.Send(" Program.exe{Enter}");

A workaround for some options has been to set up entries like this:
    
            SendKeys.Send("{CAPSLOCK}4{CAPSLOCK}{Enter}");

Which works for fixed options, but for some that I want to bring in from textboxes in the form:

            String Data1;
             Data1 = TextBox1.Text.ToString();          
            SendKeys.Send(Data1 + "{Enter}");

this does not work.  I checked if the program was at fault, but it turns out that SendKeys won't send anything to the command line if it doesn't start with a Capital character.  Any suggestions would be helpful.

Thanks

All replies (6)

Friday, September 26, 2008 4:53 AM âś…Answered | 1 vote

 Hi,

try this:

        [DllImport("USER32.DLL", CharSetCharSet = CharSet.Unicode)]  
        public static extern IntPtr FindWindow(string lpClassName,  
            string lpWindowName);  
        private Process newProc = null;  
        string wt=" ";//not ""  
 
        private void button1_Click(object sender, EventArgs e)  
        {  
            string a = textBox1.Text;  
 
          IntPtr Handle = FindWindow(null,wt);             
          if(newProc==null)  
           newProc= Process.Start("cmd.exe");  
            
          SetForegroundWindow(Handle);  
                               
          SendKeys.SendWait("{Enter}");  
             
          wt = newProc.MainWindowTitle;  
 
        } 

 Hope it will help.

Best regards,
Harry


Tuesday, September 23, 2008 9:20 PM

Oh and when I hit button1 again, the app explodes :-p  It opens up like 7 command line windows and spams whatever I have in it another 10 times.  Not quite sure what to do about that...

Thanks again


Thursday, September 25, 2008 9:40 AM | 1 vote

Hi,

As I learnt from your post above, you have used {CAPSLOCK} to send the keys, I believe it's a good means.

When you hit the button again , It opens up like 7 command line windows.I guess the reason is that:
the second time you hit the button ,the focus is on the button ,not the cmd,for it hasn't show up. then the next code( SendKeys.Send(path + "{Enter}")) run ,{Enter} is sent to the button ,and so on.
To solve this ,try to add the code line in red :

 Process newProc = Process.Start("cmd.exe");  
 Thread.Sleep(500); 

hope that helps.

Could you also share with us that What parameter would you like to use, maybe
some community members have some idea on it.

Regards,
Harry


Friday, September 26, 2008 2:39 AM

Ok Cool!

using
System.Threading.Thread.Sleep(20);
after each step works.  I also discovered that the mouse cursor needs to be in the field of the command window for it to be reliable and send non alpha characters.  Clunky.... but very helpful at this point.  Any way that I can force the .exe to top and move the mouse to it automatically?

I changed it to
Process newProc = Process.Start("Program.exe");
                SetForegroundWindow(newProc.Handle);
                IntPtr pFoundWindow = newProc.MainWindowHandle;

to force it to the top window, but it still isnt terribly reliable.

Thanks agan,
Stewart


Thursday, June 3, 2010 11:34 AM

As has been said, using {CapsLock} works, but what hasn't been said (I think) is that it can be used to send lower case. The following will send a lower case "init":

            SendKeys.SendWait("{CapsLock}INIT{Enter}{Enter}");


Thursday, June 3, 2010 12:49 PM

You might as well just use

 

Handle = newProc.MainWindowHandle;

 

after starting the process rather than having to "find" the window each time.