I need to send a standard input or a keycode right after the program starts.
For example
$ program | F3
You'd need to define starts there.
You can insert the sequences of characters sent by your terminal when you press F3 into the terminal device input buffer before running your program with the TIOCSTI ioctl() on some systems like Linux:
{
perl -le 'require "sys/ioctl.ph";
ioctl(STDIN, &TIOCSTI, $_) for split "","@ARGV"' "$(tput kf3)"
program
}
Or you can use something like expect to run your program in a faked terminal layer, wait for it to output some prompt (or any indication that it is started up to the point you expect it to), and send that output then:
expect -c 'spawn -noecho program; expect {>} {send [exec tput kf3]}; interact'
(here expecting a > prompt, replace by some string that program outputs when it's ready to read input).
You can also replace its stdin with a pipe, but if that application expects things like F3 key presses, it's most likely an interactive application so will probably not like it when it's stdin is not a terminal. You can always give it a try anyway:
tput kf3 | program
However then, after program has read the output of tput, it will see end-of-file (nothing more to be read), which it may cause it to exit. With:
{ tput kf3; cat; } | program
We send the output of kf3 and then use cat to forward everything you type on the terminal to program. Then again, it's unlikely to work as most probably program would put the terminal in a mode where the input is sent as soon as you type and disable the echo which it can no longer do as its input is not the terminal.
You could and do that same setting yourself:
saved_settings=$(stty -g)
stty -icanon -echo min 1 time 0
{ tput kf3; cat; } | program
stty "$saved_settings"
expect, if you want the program to read fromstdinyou can put it on the other side, pipes go left to right, sostdouton the left gets connected tostdinon the right, e.g.,echo Input | programwill send the stringinputtostdinofprogram