You can determine the character sequence emitted by a key by pressing
Ctrl-v at the command line, then pressing the key you're
interested in. On my system for F12, I get
Esc. Different types of terminals or terminal emulators can emit
different codes for the same key.
At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.
Now, when you press F12, you'll get "foobar" on the command line ready for further
editing. If you wanted a keystroke to enter a
command immediately, you can add a newline:
The man page for bash contains this
bind [-m keymap] -x keyseq:shell-command
bind [-m keymap] keyseq:function-name
If your shell is not bash, I suggest scrutinizing the man page for your preferred shell for equivalent capability.