can I detect keydown?

Does anyone know if I can determine the state of a keyboard key?

I want to change the way a script functions depending on what keys are pressed when it’s run. e.g. toggle state (default) force on(option) force off (option shift).

You can probably find an osax that will allow you to do this. Maybe there’s something useful in this list. :wink:

You can also use “if Keys Pressed…”.

Try this - I think this is what you are after…

On run--when the script is run
set myScriptStatus to "start"
repeat until myScriptStatus = "Perform Subroutine"--this will continually repeat as we never set myScriptStatus to anything ever again.
--But we do offer a way out by checking the keys pressed
	set foo to "fee"
	if (keys pressed) contains "F8" then --check to see what keys are pressed
		performsubroutine() of me --if the key we are looking for is one of the keys pressed do whatever comes next in the "if"
		return --quit after performing the subroutine - or not if you don't want to
	end if
end repeat
end run

on performsubroutine() --this is what happens if the option key is one of the keys pressed when checked
	--of course you could put whatever you want your script to do here instead of this
	tell application "Finder"
		activate
		display dialog "Hey, stop pressing my F8 key" with icon stop
	end tell
end performsubroutine

If you want the script to check the keys pressed before acting put the “keys pressed” command soon after the on run, or whatever event, so it will check the keys pressed and act accordingly.

If you want to use two keys as modifiers then try this:

	if (keys pressed) contains "Option" and (keys pressed) contains "Shift" then

I use this to pause and quit out of annoying stay-open scripts that think they own me.

Good luck,

According to Script Debugger, this is the work of Jon’s Commands. Is this correct?

That is correct, and you were correct in pointing neilxt to the OSAX section as Jon’s Command’s is a scripting addition. :oops: Should have looked a little closer at that.

Sooo, if neilxt were to download Jon’s Commands this script would work, otherwise it would be gibberish… Much like my posts sometimes.

Sorry for any inconvenience folks,

Mytzlscript, your posts are very helpful! I was just pointing out the osax connection for the sake of clarification. :slight_smile:

If I’m not mistaken, Jon’s Commands was part of a standard install on some versions of Mac OS, so it might already be present. That’s why it might be easy to overlook. :wink: