简体   繁体   中英

How to send an “Enter” press to another application in WPF

I have created a WPF application that monitors certain processes running on a machine. If one of the processes errors out or quits it needs to automatically restart that process. My code works by evaluating whether or not the process is still running via my_process.HasExited .

The only issue I run into is that one of the processes I am monitoring is run by a third party application, and when this application's processes have problems they display an error message before quitting. This error message requires an Enter key stroke to exit out and allow the process to fully end.

Thus, i am looking for a way to send an Enter key press to the application every 5 seconds when it checks if the process is still running. Doing so would clear the error message and allow the process to quit so that it could be restarted. I have tried using SendKeys but realized this class is not available in WPF.

Is there an easy way to send a simulation of pressing the Enter key to another application from my WPF app?

Not easy but can be done with some Windows API.

Send Message in C#

Using PostMessage/SendMessage to send keys to c# IE WebBrowser

The idea is that you get a handle to the window and you call SendMessage to send the key down and key up.

SendMessage(Handle, WM_KEYDOWN, VK_ENTER, 0);
SendMessage(Handle, WM_KEYUP, VK_ENTER, 0);

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