Wikia

Delphi Programming

AutoHotKey

2,918pages on
this wiki
Talk0

AutoHotKey is a free tool that can do quite a lot. In this context we will focus on the use in connection with Delphi.

Adding Live Template like support to older Delphi versionsEdit

You are forced to stick with older Delphi versions for whatever reason but you have seen the Live Template feature introduced with Delphi 2006, now you miss them whenever you use Delphi 7 or older. Before we get into this any further: Are you aware of the Code Templates that are a feature of the Delphi IDE since at least Delphi 5? See the Editor Options dialog on the Code Insight tab for these. You activate them by pressing Ctrl+j. If you have already typed the template name, it will automatically be activated, if you have typed nothing or only part of the name, you will get a pop up list where you can choose the template to activate. Esc will abort that function.

Are you still missing the Live Templates? AutoHotKey to the rescue:

The first thing we want to do is enable this functionality only when in the Delphi editor window. This is done by placing all the so called HotStirngs between

#IfWinActive ahk_class TEditWindow

and

#IfWinActive

(TEditWindow is the class name of the Editor window used in Delphi 7 and Delphi 5, I have not tried other Delphi versions but I guess this should work with all versions between Delphi 2 and 7.)


Let's start with something simple, a for loop. We want to type for followed by a space which should automatcially be replace by

for i := 0 to List.Count - 1 do

where the cursor should then be moved to the variable name and select it so we can easily edit it. Also we want forr to do the same for a reverse for loop. The following HotString definition does these:

#IfWinActive ahk_class TEditWindow
:O:for::for I := 0 to List.Count - 1 do{left 26}+{left}
:O:forr::for I := List.Count - 1 downto 0 do{left 30}+{left
#IfWinActive



This of course is more like the Code Templates that are available in the IDE anyway than the Live Templates of later IDEs so, maybe if we add a little bit of GUI:

#IfWinActive ahk_class TEditWindow

:O:for::
  initforgui("i", "0", "List.Count - 1")
  Gui, Add, Button, x146 y167 w70 h20 , Cancel
  Gui, Add, Button, x66 y167 w70 h20 default gForOk, OK
  GuiControl, Focus, NameEdit
  Gui, Show, , FOR statement
return

ForOk:
  gui, submit
  gui, destroy
  send for %VarName% := %StartValue% to %EndValue% do begin{enter}
  send {enter}
  send end;
  send {left 2}{up}
return

:O:forr::
  initforgui("i", "List.Count - 1", "0")
  Gui, Add, Button, x146 y167 w70 h20 , Cancel
  Gui, Add, Button, x66 y167 w70 h20 default gForrOk, OK
  GuiControl, Focus, NameEdit
  Gui, Show, , FOR statement
return

ForrOk:
  gui, submit
  gui, destroy
  send for %VarName% := %StartValue% downto %EndValue% do begin{enter}
  send {enter}
  send end;
  send {left 2}{up}
return

initforgui(_VarName, _StartValue, _EndValue)
{
  Gui, Add, Text, x6 y7 w210 h20 , Variable Name
  Gui, Add, Edit, x6 y27 w210 h20 vVarName, %_VarName%
  Gui, Add, Text, x6 y57 w210 h20 , Start Value
  Gui, Add, Edit, x6 y77 w210 h20 vStartValue, %_StartValue%
  Gui, Add, Text, x6 y107 w210 h20 , End Value
  Gui, Add, Edit, x6 y127 w210 h20 VEndValue, %_EndValue%
}
  
GuiClose:
  gui, Cancel
  gui, destroy
Return

ButtonCancel:
  gui, Cancel
  gui, destroy
return

ButtonOK:
  gui, submit
  gui, destroy
return

#IfWinActive

As you can see this looks quite a lot like programming and still does not allow automatically declaring the for loop variable. But as it said in the title: We're only adding Live Template like support.

Making Umlauts available while using a non-German keyboard layoutEdit

You might be using a German keyboard layout and found that it is not quite ideal for programming. Some often used special characters like [ ] { } ; are only available via modifier keys while the Umlauts (ä ö etc.) are available with only one keystroke but only used very infrequently in programming.

You could install a different keyboard layout (e.g. US or UK layout) which makes the Umlauts disappear and moves the special characters to more convenient keys (e.g. the ö key becomes a semicolon) but then you won't have the Umlauts available any more which makes it difficult to write a German user interface. Having multiple keyboard layouts and switching between them using the keyboard (Alt+Shift) works but then the layout is application specific so you end up getting confused about which layout is currently active.

Enter AutoHotKey: This tool allows you to configure hotkeys that can do many things like starting a program displaying dialogs or sending keys to applications. In the following example we will use that functionality to make Umlauts available even though we are using a UK keyboard layout. The Umlauts on a German keyboard use the keys that are occupied by semicolon (ö), single quote (ä) and opening square bracket (ü), also there is the special sz (ß) which occupies the key used by the dash (right between the number zero). The following AutoHotKey script will remap these keys back to the Umlauts when you press the AltGr key in in addition to the normal key combinations:

<^>!;::Send ö
<^>!'::Send ä
<^>![::Send ü

<^>!-::Send ß

+<^>!;::Send Ö
+<^>!'::Send Ä
+<^>![::Send Ü

The first line remaps the AltGr+<semicolon> key to send the Umlaut ö to the current application, the following lines do the same for the Umlauts ä and ü and the special character ß. The last three lines remap Shift+AltGr+<semicolon> and the other keys respectively to the upper case Umlauts Ö Ä and Ü.

Of course if you are used to using a German keyboard layout, switching to the UK layout may confuse you at first (in particular that the opening and closing braces () are moved to the right by one key to make space for the * character and of course there is always the z vs. y issue) but once you get used to it, you might find that writing code becomes much more fluent.

Around Wikia's network

Random Wiki