End hotstring/input with double press of a specified key.

Post your working scripts, libraries and tools
NiptheZephyr
Posts: 9
Joined: 01 Aug 2020, 21:33

End hotstring/input with double press of a specified key.

01 Aug 2020, 22:22

Hi everyone. My first post on this forum, so pardon me if the formatting is off. Came up with a utility that aids my speed, for when needing to store what you type in a variable. Using Input is a great tool, but if you use {space} as one of your end keys, you can't store multiple words in your string. Easiest workaround is to use another end key, like {enter}, but to me this doesn't flow as well for me. I had tried {space 2} as an end key initially, but that didn't work. If I entered it poorly and it actually works, well then, just ignore the following. Googling around didn't provide an answer, so I came up with some code that is the equivalent as using space twice as its end key. Just wanted to post it here in case others find it useful, or to give them something to modify for their own benefit.

Code: Select all

;AHK script with hotstring/input that can be ended by double pressing space key.

::test please:: ;trigger phrase

finalVar := "" ;resets it if used more than once in a row.

inputText := "" ;resets it if used more than once in a row.

Loop
{
	finalVar := inputText ;variable to store final input/string for whatever usage. Please note, your final variable will have a space at the beginning of it. Uncertain how to loop and put spaces between the words one types without it ending up having a space at the beginning due to this line.
	
		Input, inputText, T5, {space}{enter} ;type what you want in the variable here
			if (ErrorLevel = "Timeout") ;general catchall to allow timeout
				{
					MsgBox, Timed Out.
					finalVar := finalVar " " inputText ;Allows for the capture of words with a space in between.
					Break
				}

			if (ErrorLevel = "Endkey:enter")
				{
					finalVar := finalVar " " inputText ;Allows for the capture of words with a space in between.
					Break
				}

			if (ErrorLevel = "Endkey:space") and (inputText = "") ;Equivalent of pressing space twice for an end key
				{
					finalVar := finalVar " " inputText ;Allows for the capture of words with a space in between.
					Break
				}

			Else If (ErrorLevel = "Endkey:space")  ;restart the loop
				{
					inputText := finalVar " " inputText
				}
		}

	MsgBox, %inputText% ;Test to confirm what the last inputText was.
	MsgBox, %finalVar% ;Test to confirm final variable contains everything typed.
Have a good one!

NiptheZephyr
NiptheZephyr
Posts: 9
Joined: 01 Aug 2020, 21:33

Re: End hotstring/input with double press of a specified key.

09 Aug 2020, 09:29

Cleaned it up, no longer has a space at the beginning.

Code: Select all

;AHK script with hotstring/input that can be ended by double pressing space key.

::demo2:: ;trigger phrase

finalVar := "" ;resets it if used more than once in a row. Output variable, which stores the building string between loops.
inputText := "" ;resets it if used more than once in a row.

Loop
{
		Input, inputText, T5, {space}{enter} ;type what you want in the variable here
			if (ErrorLevel = "Timeout") ;general catchall to allow timeout
				{
					MsgBox, Timed Out.
					finalVar := finalVar " " inputText ;Allows for the capture of words with a space in between.
					Break
				}

			if (ErrorLevel = "Endkey:enter")
				{
					if A_Index = 1 ;If first runthrough ends w/ "enter", saves it.
						{
						finalVar := inputText
						Break
						}
					Else ;Any other run throughs takes the prior saved and adds it on with a space between.
						{
						finalVar := finalVar " " inputText
						Break
						}
				}

			if (ErrorLevel = "Endkey:space") and (inputText = "") ;Equivalent of pressing space twice for an end key
				{
					Break
				}

			Else If (ErrorLevel = "Endkey:space")  ;restart the loop
				{
					if A_Index = 1 ;
						{
						finalVar := inputText
						}
					Else 
						{
						finalVar := finalVar " " inputText
						}
				}
		}
	MsgBox, %finalVar% ;Test to confirm final variable contains everything typed.
Return

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Pscyking, robodesign and 24 guests