jump to navigation

Editing text made easy with AutoHotkey January 6, 2008

Posted by tilm4nn in IT in daily life, Programming.
Tags: , , , , , ,
trackback

While editing text, especially computer programs but also letters, documentation or reports, it is often required to move the cursor to another position like a line up or down or to the beginning or end of the line. Fortunately keyboards have keys to do this. Unfortunately the right hand has to be removed from its usual working position to use these. This leads to interrupts of the typing work-flow, is time-consuming and is error prone when the hand has been placed back in the wrong position. A possible solution to this is to customize the keyboard behaviour so that the required functions are accessible while the hands can stay right where they are.

Customize cursor movements

AutoHotkey is a very useful Windows utility that allows, among other functions, full customization of the keyboard behaviour. Now I’ll show how it can be used to simplify navigation in text.

After AutoHotkey has been installed a so called AutoHotkey script has to be edited or created. The best way to start is to simply edit the AutoHotkey init script which is called “AutoHotkey.ini” and is located in the install directory. It can be changed using any text editor. If AutoHotkey is running it can be accessed with a right click on the AutoHotkey system tray icon and selecting “Edit This Script”.

Now the following macros can be included in the script.

;----------------------------------
; Navigation in Text
;----------------------------------
!+j::Send ^{Left}
!+l::Send ^{Right}
!j::Send {Left}
!l::Send {Right}
!i::Send {Up}
!k::Send {Down}
!SC027::Send {End}
!h::Send {Home}

The script must be saved and AutoHotkey restarted to activate the macros. If AutoHotkey is running the restart can be done by a right clicking on the AutoHotkey system tray icon and selecting “Reload This Script”

The macros just presented allow the text writer to navigate up, down, left, right, home and end by holding down the Alt key with the left hand and using i, j, k, l as arrow keys and h, : as Home and End. Navigation by word is done by holding down Alt and Shift together and using the keys j and l.

Let’s now have a look at some lines of the code to explain how the macros work:

!j::Send {Left}

The line consists of two parts. The first part is left of “::” and is called the hotkey. The part on the right side is the command executed when the hotkey is pressed. The hotkey in the example “!j” is a compound hotkey that is a press of the j key while the Alt key is being held down. The command “Send {Left}” tells AutoHotkey to send a keypress of the left arrow to the operating system. So the key combination Alt+j has now the same effect as the left arrow.

 !SC027::Send {End}

The simulation of the end key is activated by the key combination Alt+:. Since “:” is a special character in the AutoHotkey macro language the keyboard scan code “SC027″ is used in the hotkey instead.

!+j::Send ^{Left}

“+” is standing for the Shift key and “^” is standing for the Ctrl key. This macro maps Alt+Shift+j to Ctrl+Left which allows to move the cursor one word to the left in the text.

Automatically generate or replace text

Another example for the usefulness of AutoHotkey is its hotstrings function. This functions scans the text input for defined abbreviations and replaces them with some other text on the fly. Below is shown a script compartment with some examples:

;----------------------------------
; Hotstrings for programming
;----------------------------------
#Hotstring c ; Case sensitive
#Hotstring EndChars  .(
:R:pu::public
:R:st::static
:R:pr::private
:R:fi::final
:R:str::string
:R :Str::String
:R  :STr::String
:R:bo::boolean
:R:re::return
:R:imp::implements
:R:sout::System.out.println

When a defined end character is typed (” “, “.” and “(” in this example) AutoHotkey examines the preceding text. If it can find a hotstring it is replaced. Hotstrings are defined by the notation :R:<hotstring>::<replacement> in the AutoHotkey script file. So if the script presented above is active the input of “Str ” will immediately be replaced with “String ” while typing.

Advanced features of AutoHotkey

AutoHotkey has a lot of more features than presented here that make it easy to perform repeated tasks. They can be used for any purpose such as access to installed programs by key, control of instant messengers or gaming for example. Here is the feature list from the AutoHotkey homepage

  • Automate almost anything by sending keystrokes and mouse clicks. You can write a mouse or keyboard macro by hand or use the macro recorder.
  • Create hotkeys for keyboard, joystick, and mouse. Virtually any key, button, or combination can become a hotkey.
  • Expand abbreviations as you type them. For example, typing “btw” can automatically produce “by the way”.
  • Create custom data-entry forms, user interfaces, and menu bars. See GUI for details.
  • Remap keys and buttons on your keyboard, joystick, and mouse.
  • Respond to signals from hand-held remote controls via the WinLIRC client script.
  • Run existing AutoIt v2 scripts and enhance them with new capabilities.
  • Convert any script into an EXE file that can be run on computers that don’t have AutoHotkey installed.
  • 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, including file names copied from an Explorer window.
  • 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).
  • Customize 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 colors (this is intended for legitimate uses such as the alleviation of RSI).
  • Read, write, and parse text files more easily than in other languages.
  • Perform operation(s) upon a set of files that match a wildcard pattern.
  • Work with the registry and INI files.

Comments»

1. michael chalk - May 17, 2008

Hello tilm4nn,

Great to read about someone else using AutoHotKey. i’ve been using it for Auto Text, and it’s just amazing. So good to be able to finally take my AutoText entries outside of ms word .. and even on the road with me ;-]

kind regards, michael

2. Keyboard Improvements | Mind-Manual - October 8, 2008

[...] I’m interested in a script that would take a modifer like Ctrl-Shift and use the JKLI keys for arrow keys cause I often have to navigate toe another part of the text I’m writing and I write a lot often. Found it. [...]

3. chris - February 13, 2009

i dont understand how to do this i made a auto macro for my game.. how would i make a hot key command.. so i can end the auto thing… i just need to no how to make a command to start and end like home as my start key and end as my end key… please send me a email our aim..redneck200513… .skype…sk8terguy3 our yahoo..noobisskater@yahoo.com

4. lageto - February 28, 2009

@Chris: If I understand you right this is the answer to your question about switching the home and end key. script is below:

;BEGIN SCRIPT
$home:: ;S makes the following key a hotkey which activates whatever is after ::
Send {End}
return

$end::
Send {Home}
return
;END SCRIPT

Remember to save with .ahk extension and makes sure filetype is set to *all files

5. tilm4nn - March 16, 2009

@lageto: I don’t think that was his intention. I think he’d like to have a script doing things that can be started with the home and ended with the end key

@chris: Here I have a script I wrote some time ago that is outputting the alphabet while scroll-lock is activated. I think this can be easily extended to do other things and to be activated and deactivated using home and end:

Scrolllock::
{
GetKeyState, scroll, Scrolllock, T
if scroll = U
{
SetScrollLockState, On
SendAlphabet()
}
else
{
SetScrollLockState, Off
}
}

SendAlphabet()
{
waitTime = 120
alphabet = a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
Loop
{
Loop, parse, alphabet, `,
{
SendInput %A_LoopField%
Sleep, %waitTime%
GetKeyState, scroll, Scrolllock, P
if scroll = D
{
SetScrollLockState, Off
Sleep 1000
exit = true
break
}
}
if exit = true
break
}
}