简体   繁体   中英

SendKeys to application c#

I am having the following issue, i want to simulate a keypress from c# to a application, I tried using sendkeys, inputsimulator & interception. My question is how to deal with this? I know sendkeys is a very small class but I hope it can work out in this situation,

 Process p = Process.GetProcessesByName("condor").FirstOrDefault();
 if (p != null)
 {
        IntPtr h = p.MainWindowHandle;
        SetForegroundWindow(h);
        SendKeys.Send("k");
 }

This gives me the error:

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

My question is how to simulate a key press to a formless application?

Good day Mr Erwin! You should try adding Thread.Sleep before getting MainWindowHandle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM