Correct Errant Reversed Letters with this Hotkey Trick

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
Posts: 119
Joined: 01 Aug 2016, 18:17

Correct Errant Reversed Letters with this Hotkey Trick

24 Sep 2019, 14:26

I don't know if you have this problem, but occasionally my fingers get confused and type two letters in the wrong order. I probably use this Hotkey technique a couple of times during a blogging session—whenever my mild dyslexia kicks in and leaves me with the swapped letters. The AutoCorrect.ahk script repairs a number of such mistakes but a few make it on to my computer screen. I could have written a Hotkey routine which swapped pre-selected (highlighted) letters, but, rather than taking the time to select the characters by dragging the mouse across them, I wanted to merely place the cursor between the two errant letters.

This use of the Send command makes it incredibly easy to transpose any two letters. Simply place the cursor between them and hit ALT+R (as shown in the figure.)


In Chapter Nine, “AutoHotkey Windows Clipboard Techniques for Swapping Letters: Delving into Windows Clipboard Hotkey Tricks for Switching Mistyped Letters in Any Document or Text Editor” of the book AutoHotkey Hotkey Techniques, I discuss how to select portions of text without dragging the mouse with the left button down.

This character reversing routine highlights the target letters using the SendInput command, then employs the SubStr() function to exchange the letter positions:

Code: Select all

  OldClipboard := ClipboardAll
  Clipboard = ;clears the Clipboard
  SendInput {Left}+{Right 2}
  SendInput, ^x
  ClipWait 0 ;pause for Clipboard data
  If ErrorLevel
    MsgBox, No text selected!
  SwappedLetters := SubStr(Clipboard,2) . SubStr(Clipboard,1,1)
  SendInput, %SwappedLetters%
  SendInput {Left}
  Clipboard := OldClipboard
This routine eliminates the needs to pre-select text with the mouse, although you must set the cursor between the two misplaced characters. In this case, the SendInput command moves the cursor one space to the left ({Left}) before programmatically holding down the SHIFT key (+) and highlighting two characters to the right (+{Right 2}). The SendInput command then cuts (CTRL+X) the characters and stores them in the Windows Clipboard.

This technique uses the SubStr() function to swap the two letters then inserts them back in place of the original letters using another SendInput command. The SendInput {Left} statement restores the cursor to the center position.

For a more detailed discussion of this technique, additional tips, and a look at the following word swapping tricks, see Chapter Nine and Chapter Ten of AutoHotkey Hotkey Techniques.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: thebat and 3 guests