January 16, 2006

Keyboard Shortcuts

Filed under: Automation,Scripting — Tags: — Marcus Tettmar @ 9:19 am

As mentioned in my last post, the easiest, most reliable way to automate an application is via keyboard shortcuts. But I’ve found that many people are so used to using the mouse that they don’t even realise you can use the keyboard to move around Windows applications.

Some things I take for granted are completely new to others. For example, moving from one field to the next is accomplished with the Tab key. After entering some information into an edit box just hit Tab to move to the next edit box. This is surely faster than moving your hand away from the keyboard, moving the mouse, clicking in the next edit box, moving back to the keyboard … and so on. Tab is all you need. Something a lot of people don’t realise is that this even works on web pages. Tab will move from form field to form field, but also from link to link. Try it now while you are reading this post. Press Tab a few times. You’ll notice a faint hashed box around the focused link. Press Tab again and the box moves to the next link. And so on. When you’re on a clickable object, or link, the Enter key will select or “click” it. Tab to a link and then press Enter and you will go to that page. In a Windows application you can tab to any standard object. You can tab through edit boxes as well as check boxes, menu items and buttons, and pretty much anything else. If you tab to a button pressing Enter will “click” it.

Checkboxes can be toggled from checked/unchecked and back again by pressing the space bar. Tab to a checkbox and hit the space bar. Its checked status will change. To select an item in a list box you can use the up and down arrows. Same goes for treeviews and combo boxes. But what you might not realise is that many list boxes and combo boxes have a kind of “drill down” feature. Type the first few characters of an entry and the selected item will change to the first item that starts with those characters. Type more characters in one go to narrow down the selection. This can be really useful when automating applications where you want to select an item in a list box or drop down – you can just send the text of the item you want to select. E.g. the following script automates the Regional Settings control panel applet to automatically change the default input language to Chinese (Hong Kong):

//Set to Chinese (Hong Kong …)
WaitWindowOpen>Regional and Language Options
SetFocus>Regional and Language Options
Send>Chinese (Hong Kong
Press Enter

This works by sending the first few characters unique to the entry we want to select. Unfortunately not all list boxes work like this. That’s why Macro Scheduler has advanced commands like GetListItem to determine the index of an item given it’s text caption.

Most of us know that we can select menu items using the Alt key. When you press the Alt key you should see certain characters in the menu items and on other objects become underscored. These are the shortcut keys. So to select the file menu in most applications you would press ALT-F. If an application has been designed properly other fields and buttons will have underlined characters also. Even labels associated with objects should have shortcut keys so that when you press ALT and that key you move focus immediately to that object. All this makes automation so much easier.

Other key combinations that are useful include CTRL-TAB to move from page to page or tab to tab in a tabbed window, such as Firefox. Try CTRL-TAB in firefox with several tabs open. You’ll move from one to the next.

Did you know you can select the next word in an editor by pressing CTRL-SHIFT-RIGHT? SHIFT-RIGHT on its own just moves the cursor to the end of the word and then to the next word and so on. Reverse it with SHIFT-LEFT. SHIFT-END will highlight to the end of the line. SHIFT-CTRL-END to go to the end of the document.

This is just the beginning. There are all sorts of keyboard shortcuts that make working in Windows so much faster and make automation so much easier and more reliable. I don’t know them all. But it’s worth getting to know them if you want to get the best out of Windows Automation.

Here are links to some pages that list useful keyboard shortcuts:

List of the keyboard shortcuts that are available in Windows XP

Shortcut keys in Windows 95,98,Me:

Getting the most out of your Windows Keyboard