Applescript that responds to key presses? I want to write an AppleScript that 


runs (in a loop) until I press esc
"listens" for keystrokes 1, 2, etc.
executes a given command for a each keypress (running a shell command ddcctl to change secondary monitor brightness/contrast)


I've searched and everything I find relates to simulating a key press.

(If this can be done in bash that's fine but I don't know enough shell scripting to even start...)



UPDATE

Based on this tip, I wrote the shell script below, which works great, but only if I run it within Terminal. Is there any way to run this script 'invisibly', say, from Spotlight or via AppleScript do shell script, and have it respond until I exit?

#!/bin/bash
if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
keypress=''
while [ "$k" != "^[" ]; do
    case "$k" in
        1) ddcctl -d 1 -b 10;;
        2) ddcctl -d 1 -b 20;;
        3) ddcctl -d 1 -b 30;;
        # ...etc...
        0) ddcctl -d 1 -b 100;;
    esac
    k="`cat -v`"
done
if [ -t 0 ]; then stty sane; fi
exit 0
The easiest way of accomplishing this is probably to use Automator to create a Service. The service should just execute your a bash script similar to this:

#!/bin/bash
ddcctl -d 1 -b 10


Then in System Preference under Keyboard, add a keyboard shortcut to activate your Service.

This way your shortcut will work across all programs.

You'll probably need to do this for each of the 10 keyboard shortcuts you need.