Interrupt Sleep: Toggle or Button Press

Post your working scripts, libraries and tools
doctorafterman
Posts: 25
Joined: 26 Aug 2019, 08:45

Interrupt Sleep: Toggle or Button Press

03 Sep 2019, 10:34

I wrote the following code so I could interrupt sleep by toggling the hotkey or change the sleep period to an alternate (lesser) amount of time by pressing the assigned key (LCtrl).

I don't know if anyone will find this particularly useful but it's a combination of a lot of pieces I've found here and there. This version is an example script with input boxes, you can replaces these lines with Variable:= for actual use or even just do the math yourself and remove the variables all together.

The resolution refines how long you need to hold the assigned key (LCtrl) for the loop to recognize and break sleep.

Code: Select all

#MaxThreadsPerHotkey 2
return

IsKeyPressed(v_KeyName)
{
GetKeyState, state, %v_KeyName%, P
If state = D
{
Return 1
}
Return 0
}

^+s::
Toggle := !Toggle
If (!Toggle)
	{
	return
	} 
SleepCount:=0
DefaultSleep:=0
AltSleep:=0
Resolution:=0
Loop
{
InputBox DefaultSleep, , Seconds for DefaultSleep?, , , , , , , , 30
sleep 100
InputBox AltSleep, , Seconds for AltSleep?, , , , , , , , 10
sleep 100
InputBox Resolution, , Resolution for LCtrl press (Multiple of 2), , , , , , , , 32
Loop, % DefaultSleep*Resolution
{
if IsKeyPressed("LCtrl")
	{
	Loop
		{
		if SleepCount >= % AltSleep*Resolution
			{
			goto BreakSleep
			}
		Sleep % 1000/Resolution
		SleepCount:= SleepCount +1
		If (!Toggle)
			{
			MsgBox Script stopped.
			return
			}
		}
	}
Else
Sleep % 1000/Resolution
SleepCount:= SleepCount +1
If (!Toggle)
	{
	MsgBox Script stopped.
	return
	}
}
BreakSleep:
MsgBox Sleeping Finished.
}
return

Return to “Scripts and Functions”

Who is online

Users browsing this forum: catdog557 and 53 guests