Home Page

 


EARLIER FEATURES

 


FEATURES CONTENTS

 


LATER FEATURES

 

Features Contents


5th March 2006

AUTOHOTKEY

Brian Grainger

email.gif (183 bytes)
brian@grainger1.freeserve.co.uk


 

I have, in the past, described how to produce keyboard macros by making use of the capabilities of Windows Scripting Host (WSH) and then creating a keyboard shortcut on the desktop to call it up. Although it works, this is a makeshift solution with some drawbacks. AutoHotkey is the real solution.

Here is a list of the earlier features on using Windows Scripting Host:
Keyboard Macros With Windows Scripting Host
Esperanto and Plugging the Gaps in Windows Scripting Host
Esperanto and Other Characters - The Complete Solution

These articles described how to use the Sendkeys command within a WSH script to simulate pressing user defined keys from the keyboard. When a particular script is linked to an icon on the desktop a keyboard shortcut can be added. The total effect is that this keyboard shortcut can then be a 'hotkey' to execute a sequence of different key presses.

There were two problems with this approach -one minor and one major.

The need to create a desktop shortcut is the minor one. It is a bit of a kludge making use of a weird Windows facility. If you have lots of different keyboard macros it can be a bit of a waste of screen space and you may run out of shortcut keys.

The major problem is that the Sendkeys command of Windows Scripting Host was limited in the keys that could be sent. It could not differentiate between the numeric keypad and ordinary number keys and it could not simulate the newer keys on the keyboard, such as the Windows Key and the Context Menu key.

Despite these drawbacks, I have used WSH to create a number of keyboard macros that help me in my day to day computing tasks. Then I purchased the February 2006 issue of 'Personal Computer World'. These days I tend to buy mostly Linux magazines. I do buy the occasional Windows based magazine, especially when it has a Linux program I want on the cover disk! This particular issue had two! Personal Computer World has been around longer than ICPUG, just, and has always rated highly with me. The Hands On section of tips and user notes has been invaluable over the years. I rip out the rest of the magazine and store these sections for future reference. Unfortunately, I usually forget what I read - or remember it, but forget where to find it! I digress. This issue of PCW, in the Windows Hands On by Tim Nott, introduced a scripting utility, AutoHotkey, that Tim had been trying out. To quote Tim:

"Comparing it, (AutoHotkey), to the Windows Scripting Host is like comparing an electric circular saw to a breadknife."

That was enough to intrigue me, so I was off to the web site, http://www.autohotkey.com, to get me a copy and look for myself. I'm glad I did.

AutoHotkey Logo

AutoHotkey was derived from an earlier program, AutoIt v2. The latter was a kind of WSH improvement program. It was released as Free (as in Freedom) software. Chris Mallett wanted to introduce hotkeys, so decided to create AutoHotkey. Unfortunately, this seemed to upset the AutoIt team and a flame war broke out on the forums for each product. It would seem the AutoIt people took exception to the AutoHotkey crowd using their previously GPL, (GNU Public License), code without enough acknowledgement for them. This resulted in AutoIt v3 not being released under the GPL. My feeling on the matter is that AutoIt obviously do not understand the concept of Free Software. What AutoHotkey did was what Free Software is all about! Perhaps AutoIt think Free Software is just about being no cost. Never mind. We now have two products. AutoIt v3 seems to be a replacement for WSH with the capability of creating GUIs as well. AutoIt v3 can also interact with the .dll files on your system. It uses a Visual Basic like syntax and emphatically decries the use of the goto command. It therefore has some merits! AutoHotkey will do scripting but includes a proper Hotkey function. The syntax of AutoHotkey is somewhat simpler than AutoIt v3.

AutoHotkey is still released under the GPL. It also falls under my definition of FAB, (Free Acceptably Behaved), software. Installation and use does not add or modify the Windows system files and makes no use of the registry unless you ask it to set up file associations for AutoHotkey (.ahk) files.

As well as simulating keystrokes, AutoHotkey will also simulate mouse clicks and manipulate windows. You can also create graphical interfaces to interact with the user.

