Hi.
Now i have no time for read the script, but i can say, you can program F-keys by making aliases named f?. Example:
alias f1 echo -a Dont press your keyboard while the computer is turned on, or you will smile.
will echo "Dont press your keyboard while the computer is turned on, or you will smile." into the active window when you press F1.