Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Windows Macrorecoders and Keystroke Log Utilities

 News See also Recommended Links Expect WSH
Techniques for 'driving' Windows applications Screen  Keyboard remapping Humor Etc

Note: For Unix expect is a great program with the capabilities unmatched in the Windows world. There is O'Reilly book about Expect.

They are essential if you want to understand how you are using the system and what for what operations you need to create macros and/or user menu items.


Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index


Old News ;-)

Microsoft Windows 2000 Scripting Guide - Sending Keystrokes to a Program

Sending Keystrokes to a Program  

Microsoft® Windows® 2000 Scripting Guide

By providing scripts with access to most COM objects, WSH enables you to automate applications that have a COM-based object model. Unfortunately, some applications, especially older ones, do not have a COM-based object model. To automate these applications, WSH provides a way to send keystrokes to these applications.

When you use the WshShell SendKeys method to send keystrokes to an application, your script mimics a human typing on the keyboard. To send a single keyboard character, you pass SendKeys the character itself as a string argument. For example, "x" to send the letter x. To send a space, send the string " ". This is exactly what a user would do if he or she was working with the application: to type the letter x, the user would simply press the x key on the keyboard.

When you use the SendKeys method, special keys that do not have a direct text representation (for example, CTRL or ALT) are represented by special characters. Table 3.16 lists these SendKeys representations for commonly used keys.

Table 3.16 SendKeys Representations of Common Keys

Key SendKeys Representation

BACKSPACE

{BACKSPACE}, {BS}, or {BKSP}

BREAK

{BREAK}

CAPS LOCK

{CAPSLOCK}

DEL or DELETE

{DELETE} or {DEL}

DOWN ARROW

{DOWN}

END

{END}

ENTER

{ENTER} or ~

ESC

{ESC}

HELP

{HELP}

HOME

{HOME}

INS or INSERT

{INSERT} or {INS}

LEFT ARROW

{LEFT}

NUM LOCK

{NUMLOCK}

PAGE DOWN

{PGDN}

PAGE UP

{PGUP}

PRINT SCREEN

{PRTSC}

RIGHT ARROW

{RIGHT}

SCROLL LOCK

{SCROLLLOCK}

TAB

{TAB}

UP ARROW

{UP}

SHIFT

+

CONTROL

^

ALT

%

BACKSPACE

{BACKSPACE}, {BS}, or {BKSP}

 

All function keys, like F1, are represented by the button name contained within braces for example, {F1} for the F1 button and {F2} for the F2 button.

For example, the following script starts Notepad and then types the sentence, "This is a test."

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Notepad.exe"
Do Until Success = True
    Success = objShell.AppActivate("Notepad")
    Wscript.Sleep 1000
Loop
objShell.SendKeys "This is a test."

When the script runs, Notepad will open, and the sample sentence will be typed in, as shown in Figure 3.12.

Figure 3.12 Controlling Notepad by Using SendKeys

sas_wsh_114s
 

See full-sized image.

 

Note

You can send repeated keystrokes by using the SendKeys method. For example, to send the letter a ten times, you send the string "{a 10}". You must include a space between the keystroke and the number. SendKeys allows you to send only repeated single keystrokes. You cannot send multiple characters using repeated keystrokes; for example, this command will fail: {dog 10}.

You should be aware that sending keystrokes to an application is not the optimal method for automating a procedure. If you have an application in your enterprise that you need to automate and it has no COM-based object model, you might consider this technique. However, you should first examine whether other methods exist for automating that particular application.

Although SendKeys can be used effectively, there are several potential problems with this approach:

The script might have difficulty determining which window to send the keystrokes to.
Because the application runs in GUI mode, a user might close the application prematurely. Unfortunately, this will not terminate the script, and the script could end up sending keystrokes to the wrong application.
The script might have difficulty synchronizing with the application.

This timing issue is especially troublesome, simply because scripts tend to run much faster than GUI applications. For example, this simple script, which starts Calculator and then tries to type the number 2 into the application, is coded correctly but will likely fail when run (Calculator will start, but the number 2 will not be entered):

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
objShell.AppActivate "Calculator"
objShell.SendKeys "2"

The script fails not because of a syntax issue but because of a timing issue. As quickly as it can, the script issues commands to:

1. Start Calculator.
2. Switch the focus to Calculator (using the AppActivate method).
3. Send the number 2 to Calculator.

Unfortunately, the script runs faster than Calculator can load. As a result, the number 2 is sent, and the script terminates, before Calculator can finish loading and start accepting keystrokes.

There are at least two ways of working around this problem. First, you might be able to estimate how long it will take an application to load and then pause the script for that amount of time. For example, in this script the Run method is called, and then the script pauses for 5 seconds, giving Calculator time to load:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
Wscript.Sleep 5000
objShell.AppActivate "Calculator"
objShell.SendKeys "2"

Of course, is some cases it might be difficult to estimate how long it will take before an application is loaded and ready to accept keystrokes. In that case, you can call the AppActivate method and check the return value.

