[[ [testing], automation, ...]] [[Technologies: * [cwind] - find windows, inject keystrokes (free) * [wintclsend] - similar to cwind, includes mouse moves (license) * [ActiveX] * [Android] (when combined with [VNC] or '''rdesktop'''[http://www.rdesktop.org/]) * [AutoIt] [http://www.hiddensoft.com/AutoIt/] * [COM] * [DDE] * Eventcorder [http://www.volny.cz/eventcorder] * [Expect for Windows] * [Perl]'s Win32::GuiTest * [PowerPro] * [TWAPI] includes window management and input injection (mouse and keyboard) * [WSH] * Win32-GuiText-X * (other) commercial testing applications, * Girder [http://www.girder.nl] (including [Python] client [http://www.dur.ac.uk/christopher.saunter/soft.html]) * win32api's PostMessage provides for delivery of keystrokes, button presses, and such, to external processes; while this [http://mail.python.org/pipermail/python-win32/2002-December/000635.html] discussion, as well as this [http://www.brunningonline.net/simon/blog/archives/000664.html], are about Python coding, the same functionality is available to Tcl through ... (?) * ...]] ---- '''Forget Cwind, WinTclSend And The Like...''' Peter Newman 29 Feb 2004: Win32::GuiTest are programs that rely on three standard Windows API functions:- * '''Find Window''' - which given a window's title (the text string that goes in the Title Bar,) will return it's HWND (a unique identifier that Windows assigns to a window). * '''Set Foreground Window''' - which makes the HWND supplied it the ''foreground window'' - to which all subsequent key-strokes and mouse-clicks are sent, and; * '''Send''' - which sends the specified keystrokes to the current foreground window. The documentation for these programs says or implies that you can control applications like Notepad, Word, Excel, etc - from your Tcl script - just as a user could - by using the package functions to simulate user keyboard/mouse input. BOLLOCKS! This technique is hopelessly inadequate and unreliable - except in certain very restricted situations. There are three problems:- 1. '''The Disappearing Foreground Window'''. The foreground window is a global thing - shared by all processes and applications that are running. So although you can SetForegroundWindow to the application you want to send key-strokes and/or mouse-clicks too - Windows and/or any other application can do the same thing too. In particular, every time the user clicks on something, that something becomes the new foreground window. Quite obviously, if the user's trying to use their web browser (or whatever), at the same time as your script is trying to send keystrokes to (say) Excel (or whatever), it's going to be a complete disaster. And even if no user is using the computer while your script is running, that's still no guarantee your key-strokes will reach their intended destination. Other processes can pop up message boxes etc, and steal the foreground window away from the window you set it to, at any time. 1. '''No Feedback'''. In general, when a user clicks the mouse or types in keys, they get usually visual feedback that those key-strokes and mouse-clicks are being processed. The key-strokes are echoed into the input box, and windows open and close, etc. But your script can't see any of this. And has virtually no way of knowing whether or not the requested action has been taken. 1. '''Time Delays'''. The '''Send Keys''' function will return immediately. But that doesn't mean the requested task has been completed. Say you ask Internet Explorer to open some URL - it could be 1 second or 10 seconds or 100 seconds or never - before the specified HTML is actually downloaded and displayed (assuming of course, that it is downloaded and displayed). Similarly, asking Notepad to print a file, or Excel to re-calculate a spreadsheet, could take similar wildly varying times. And just because it takes (say) 10 seconds on your lightly loaded 1 GHz machine, that doesn't mean it will take 10 seconds on some other user's 200 MHz machine on which they're simultaneously playing a CD - and a 3D shoot-em-up. Taking the above factors into account, it should be obvious that you can't generally use the SetForegroundWindow/SendKeys technique to control any old Windows application. Possibly, if no-one is using the computer whilst your script is running, you might get some mileage out of it. But if you want to drive Excel, Notepad, Word, Internet Explorer, your MP3 player, or the like, whilst you or other users are using the computer, forget it. The other solutions mentioned above are much better alternatives.