Search code examples
processkeypress

Press key to a specific process


I want to write a small application that can press keys on different process. The different process is a console application - but I want to run it in a hidden mode and specifically simulate the "Pause/Break" key-press on it (same as if it was focused and I pressed the pause key manually)

I've found those examples: How can I programmatically generate keypress events in C#? How to simulate key presses on a specific application

but I think they can't assist in my situation (or maybe I'm wrong?)

as for programming language - I don't much care - (c#/c++/c/python/ruby - whatever will do the job)


Solution

  • I believe you're storing your processes in some variables/handlers.

    All you need to do is get the input stream of your process and send the key that you want to pass/press for the specific process.

    your code should look something like this:

        Process p = new Process(); //your process
        string key = "A"; //character or string that you want to press
        p.StandardInput.Write(key);
    

    Where 'P' is the process and key is the pressed key.

    Hope this helps!