13. Blocks and Nesting

If you've done some programming you're accustomed to dividing your code into blocks with special symbols. HotkeyNet's scripts are different. They contain blocks but there isn't any way for you to mark them. Instead HotkeyNet figures out where the blocks are by applying rules.

Here's an example. Suppose you write

<Hotkey F1>
<If ActiveWinIs w1>
<Key A>
<Key B>
<If ActiveWinIs w2>
<Key C>
<Key D>

When HotkeyNet loads that definition, it divides the code into blocks as shown by the indentation in the following screenshot.

The screenshot shows one of the most useful features of the program, the "Show hotkey definition" window which is reached from the Actions Menu. This window shows you how HotkeyNet interprets what you wrote. Often the program's interpretation is different from what you intended.

Note that HotkeyNet added SendPC blocks using the local PC as the default.

How does HotkeyNet decide which blocks nest inside which other blocks? It follows two rules. First, blocks nest in the following order:

Hotkey definitions enclose
Toggle blocks which enclose
If and Else blocks which enclose
SendPC blocks.

You can't change that order. It's built into the program.

Second, a block ends when another block of the same or "larger" type is found in the file. For example, a hotkey definition ends when the next hotkey definition begins. A toggle block ends when the next toggle begins or when the enclosing hotkey definition ends.

The second rule means you cannot nest a block inside another block of the same kind. Specifically, you cannot nest an If-block inside another If-block. You can work around this limitation by putting the controlled block of the outer If-statement in a separate hotkey and calling that hotkey with DoHotkey.

HotkeyNet can almost always figure out where a block ends without any possibility of ambiguity. However there is one exception. After an If-block you may want to add instructions that should always execute without regard for conditions. In this case you'll need to write <EndIf> before those instructions so HotkeyNet knows they aren't part of the If-block.

All commands that perform actions nest at the lowest level inside SendPC blocks. If you try to write an action outside a SendPC block, HotkeyNet will assume you meant to place it inside the default SendPC block which gets sent to the local PC.

Next page

This page was last revised on April 15, 2009