Detect keystoke or mouse click while applescript is running?

I’ve searched Applescript and Xcode forums and I haven’t found an answer to a question I have. I only know basic Applescript and Xcode, unfortunately. I am trying to create an application that will run a timer and detect if the user has pressed a keystroke or mouse button before the timer has finished running. The timer uses a Start button to start the timer and maybe a second button to detect a mouse click:

on clicked theObject
	if the name of theObject is "btnStart" then
		tell window of theObject
			set numTimer to 0
			repeat while numTimer is less than 5000000
				-- check if key is pressed or mouse clicked
				set numTimer to numTimer + 1
			end repeat
		end tell
	end if
end clicked

I haven’t been able to get a second button to work while the script is running. I’d like to avoid using third party software (e.g. Extra Suites) if I can so I can publish this app. Is this possible? I’d be grateful for any help.
XCode 3.0, XCode core 921.0

Model: Mac Pro
Browser: Opera/9.52 (Macintosh; Intel Mac OS X; U; en)
Operating System: Mac OS X (10.5)

For something like a timer the fix is simple. Put the timer code in the ‘on idle’ handler. That way your timer works when nothing is going on in the application… but the application will respond to button clicks. Something like this should work…

property endTime : missing value
property theTime : 30 – the seconds for the timer

on clicked
if name of theobject is “startTimer” then
set endTime to current date + theTime
else if name of theobject is “stopTimer” then
set endTime to missing value

on idle
if endTime is not missing value then
set currentTime to current date
if currentTime is greater than endTime then display dialog “The time is up”
return 1

Thanks much!