views:

163

answers:

2

This may be a cardinal programming sin, but what's the easiest and quickest way via executable or batch file to bring focus to Window X and hit keystroke Y? This won't be reused, and X and Y can be hard-coded if necessary.

edit: on a Windows machine

+2  A: 

Use the Win32 API calls FindWindowEx and SendMessage (or SendNotifyMessage if you want control to return to your application immediately without waiting for the receiving application to process the message).

Assuming this is Windows, of course.

This isn't really much of a sin, by the way. There are much more evil things that a program can do than sending keystrokes to other applications (e.g. randomly deleting files, emailing porn to your grandmother etc.).

MusiGenesis
+3  A: 

You can try AutoHotkey or use win32 API as MusiGenesis suggested.

Nick D
AutoHotKey seems to be the quickest and dirtiest solution here.No need to reinvent the wheel.Something like:IfWinExist Program-X{ WinActivate}else{ Run Program-X WinWait Program-X WinActivate}Send {Y}
Derek