Originally Posted by floobit
If I've understood correctly, the users are logged in over putty from Windows machines to a Ubuntu machine, and they run some text-based/curses-based app which uses F4 to save the database.
Have you considered the answerback sequence? This is often confused with the terminal-type identification, but was originally designed for telex operators to send their own station ids. You program it to send your id, and the terminal will transmit it when you press the "send id" button, or when it receives an answerback enquiry from the computer: a control-E.
For your application you program the answerback to be <ESC> OS, which is what the F4 key sends (at least in xtem). You send it a ctrl-E, and it will be as if the user had typed <ESC> OS.
I tested it with xterm as follows:
Make a file 'redf4':
Tell your xserver to accept these resources:
Run an xterm, and run your app inside it. Note that the cursor should be red.
In that window, find the tty and then run your application
In another window, send the enquiry:
As you're unlikely to own the target terminal, you'll probably need:
printf '\005' > /dev/pts/1
I tried it with various escape sequences to quit Vi and so on, seems to work exactly like you need. (The only thing which appeared strange was you can't put a \015 (newline) at the end of the string, only in the middle.)
sudo sh -c "printf '\005' > /dev/pts/1"
I see you can set the answerback in putty in its config file: http://the.earth.li/~sgtatham/putty/0.61/htmldoc/Chapter4.html#config-answerback
On real terminals you could program the answerbacks in a local mode. In that era it was considered hilarious to program the answerback to something unscrupulous and trigger it when a victim later sat at that terminal. Of course legitimate uses also existed, but it was problematic so it's not implemented in a lot of terminal emulators -- I believe gnome-terminal, for example, doesn't have it.
Hope thats helpful.