Using AppActivate

Before sending keystrokes to an application, you must first ensure that the application is running and that the focus is on the application (that is, the application is running in the active window). You can use the AppActivate method to set the focus on an application. The AppActivate method brings the specified window to the foreground so that you can then start using the WshShell SendKeys method to send keystrokes to the application.

The AppActivate method takes a single parameter that can be either a string containing the title of the application as it appears in the title bar or the process ID of the application. The AppActivate method returns a Boolean value that indicates whether the procedure call has been successful. If the value is False, AppActivate has failed, usually because it was unable to find the application (possibly because that application had not finished loading).

You can place your script in a loop, periodically calling AppActivate until the return value is True. At that point, the application is loaded and prepared to accept keystrokes.

For example, this script checks the return value for AppActivate. If this value is False, the script pauses for 1 second and then checks the value again. This continues until the return value is True, meaning that the application is loaded and ready for use. At that point, the script continues.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
Do Until Success = True
    Success = objShell.AppActivate("Calculator")
    Wscript.Sleep 1000
Loop
objShell.SendKeys "2"

When the script is determining which application to activate, the given title is compared to the title of each window visible on-screen. If no exact match exists, the AppActivate method sets the focus to the first window whose title begins with the given text. If a window still cannot be found, the first window whose title string ends with the text is given the focus. The partial matching with the leading and trailing text of title bars ensures that AppActivate works with applications, such as Notepad, that display the name of the currently opened document on the title bar. (For example, when you first start Notepad, the window title is Untitled - Notepad, not Notepad.)

This means that when setting the focus to the Calculator, you can use one of the following lines of code:

objShell.AppActivate "Calculator"
objShell.AppActivate "Calc"
objShell.AppActivate "C"

Of course, this shortcut method of referring to a window can cause problems. For example, suppose you use this line of code:

objShell.AppActivate "Calc"

If you happen to be working on a Microsoft Word document named Calculations.doc, the keystrokes might be sent to the Word document instead of Calculator.

The script in Listing 3.30 demonstrates a more practical use of the SendKeys method: It starts and sets focus to the Microsoft Management Console (MMC) and then sends keystrokes that cause the Add/Remove Snap-in and Add Standalone Snap-in dialog boxes to be displayed. The script automates the first part of the common task of constructing custom MMC snap-in tools.

Listing 3.30 Sending Keystrokes to a GUI Application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Const iNormalFocus = 1
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "mmc.exe",iNormalFocus

Wscript.Sleep 300

objShell.AppActivate "Console1"
Wscript.Sleep 100
objShell.SendKeys "^m"
Wscript.Sleep 100
objShell.SendKeys "{TAB}"
Wscript.Sleep 100
objShell.SendKeys "{TAB}"
Wscript.Sleep 100
objShell.SendKeys "{ENTER}"
 

 

AutoIt v3 - Automate and Script Windows Tasks - For Free!

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!

AutoIt was initially designed for PC "roll out" situations to reliably automate and configure thousands of PCs. Over time it has become a powerful language that supports complex expressions, user functions, loops and everything else that veteran scripters would expect.

Features:

AutoIt has been designed to be as small as possible and stand-alone with no external .dll files or registry entries required making it safe to use on Servers. Scripts can be compiled into stand-alone executables with Aut2Exe.

Also supplied is a combined COM and DLL version of AutoIt called AutoItX that allows you to add the unique features of AutoIt to your own favourite scripting or programming languages!

Best of all, AutoIt continues to be FREE - but if you want to support the time, money and effort spent on the project and web hosting then you may donate using the link to your left in the menu.

Techniques for 'driving' Windows applications

[ testing, automation, ...]

