Display dialog when key is pressed

I’ve got a repeat process that repeats endlessly

I’ve found that a dialog box will pause a repeat, then continue where it left off when you hit OK.

My questions are
1)Is there a way to display dialog when a specific key is pressed?
2)Is there a better way to pause a repeat function?


if keystroke "1" then
	display dialog "Continue?"

end if


you can control a repeat loop with the gave up parameter of display dialog, for example

repeat 10 times
	set {button returned:bRet, gave up:gaveUp} to display dialog "Continue" buttons {"No", "Yes"} default button 2 giving up after 2
	if bRet is "No" then exit repeat
	delay 5
end repeat
if gaveUp then
	display dialog "timed out"
else if bRet is "No" then
	display dialog "Aborted"
end if


If you create a service that create a file with a given name, or an Apple Script that do the same, when a specific key is hit. (script installed via some utility on a hotkey).

Then you could check for the existence of that file in the repeat loop-
Script one goes like this and is installed on some hotkey. Which maybe the other script should do for you if feasable ( I really don’t know.)

set pauseFile to (path to desktop) & "Mypause.txt" as text
do shell script "touch " & quoted form of POSIX path of pauseFile 

The main script with the loop contains something like this:

set pauseFile to (path to desktop) & "Mypause.txt" as text

			pauseFile as alias  ” generates err condition if pauseFile doesn't exist
				display dialog "Continue?"
				do shell script "rm " & quoted form of pauseFile
			on error
					do shell script "rm " & quoted form of pauseFile
			end try	
		end try
		” rest of your repeat loop goes here. You get straight here if no pausefile.

There are also some initiating to be done, to ensure the state of the script when starting up, like
removing any existent previous pause file (If your script died before removing it.

Best regards