InputTill(EndString,MaxLen=99) { Loop %MaxLen% { Input ch, I L1 M V, {TAB}{Esc}{BS}{Enter}{LControl}{RControl}{LAlt}{RAlt}{LWin}{RWin}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn} IfNotEqual ErrorLevel,Max, Return ; non-letter key pressed text = %text%%ch% StringReplace text, text, %EndString%,,UseErrorLevel IfNotEqual ErrorLevel,0, Return text } }The script can be extended to abort if the active window changes or there is a mouse click.
A simple application of the InputTill function is automatic pairing of HTML tags, with proper handling of tag options and exceptions (tags, which don't have pairs). It takes, all together, 20 lines of AHK code!
The non erasing Hotstring "<" starts monitoring the keyboard input until ">" is typed. The keys entered in between (until the first space) is sent to the application between "" and ">" and the cursor is positioned in front of this, that is, if you type
AutoTrim Off :*B0:<:: tag := InputTill(">") StringGetPos pos, tag, %A_Space% IfLess pos,0, SetEnv pos,999 StringLeft tag, tag, pos If tag in ,br,p ; add here more exceptions, not needing a pair Return Send % "</" tag ">{Left " StrLen(tag)+3 "}" Return
The delimiters need not be single characters, demonstrated by the following example, where after typing <
:*B0:<<:: tag := InputTill(">>") Send "<</" tag ">>{Left " StrLen(tag)+5 "}" ReturnDoes anyone know a real application for multi character delimiters?
If you have problems with long lines, replace the Input command with
Input k, I L1 M V, ( Join LTrim {TAB}{Esc}{BS}{Enter} {LControl}{RControl}{LAlt}{RAlt}{LWin}{RWin} {F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12} {Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn} )Edit 2005.11.28: Long Input command broken up (Thanks JSLover)