Virtually any key, button, or combination can become a hotkey. 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.

You can remap the keys on your keyboard and the buttons on your mouse.

Here are some of the things you can do with AutoHotkey:

  • Change the volume, mute, and other settings of any soundcard.
  • Make any window transparent, always-on-top, or alter its shape.
  • Use a joystick or keyboard as a mouse.
  • Monitor your system. For example, close unwanted windows the moment they appear.
  • Retrieve and change the clipboard's contents.
  • Disable or override Windows' own shortcut keys such as Win+E and Win+R.
  • Alleviate RSI with substitutes for Alt-Tab, (using keys, mouse wheel, or buttons).
  • Customise the tray icon menu with your own icon, tooltip, menu items, and submenus.
  • Display dialog boxes, tooltips, balloon tips, and popup menus to interact with the user.
  • Perform scripted actions in response to system shutdown or logoff.
  • Detect how long the user has been idle. For example, run CPU intensive tasks only when the user is away.
  • Automate game actions by detecting images and pixel colours.
  • Read, write, and parse text files.
  • Perform operation(s) upon a set of files that match a wildcard pattern.
  • Work with the registry and INI files.

In order to use AutoHotkey you create a script file. Any text editor can be used although AutoHotkey comes with utilities to enhance script file creation - such as auto completion. The created script file is like a program that runs with AutoHotkey and is called up in the usual way -double clicking in Windows Explorer, if a file association is set, or via the Start button Run command. Multiple script files may be run simultaneously. When a script file is running it appears in the Windows system tray, with an icon like that pictured above. You then have a number of context menu options, such as editing the script file or stopping script execution, temporarily or permanently.

When a script file containing hotkey definitions is executed the system will monitor for the hotkeys being pressed. When pressed the defined actions will be performed. Different script files could be set up with different hotkey definitions assigned to the same hotkeys. Which actions are performed depends on which script file you are running at the time. This solves the problem of running out of hotkeys, although with the use of the extra modifier keys this is not such a problem as with WSH. The big plus over WSH is that hot keys are only monitored while the script is running. Because WSH relies on desktop shortcuts for hot keys they are in operation all the time the icon is on the desktop.

If AutoHotkey is run without a script file being named it will, by default, try and run the script defined in the AutoHotkey.ini file. This file could contain your most frequently used scripts.

^!x::
InputBox, No, Remove(n), No to Remove,,150,120
Loop, %No%
{
Send, {F2}{END}{LEFT}{LEFT}{LEFT}{LEFT}{BS}{BS}{BS}{ENTER}{DOWN}
Sleep, 100
}
return
^!j::
InputBox, No, jpg->JPG, No to Change,,150,120
Loop, %No%
{
Send, {F2}{END}{BS}{BS}{BS}JPG{ENTER}{DOWN}
Sleep, 100
}
return

The above is an example of a script file. It defines two hot keys. One is defined by CTRL-ALT-x (^!x) and the other by CTRL-ALT-j (^!j). The first will convert a user defined number (No) of filenames of the form filename(n).xxx into filename.xxx. The second will convert filenames of the form filename.jpg into filename.JPG. You can see the syntax is not too complicated. The use of the curly brackets is probably the strangest thing to Visual BASIC programmers.

There is one other very useful tool that comes with AutoHotkey - a compiler. This means you can create a compiled script file and run it on a PC that does not have AutoHotkey installed. This makes it very easy to transfer useful tools to friends who may not be proficient in program installation, or you want to run a particular script across a network of PCs in your organisation.

AutoHotkey comes with a very comprehensive compiled HTML Help file and there is the forum on the web to ask for help from other users.

All in all, AutoHotkey is a very useful tool. There are a number of scripting tools available for Windows systems. I have not found any other scripting tools as powerful and professional as AutoHotkey that have been released under the GPL. With AutoHotkey, I can now replace another Microsoft program on my PC, (WSH), with a Free alternative.


 

 

 

 


TOP