KeyStuff

by Rich Pasco
Revised Monday, November 9, 2009
Copyright © 2004-2009 Richard Pasco

KeyStuff is a Windows 32-bit console utility to load keystroke events into the keyboard queue for processing by subsequent applications. It is useful in BAT (and CMD) files for automating operations of programs that were designed for interactive keyboard use.

License: Use of KeyStuff is free to end users, but I retain the copyright. This program is shared "as-is" without warranty, promise of support, or liability for consequences of its use. Bug reports are requested, and enhancement requests are invited.

Calling syntax

Command-line invocation is according to the syntax:

keystuff token1 token2 token3 ...
where each token is either a key mnemonic or a literal string.

Key mnemonics

A key mnemonic is the name of a key optionally preceded by one or more shift qualifiers, separated by hyphens. A shift qualifier is any of Alt-, Ctrl-, Shift-, or Windows-. Valid key names include Enter, F1 through F12, Home, End, Pgup, Pgdn, Left, Right, Up, Down, Ins, Esc, Del, Tab, Bksp, Space, PrtSc, the digits 0 through 9, and the letters A through Z. Case is ignored; the capitalization shown is merely suggested for human readability.

In addition to the names of real keys, KeyStuff defines two pseudo keys, Null and Wait, which support shift qualifiers (or their absence) and time delays.

Pseudo-key Null sequences shift qualifiers

In some applications, the order in which shift qualifier keys change state is important. With KeyStuff, if two consecutive key mnemonics have any shift qualifiers in common, they are held down while pressing both keys. If two consecutive key mnemonics have different sets of shift qualifiers, only those qualifiers which need to change state are toggled. In this case, the qualifiers associated with only the first one are released (in order Shift, Ctrl, Alt, and Windows) before the qualifiers associated with only the second one are depressed (in the same order). If this is not acceptable, it may be altered with the Null pseudo key.

Null may be used between two keys with the same shift qualifier to specify releasing the shift keys momentarily between them. For example, the command

keystuff Alt-Tab Alt-F4
would act like holding down the Alt key while pressing Tab and then F4, but
keystuff Alt-Tab Null Alt-F4
would act like momentarily releasing the Alt key in between.

Conversely, Null may be used to tap a shift qualifier key without pressing any other keys, for example

keystuff Alt-Null
would briefly tap the Alt key.

Null may also be used to explicitly specify the order in which shift qualifiers are toggled. For example,

keystuff Shift-Tab Ctrl-End
would hold the Shift key while pressing and releasing Tab, then release shift, then hold the Ctrl while pressing and release End, then finally release the Ctrl key. But if overlap between the Shift and Ctrl were necessary, it could be achieved by
keystuff Shift-Tab Shift-Ctrl-Null Ctrl-End

Pseudo-key Wait introduces time delay

The pseudo-key Wait is like Null except that it takes an argument, the number of milliseconds to wait. KeyStuff will wait the specified time between "typing" the characters before and after this command. For example,

keystuff Alt-Tab Wait1000 Alt-F4
will cause KeyStuff to "hit" the Alt-Tab combination, release both keys and wait one second, then "hit" the Alt-F4 combination.

A fine point: The waiting is done while KeyStuff is still executing, before it puts subsequent keystrokes into the buffer. KeyStuff does not actually stuff any wait codes into the keyboard buffer (in fact, there is no such thing as a wait code).

Literal strings

A literal string is enclosed in a pair of quotation marks (not apostrophes). To include a quotation mark in a literal string, double it up.

Examples

It may be instructive to copy each of these commands and paste them onto a command shell command line, then hit Enter to see what they do:

These examples are processed immediately:
keystuff /?Display version number and brief help info
keystuff Alt-EnterToggle shell between full-screen and window
keystuff Alt-TabSwitch to the next window
keystuff Alt-Tab Null Alt-F4Switch to the next window and close it
keystuff Windows-DMinimize all windows to show the Desktop
keystuff Windows-Null Wait1000 Esc Alt-TabBring up Start Menu for one second, then close it and return to the previous window

These examples leave keystrokes to be processed by the command shell interpreter CMD.EXE after KeyStuff exits:
keystuff "dir" EnterExecute the shell command "dir"
keystuff "echo ""Hello World""" EnterType "Hello World" including the quotes
keystuff "exit" EnterExit the shell
keystuff Alt-Space E PPaste the clipboard into the shell

Here are some syntactically valid examples that don't work as expected:
keystuff Ctrl-Alt-DelBring up Task Manager

Installation

  1. Download and open keystuff.zip.
  2. Move keystuff.exe into any directory on your command PATH.
  3. Save keystuff.html (this file) for future reference.

Revision History

1.2Jan 22, 2004First release
1.3Jan 27, 2005Minor bug fixes
1.4Jan 17, 2006Added pseudo-key Space
1.5Oct 12, 2006Maximum length of any one literal increased from 256 chars to 16K chars
1.6Nov 8, 2009Added PrtSc key names.
Fixed bug due to MapVirtualKey failure to return scan code for period and comma virtual-key codes.

Author: Rich Pasco