DateStamp – Sending Keystrokes to Another Application

This is a utility I wrote for a department at work to stamp a date and time for an annotation in an e-mail. The e-mail client we are using does not support this feature so we have to do it manually.
The program is interesting because it shows how to send keystrokes from a C# application to another program. I was trying to make the target application active to send the keys, but the command to make it’s document active requires the document name in it’s arguments (see below). I found it simpler to set the DateStamp program so it’s always the topmost window and when you click the Stamp button, it will send an Alt-Tab keystroke to switch to the previous application. The date and time are then sent as key presses. It’s not a very elegant solution but it works.
You can make an application active and send commands with the following statements:

Microsoft.VisualBasic.Interaction.AppActivate("Untitled - Notepad");
SendKeys.Send ("This is a test.");

If you are using C#, you need to add a reference to Microsoft.VisualBasic in order to use the above AppActivate function.
The Send command sends the text as key presses to the application. You can also use the SendWait function which does the same thing but also waits for the message to be processed. If the commands from SendWait are not being processed properly, a slight time delay may need to be set between commands. This is a problem with the .NET Framework.

3 thoughts on “DateStamp – Sending Keystrokes to Another Application”

  1. Not sure if it applies in this case, but another handy tool for general Windows macroing is AutoHotKey. It can do some neat stuff – I use it to remap various keystrokes.

  2. I have used AutoHotKey to set up predefined emails (such as FTP instructions and such.), and for renaming files before posting to DFS, or for our internal use.

  3. AutoHotKey looks like a good utility. Before writing DateStamp, I was trying to find a Microsoft programmable keyboard but there weren’t any available. I’ll have to try the AutoHotKey program because I’d like the DateStamp program to be run by pressing a function key. It would save me the trouble of trying to figure out how to do this in C#.


Leave a Comment