Repeating codes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Repeating codes

04 May 2016, 20:05

Hello again. Can someone pls teach me how to do this? I want to set a hot key like control h to show me an input box where numbers are only allowed to 1000, then it will repeat this code enter down space based on the user's input. Pls help me. Example i will put 100 in the input box and what will it do is to execute enter down space a hundred times. Thank you for those who'll care.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Repeating codes

04 May 2016, 20:13

Try this:

Code: Select all

While (Count < 1) or (Count > 1000)
{
    InputBox, Count, Repetitions, Enter a number from 1 to 1000:
    IfEqual, ErrorLevel, 1, ExitApp ; Cancel button pressed
}
Loop, %Count%
    Send, {Enter}{Down}{Space
I hope that helps.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Repeating codes

04 May 2016, 20:39

Or try this - takes into consideration dumb users that fills in non-numeric values:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

defaultValue := 1
timeOutSeconds := 5

; Ctrl+H
^h::
	InputBox, userInput, Enter a number, Enter a number between 1 and 1000, , 400, Height, , , , timeOutSeconds, 100
	If (errorlevel = 2) {
		MsgBox, Time out - you failed to decide for a number....
		Return
	}
	
	; Check if text is a valid number
	userInput := 0 + userInput
	if not userInput is number
	{
		MsgBox, %userInput% is a valid number
		Return
	}
	
	; Check if number is between the limits
	If (userInput > 1000)
	{
		MsgBox, %userInput% bigger than thousand.
		Return
	}
	
	Loop {
		Send, {Enter}
		Send, {Down}
		Send, % " "
	} Until A_index = userInput
	
Return
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

05 May 2016, 01:09

After testing the two scripts above, i felt that the second one better suits my need. Thanks for the care both of you guys. But, there is one thing, the cancel button executes the same execution as Ok button in the second script. How to fix that?
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

05 May 2016, 01:11

wolf_II wrote:Try this:

Code: Select all

While (Count < 1) or (Count > 1000)
{
    InputBox, Count, Repetitions, Enter a number from 1 to 1000:
    IfEqual, ErrorLevel, 1, ExitApp ; Cancel button pressed
}
Loop, %Count%
    Send, {Enter}{Down}{Space
I hope that helps.
I had used a hotkey to this. But whenever i pressed the control m again. There's no input box appears. It will execute the loop instantly.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Repeating codes

05 May 2016, 02:31

Try this: (can deal with non-numeric input)

Code: Select all

^m:: ; hotkey
    Count := 0
    While (Count < 1) or (Count > 1000)
    {
        InputBox, Count, Repetitions, Enter a number from 1 to 1000:
        IfEqual, ErrorLevel, 1, Return ; Cancel button pressed
    }
    Loop, %Count%
        Send, {Enter}{Down}{Space}
Return
I hope that helps.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Repeating codes

05 May 2016, 07:52

InputBox sets errorlevel = 1 when Cancel button was pressed, so:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

defaultValue := 1
timeOutSeconds := 5

; Ctrl+H
^h::
	InputBox, userInput, Enter a number, Enter a number between 1 and 1000, , 400, Height, , , , timeOutSeconds, 100
	If (errorlevel = 2) {
		MsgBox, Time out - you failed to decide for a number....
		Return
	} else If (errorlevel) {
		MsgBox, Canceled by user.
		Return
	}
	
	; Check if text is a valid number
	userInput := 0 + userInput
	if not userInput is number
	{
		MsgBox, %userInput% is a valid number
		Return
	}
	
	; Check if number is between the limits
	If (userInput > 1000)
	{
		MsgBox, %userInput% bigger than thousand.
		Return
	}
	
	Loop {
		Send, {Enter}
		Send, {Down}
		Send, % " "
	} Until A_index = userInput
	
Return
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

06 May 2016, 19:02

Almost_there wrote:InputBox sets errorlevel = 1 when Cancel button was pressed, so:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

defaultValue := 1
timeOutSeconds := 5

; Ctrl+H
^h::
	InputBox, userInput, Enter a number, Enter a number between 1 and 1000, , 400, Height, , , , timeOutSeconds, 100
	If (errorlevel = 2) {
		MsgBox, Time out - you failed to decide for a number....
		Return
	} else If (errorlevel) {
		MsgBox, Canceled by user.
		Return
	}
	
	; Check if text is a valid number
	userInput := 0 + userInput
	if not userInput is number
	{
		MsgBox, %userInput% is a valid number
		Return
	}
	
	; Check if number is between the limits
	If (userInput > 1000)
	{
		MsgBox, %userInput% bigger than thousand.
		Return
	}
	
	Loop {
		Send, {Enter}
		Send, {Down}
		Send, % " "
	} Until A_index = userInput
	
Return
I love it! Works perfectly as i wanted. My only problem now is i can't stop the process when i suddenly know that i'm pasting a wrong word. I have to wait for the process to end, i want an interruption key. Now, what i want to do is to stop the loop when i press space bar or enter. Pls help me. Can i put that addition codes inside the existing block because i only pasted this whole script inside my existing script and i'm worried if the additional codes might affect my other script.
keamo
Posts: 2
Joined: 06 May 2016, 20:10

Re: Repeating codes

06 May 2016, 21:48

neosickle wrote: I love it! Works perfectly as i wanted. My only problem now is i can't stop the process when i suddenly know that i'm pasting a wrong word. I have to wait for the process to end, i want an interruption key. Now, what i want to do is to stop the loop when i press space bar or enter. Pls help me. Can i put that addition codes inside the existing block because i only pasted this whole script inside my existing script and i'm worried if the additional codes might affect my other script.
Sounds like you just explained the macro to build, a key that ends the process and enables another w/o the loops? :?:
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

06 May 2016, 23:12

Nope buddy, just a key to stop the process when space bar or enter key was pressed.
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

07 May 2016, 13:34

Loop {
Send, {Enter}
Send, {Down}
Send, % " "
}

How can i add space bar to break the process inside the block? I used if {Space} break but don't work. Pls help me.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Repeating codes

07 May 2016, 13:48

Like this (untested)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

defaultValue := 1
timeOutSeconds := 5

; Ctrl+H
^h::
	InputBox, userInput, Enter a number, Enter a number between 1 and 1000, , 400, Height, , , , timeOutSeconds, 100
	If (errorlevel = 2) {
		MsgBox, Time out - you failed to decide for a number....
		Return
	} else If (errorlevel) {
		MsgBox, Canceled by user.
		Return
	}
	
	; Check if text is a valid number
	userInput := 0 + userInput
	if not userInput is number
	{
		MsgBox, %userInput% is a valid number
		Return
	}
	
	; Check if number is between the limits
	If (userInput > 1000)
	{
		MsgBox, %userInput% bigger than thousand.
		Return
	}
	
	Loop {
		If GetKeyState("Space")
			Return
		Send, {Enter}
		Send, {Down}
		Send, % " "
	} Until A_index = userInput
	
Return
neosickle
Posts: 42
Joined: 01 May 2016, 14:02

Re: Repeating codes

08 May 2016, 23:09

Almost_there wrote:Like this (untested)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

defaultValue := 1
timeOutSeconds := 5

; Ctrl+H
^h::
	InputBox, userInput, Enter a number, Enter a number between 1 and 1000, , 400, Height, , , , timeOutSeconds, 100
	If (errorlevel = 2) {
		MsgBox, Time out - you failed to decide for a number....
		Return
	} else If (errorlevel) {
		MsgBox, Canceled by user.
		Return
	}
	
	; Check if text is a valid number
	userInput := 0 + userInput
	if not userInput is number
	{
		MsgBox, %userInput% is a valid number
		Return
	}
	
	; Check if number is between the limits
	If (userInput > 1000)
	{
		MsgBox, %userInput% bigger than thousand.
		Return
	}
	Loop {
		If GetKeyState("Space")
			Return
		Send, {Enter}
		Send, {Down}
		Send, % " "
	} Until A_index = userInput
	
Return
Perfect! Solved my problem. Thank you very much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: just me, Rohwedder and 170 guests