Can a Mac be programmed to simulate pressing a key at a certain rate via software?

Here's an AppleScript to do what you want:

set i to 0
repeat while i < 15
    set i to i + 1
    delay 5
    tell application "System Events" to keystroke "k"
end repeat

You can inline it in a shell script like this:

echo "set i to 0
repeat while i < 15
set i to i + 1
delay 5
tell application \"System Events\" to keystroke \"k\"
end repeat" | osascript

(Thanks to @houbysoft for the echo "script" | osacript syntax!)


I found a way to do this in a bash script:

#!/bin/sh
# Simulates hitting a key on OS X
# http://apple.stackexchange.com/a/63899/72339

echo "tell application \"System Events\" to keystroke \"$1\"" | osascript

Save as hitkey, chmod +x hitkey, hitkey k to hit K.

From there it's simple to use Automator to loop over the script several times or make an iCal alarm to initiate the script at a specific time.

Automator document types