You cannot and you must not for security reasons (as Pekka already pointed out). You will always require a user interaction in between. Additionally imagine the chance of the browser vendors getting sued by users, as various programmatic keyboard events will have led to spoofing attacks.
See this post for alternatives and more details. There is always the flash based copy-and-paste. Here is an elegant example. At the same time it is a testimony why the web is moving away from plugin vendors.
There is a similar security mindset applied in case of the opt-in CORS policy to access remote content programmatically.
The answer is:
There is no way to programmatically trigger input keys in the sandboxed browser environment under normal circumstances.
Bottomline: I am not saying it will not be possible in the future, under special browser-modes and/or privileges towards the end-goal of gaming, or similar user-experiences. However prior to entering such modes, the user will be asked for permissions and risks, similar to the Fullscreen API model. ( I included a google search link, as it neatly includes the relevant text lines in search-result)