JavaScript keyboard simulation in Google Docs

I’m trying to create a script that can automatically type and delete text at the cursor location in Google Docs using JavaScript. Regular keypress event simulation methods don’t seem to work, even when I use jQuery.

While exploring the Google Docs interface, I found out they use a virtual keyboard system. When you click the virtual keyboard buttons, it triggers this function:

D.POb = function(b) {
  this.dispatchEvent(new R(Ud, {keyCode: b}))
};

In this code, Ud appears to be the string “action” and R looks like some kind of Event constructor.

How can I properly trigger this event using JavaScript? Are there any alternative methods to simulate keyboard input in Google Docs that actually work?

honestly the cleanest workaround ive found is using the google docs api instead of trying to hack the frontend. yeah i know its not the same as direct browser manipulation but if you’re doing automation stuff the api is way more reliable and wont break every time they push updates.

Google Docs has pretty aggressive protection against automated input, which is why standard event simulation fails. I’ve encountered this exact issue when trying to build browser extensions that interact with Docs. The virtual keyboard approach you found is interesting but won’t work reliably since those internal functions are obfuscated and change frequently with updates. What I discovered through trial and error is that Google Docs primarily relies on composition events and input events rather than traditional keypress events. You might have better luck with dispatching ‘beforeinput’ and ‘input’ events directly to the contenteditable elements, though even this approach has limitations. Another workaround I’ve seen work is using the clipboard API to paste text programmatically, which Docs generally accepts since it’s considered user-initiated action. However, be aware that Google actively prevents automated interaction for security reasons, so any solution might break with future updates.

I ran into this same problem when working on a document automation project. The obfuscated functions you found are part of Google’s internal API and will break whenever they update their codebase. Instead of trying to reverse engineer their virtual keyboard system, I found success using the execCommand approach combined with focus manipulation. You need to ensure the document has proper focus first, then use document.execCommand(‘insertText’, false, ‘your text here’) which works more consistently than event simulation. For deletion, execCommand(‘delete’) can handle backspace functionality. The key is timing - Google Docs needs a moment to initialize its editor state before accepting programmatic input. I also discovered that wrapping these commands in a setTimeout with even a minimal delay improves reliability significantly. This method bypasses their event filtering since execCommand is treated as a legitimate browser API call rather than simulated user input.