[Technologies:

[Nov 11, 2008] AutoHotkey - Free Mouse and Keyboard Macro Program with Hotkeys and AutoText

AutoHotkey is a free, open-source utility for Windows. With it, you can:

Getting started might be easier than you think. Check out the quick-start tutorial.

[Apr 18, 2008]  CoScripter

A useful Firefox plug-in

CoScripter is a system for recording, automating, and sharing processes performed in a web browser such as printing photos online, requesting a vacation hold for postal mail, or checking flight arrival times. Instructions for processes are recorded and stored in easy-to-read text here on the CoScripter web site, so anyone can make use of them. If you are having trouble with a web-based process, check to see if someone has written a CoScript for it!

[Feb 29, 2008] Project details for Key Scripter

freshmeat.net

About:
Key Scripter listens to key press/release events from a keyboard device and sends fake key events to an X display. It supports gaming keypads such as the Nostromo SpeedPad and allows the creation and usage of complicated key scripts for games and other applications.

Release focus: Major feature enhancements

Changes:
This release also supports Windows. A Win32 binary has been added to the download packages. To compile the source files on Windows, the latest release of MinGW is required. Additionally, this release fixes a few memory allocation bugs, adds support for wildcard binds, and provides improvements to debug messages. The example configuration file has been extended with extra features.

Author:
Andrei Romanov [contact developer]

[Nov 25, 2007] AutoHotkey - Free Mouse and Keyboard Macro Program with Hotkeys and AutoText Nice addition to windows.

AutoHotkey is a free, open-source utility for Windows. With it, you can:

Getting started might be easier than you think. Check out the quick-start tutorial.

More About Hotkeys

AutoHotkey unleashes the full potential of your keyboard, joystick, and mouse. For example, in addition to the typical Control, Alt, and Shift modifiers, you can use the Windows key and the Capslock key as modifiers. In fact, you can make any key or mouse button act as a modifier. For these and other capabilities, see Advanced Hotkeys.

Other Features

License: GNU General Public License

Free Workspace Macro Pro

Automate & schedule tasks easily. Reliable macro program & Windows automation software with macro scripting & task scheduler. Record keyboard & mouse or write macro scripts to create macros. User-friendly macro recorder & task automation tool. Features unique SMART macro technology, macro editing, repeat options, easy macro language, Turbo-speed replay, IE plug-in & customizable UI.

Use it to automate and schedule routine business processes like checking inventory, importing data into Excel or generating reports. Use advanced editing capabilities to optimize recorded macro scripts. Use it to perform system maintenance functions, test applications and web pages, automate data entry at high-speeds, intelligently copy data from one application to another etc. All with just a single click! The possibilities are endless. Rely on this 'Record once & play anytime' macro automation utility.

Use the add-on program Launch-n-Go to launch automation macros using hotkeys or keywords.

ShortKeys 98 Lite

A free keyboard-macro creator for all your Windows application, for saving repetitive keystrokes.

AutoMate Pro  

Put your PC on autopilot by programming it to execute a variety of repetitive tasks. Automatically check e-mail, scan for viruses, back up files, and much more with this powerful utility. AutoMate lets you  send keystrokes, button clicks, mouse movements, and positioning info on any specified schedule or trigger.

Macro Scheduler

Schedule programs to run at predefined  times, and use the macro recorder facility to send keystrokes or mouse clicks that automate the task. Macro Scheduler features over 100 built-in commands that will make it easy for you to record macros and provide some level of automation that fits your program execution needs.

AutoIt.exe

Automate tasks using keystrokes and mouse commands.
Location: winsite-winnt archive Directory: sysutil/ Size: 98.1K

pcpro30.exe

Invisible Stealth Keystroke and Activity Logger
Location: winsite-winnt archive Directory: miscutil/ Size: 138.6K

Invisible Stealth Keystroke and Activity Logger
File date: March 2, 1999
Archive: winsite-winnt  Directory: miscutil/  Size: 138.6K

http://www.macroexpress.com 

 "the "Macro Express" program works very well and is relatively inexpensive compared to many others" [added Sept 08, 2004, thanks to Ron Perrella for the link]

Keyboard Spectator Lite 1.3 

Released: Oct 05, 2003

OS: Windows 95/98/ME/NT/2000/XP

License: Freeware

Keyboard Spectator is a multifunctional keyboard tracking software (a.k.a. key logger) that is widely used by both regular users and IT security specialists. The reason for such popularity is that this program does not just record keystrokes; it is capable of recording language specific characters (ex. umlauts), date and time certain window was initiated as well as the caption of that window. Thus, this software combines two very important qualities - it records all typed data, so that you won't lose it when your computer unexpectedly crashes, and it keeps the record of all keyboard activity. It allows you to monitor your children's activity at home or to make sure your employees do not use company's computers inappropriately. There are two versions of Keyboard Spectator - Lite and Pro. Keyboard Spectator Lite is free, contains all basic features described above and is intended for non-commercial use. Keyboard Spectator Pro is a well known, highly praised IT security oriented software that goes beyond recording keystrokes. Originally created for corporations and small businesses, it is becoming increasingly popular for home use. The main reason for that is the extended capabilities of Pro version.Unlike KS Lite, it can run invisibly in the background, record names of all executed programs, supports multi-user operating systems, has multi-lingual support, provides password reminder option and is highly stable and secure. Industry experts regard Keyboard Spectator Pro as the best low-cost non-invasive software from the key logger family. All major corporations use keyboard loggers to prevent loss of data and to monitor employees' activity; large part of them chose Keyboard Spectator from www.refog.com Keyboard Spectator is sold on a try-before-you-buy basis, which means there are no up front costs. You are given substantial time period to try and test the software to make sure this is the application you've been searching for. Download Keyboard Spectator now.

Key Scripter by Andrei Romanov

This is for X allocations

Key Scripter listens to key press/release events from a keyboard device and sends fake key events to an X display. It supports gaming keypads such as the Nostromo SpeedPad and allows the creation and usage of complicated key scripts for games and other applications.

Recommended Links

AutoIt v3 - Automate and Script Windows Tasks - For Free!

Key Scripter by Andrei Romanov

WinBatch®, the batch language for Windows, your scripting solution.

Winbatch - Wikipedia, the free encyclopedia



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Created Jan 2, 1997.  Last modified: August 12, 2009