# .NET: Can one program feed events to another?

**URL:** <https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308>\
**Category:** Factual Questions\
**Created:** [September 24, 2007, 3:28pm UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308 "2007-09-24T15:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [September 24, 2007, 3:28pm UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308/1 "2007-09-24T15:28:26Z")

</div>

Using [VisualStudio.NET](http://VisualStudio.NET), I’d like to create a program that generates keystroke events for another program that consumes them. (The idea is to be able to test the second program without actually having to sit at a keyboard and press keys.)

Is this possible?

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 24, 2007, 3:34pm UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308/2 "2007-09-24T15:34:18Z")

</div>

I can’t remember the specific function call off the top of my head, but something [here](http://msdn2.microsoft.com/en-us/library/ms645530.aspx) might be helpful.

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [September 24, 2007, 5:02pm UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308/3 "2007-09-24T17:02:30Z")

</div>

I can’t think of anything specific in .NET that would do what you want, but you can write either a Keyboard hook or Journal playback hook.

There is a great book that has a few chapters devoted to this called _Windows 95 A developer’s Guide_ buy Jeffrey Richter and Jonathan Locke. Chapters 6 and 8 talk about the various ways you could achieve what you want to do and why he chose to solve the problem the way he did. Don’t worry that was written for Windows 95, the concepts are still the same. The book is probably out of print, but you can likely get a used copy off of Amazon

---

<div class="post-metadata">

**Author:** ![chrisk](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@chrisk](https://boards.straightdope.com/u/chrisk)\
**Post date:** [September 24, 2007, 6:11pm UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308/4 "2007-09-24T18:11:13Z")

</div>

Okay, I knew that there had to be an easier way to do this, so I looked through my MSDN a bit…

> [@Microsoft Developer Network](#):
>
> Code: Sending Keystrokes to Another Application (Visual Basic)  
> See Also Windows Applications Example Topics | Shell Function | AppActivate Function  
> This example uses the AppActivate Function to activate a running application and then uses the SendKeys Class to send it keystrokes.
> 
> Example  
> AppActivate(“Untitled - Notepad”)  
> SendKeys.SendWait(“Hello world!”)  
> ’ Send Ctrl S to open the Save As dialog box.  
> SendKeys.SendWait(“^S”)  
> ’ Send the name of the new file.  
> SendKeys.SendWait(“c:\HelloWorld.txt”)  
> ’ Send Alt S to save the file.  
> SendKeys.SendWait(“%S”)
> 
> Compiling the Code  
> This example requires:  
> A reference to the System.Windows.Forms namespace.  
> A running instance of the Notepad (notepad.exe) application.
> 
> Robust Programming  
> A System.ArgumentException error is raised if an application with the requested title cannot be found.  
> When called with a string, the AppActivate function activates the first application found where the title bar of the application window matches the requested string, even if multiple applications have the same title. You can avoid this problem by using the Shell function to start the application and by supplying the returned program id to the AppActivate function.

I’ve had experience using sendkeys in VB6, so that’s why I knew what dot net terms to try looking under.

---

<div class="post-metadata">

**Author:** ![Xema](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@Xema](https://boards.straightdope.com/u/Xema)\
**Post date:** [September 25, 2007, 2:25am UTC](https://boards.straightdope.com/t/net-can-one-program-feed-events-to-another/420308/5 "2007-09-25T02:25:55Z")

</div>

Thanks to all - most useful.
