Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Input Till EndString: auto send pair of HTML tag


  • Please log in to reply
7 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
The following function starts monitoring the keyboard input until a specified text is entered, and returns the string of characters typed. Monitoring gets interrupted if a not alphanumeric key is pressed or a given limit of keystrokes is reached. In these cases an empty string is returned. Matching is not case sensitive unless StringCaseSense has been turned on.
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 the cursor is positioned in front of this, that is, if you type the script writes the closing and moves the cursor to where it was: |, or becomes |. There is an extensible list of exceptions (,br,p), those tags, which don't need a pair.
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 <> the script writes the closing <> and moves the cursor in between: <>|<>.
:*B0:<<::
   tag := InputTill(">>")
   Send "<</" tag ">>{Left " StrLen(tag)+5 "}"
Return
Does 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)

thatOtherGuest
  • Guests
  • Last active:
  • Joined: --
:D

Thank you very much! (I'm the guest of the other thread)

8) 8) 8)

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004
HORRIBLE SCROLLING!!!

Not tested, but shouldn't this work?...

InputTill(EndString,MaxLen=99)
{
	Loop %MaxLen%
	{
		AllKeysUnderTheSun=
		(LTrim Join
			{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}
		)
		Input ch, I L1 M V, %AllKeysUnderTheSun%
		IfNotEqual ErrorLevel,Max, Return   ; non-letter key pressed
		text = %text%%ch%
		StringReplace text, text, %EndString%,,UseErrorLevel
		IfNotEqual ErrorLevel,0, Return text
	}
}
...or even...

InputTill(EndString,MaxLen=99)
{
	Loop %MaxLen%
	{
		Input ch, I L1 M V, 
		(LTrim Join
			{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
	}
}
Testcase (manually merge longline back into one long line)...

longline={TAB}{Esc}{BS}{Enter}{LControl}{RControl}{LAlt}{RAlt}{LWin}{RWin}<Join>
<Me>{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}<Join>
<Me>{Up}{Down}{Home}{End}{PgUp}{PgDn}
AllKeysUnderTheSun=
(LTrim Join
	{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}
)

if (longline!=AllKeysUnderTheSun)
	dont:="don't "
msgbox, %longline%`n`n%AllKeysUnderTheSun%`n`nthey %dont%match

Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Thanks, JSLover! 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}

)


JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

Edit 2005.11.28: Long Input command broken up (Thanks JSLover)

...I meant in the forum, not just in the actual code...you added the new version, but this topic still has horiz scrolling, cuz the initial long line is still there. I didn't have trouble with the long line, in say, my editor (I didn't test it at all), I just meant it makes the topic hard to read in the forum.
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
What browser do you use? In my IE6 long lines are simply wrapped, there is no horizontal scrolling. I guess you could configure your browser to do the same, which is the default with IE6.

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

What browser do you use? In my IE6 long lines are simply wrapped

...I use Netscape 7.2, it's safer than IE, with IE you can get viruses by just visiting a webpage. Unfortunatly, NS don't wrap on all the same chars that IE does...IE wraps on {} (& others) so horiz don't happen often (or ever?) with IE.

I guess you could configure your browser to do the same

...I don't know of any option for that...Netscape/Mozilla does a lot of crappy things...that you can't override (like non-standard windows controls/"standards compliant" html to the max/not wrapping when it should)...but there ain't any better browser for me yet...plus I like the name Netscape. I just wish someone would code a browser based on Netscape 4 (when it used normal windows controls for stuff), just with the new Gecko engine...everyone (but me) hates NS 4 because it "sucks", but I liked the Gui, it just needs the Gecko engine so it can render pages with today's standards (but not 100% strict on "standards" tho...the W3 are not Gods)...NS 6 should've never happened, they shoulda made NS 5 (if it was based on 4 & not a complete re-write like 6 was).
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

...I use Netscape 7.2, it's safer than IE

Any browser, which has a much smaller market share than IE is safer, because it represents a smaller target to malware. You just have to hope it won't catch up, because your security will be lost. Like with FireFox. It has not fewer, only different bugs, flaws. Now, as FireFox gained acceptance, we have equally many exploits for it.