Page 1 of 1

SoundBeep after Loop, break

Posted: 05 Apr 2020, 16:12
by moonmanmacros
Here is the code for one of my most used scripts which turns double taps of the Alt keys into Copy paste functions. I want to add a sound beep at the end of the copy loop to confirm that the script is running and working whenever I double tap LAlt, but SoundBeep will trigger every time I release the alt key even if doing so does not triggert hot key. It seems to happen regardless of where I put SoundBeep. Is there another way to verify that my double tap has sucessfully copied to the clipboard? The only other way I have pulled it off is a total pain.

~LAlt::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 200)
Run, %A_ScriptDir%\copysoundbeep.exe
return

Code: Select all

~LAlt up::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 200)
	clipboard := ""  ; empty clipboard
Loop
{
	Send, ^c
	ClipWait, 1		; wait for the clipboard to contain data
	if (!ErrorLevel)    ; If NOT ErrorLevel clipwait found data on the clipboard
    break
    ;SoundBeep, 750, 500
}
Return

~RAlt::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 200)
	Send, ^v
Return

Re: SoundBeep after Loop, break

Posted: 05 Apr 2020, 16:59
by boiler
The only command you have executing on the condition that it was double-clicked is the clipboard := "" line. So your script is executing everything below it (the loop) every time you release LAlt whether it was double tapped or not. You need to use { } to designate a code block after the If statement so that all of it gets executed only when the condition is true:

Code: Select all

~LAlt up::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 200)
{
	clipboard := ""  ; empty clipboard
	Loop
	{
		Send, ^c
		ClipWait, 1		; wait for the clipboard to contain data
		if (!ErrorLevel)    ; If NOT ErrorLevel clipwait found data on the clipboard
		break
		SoundBeep, 750, 500
	}
}
Return

~RAlt::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 200)
	Send, ^v
Return

Re: SoundBeep after Loop, break

Posted: 05 Apr 2020, 23:11
by moonmanmacros
I did not realize it would be that easy to just place one set of brackets inside another. It now works great! Guess im still to used to the double and triple quotes that you would normal use in Excel for something like this.

Code: Select all

~LAlt up::
If (A_ThisHotkey = A_PriorHotkey and A_TimeSincePriorHotkey < 250)
{
	clipboard := ""  ; empty clipboard
Loop
{
	Send, ^c
	ClipWait, 1		; wait for the clipboard to contain data
	if (!ErrorLevel)    ; If NOT ErrorLevel clipwait found data on the clipboard
   break
}
SoundBeep, 750, 500
}
Return