DateStamp – Sending Keystrokes to Another Application
Posted by Johan Cyprich on 30 Mar 2007 | Tagged as: Applications, Programming
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.
You can view the source code in the sourcedot.com forums and you can also download it here. The code is very simple and it can be modified for use in Visual Basic.
| Related posts: | |
|
|
Share this post:
Follow Me:
Did you find this post interesting and useful? You can keep up to date on this blog by subscribing to my RSS feed, or you can have new posts sent to you by e-mail. You can also follow me on Twitter.
3 Comments »
Tweet This Post!
















Loading...
on 30 Mar 2007 at 5:56 pm 1.Jonathan Aquino said …
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.
on 02 Apr 2007 at 4:28 am 2.Steve.Lippert said …
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.
on 02 Apr 2007 at 9:23 am 3.Johan Cyprich said …
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#.