make the computer press keys

  • Thread starter Thread starter Rogelio
  • Start date Start date
R

Rogelio

hey I need to make a program that opens a different program, then goes to
"File -> New" and then press enter to indicate a new document, etc..

is it possible to make a windows service, that will open a program press a
series of keys for me?

my reality is more complicated than my example, I need to take data from the
clip board, open a program, and paste it in a certain field that I need to
get to by pressing tab. but the basic question is the same.

any ideas or thoughts are appreciated.
 
You are going to want to look at the SendInput API function to do this.

However, doing this from a service isn't advised. You would have to
allow the service to interact with the desktop, a feature which was removed
with Vista.
 
If you can program in the environment of the/your problem (try vba in
word,excel and so on) to get around creating a service (dont go
there!) manipulating/acting as a person typing on the keyboard it is
preferrable. I have done (simulating keystrokes) this with VB6 (yes,
we still exist) and sendKeys but appactivate and what the window to
activate should be some problem, if they have 3 widows open and I look
for DLC3270... witch one is active... if its an console app, there is
always the pipe or whatever its called eg. "myapp.exe < text.txt"
//CY
 
If you can program in the environment of the/your problem (try vba in
word,excel and so on) to get around creating a service (dont go
there!) manipulating/acting as a person typing on the keyboard it is
preferrable. I have done (simulating keystrokes) this with VB6 (yes,
we still exist) and sendKeys but appactivate and what the window to
activate should be some problem, if they have 3 widows open and I look
for DLC3270... witch one is active... if its an console app, there is
always the pipe or whatever its called eg. "myapp.exe < text.txt"
//CY


Your best bet would be to fire up the program you want to feed keys to and
use Spy++ to find the window. Then use EnumWindows (Process.GetProcesses)
followed by a nested series of EnumChildWindows to identify the hWnd of the
program's message pump. Then you can use PostMessage to feed the keystrokes
directly to the program. If you would like an example, email me at
(e-mail address removed) so I can send you a sample of the resulting code.

Mike.
 
Back
Top