Dispatch key events to fill an input field

263 views
Skip to first unread message

Ben Bracha

unread,
Aug 21, 2023, 8:17:13 AM8/21/23
to chrome-debugging-protocol
Hi,

I'm trying to automate input-filling by dispatching key events using the CDP.
Subscribing to DOM events, I see the events being fired with "trusted:true", but the input field on the page remains empty.

Here are the commands I send, where sendCommandPromise is just a simple promise wrapper around chrome.debugger.sendCommand

await sendCommandPromise(customerTabId, 'Input.dispatchKeyEvent', {
type: 'keyDown',
key: 'a',
code: 'KeyA',
});
await sendCommandPromise(customerTabId, 'Input.dispatchKeyEvent', {
type: 'keyUp',
key: 'a',
code: 'KeyA',
});

What am I missing here?

Ophir Back

unread,
Aug 21, 2023, 8:29:52 AM8/21/23
to Ben Bracha, chrome-debugging-protocol
In JavaScript in general, the event that triggers the actual writing of the text into an input / textarea (and other) element is the event with the `KeyPress`  type.
Likewise, in CDP, the event that is required to trigger the actual writing is the `char` one. You need to have `keyUp` -> `char` -> `keyDown`.

Also, there's the `text` field in the parameters of `Input.dispatchKeyEvent` that should be filled for the `char` event (and only in this event). 

Overall, here's the type, text, key and code of writing down the letter "a" successfully with CDP:
"keyDown", undefined, "a", "KeyA"
"char", "a", "a" , "KeyA"
"keyUp", undefined, "a", "KeyA"

I hope it makes sense. If you have any questions, please let me know. 


--
You received this message because you are subscribed to the Google Groups "chrome-debugging-protocol" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chrome-debugging-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chrome-debugging-protocol/f0a1e619-be8d-4038-80ab-a53b6fdf51ddn%40googlegroups.com.

This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.

Ben Bracha

unread,
Aug 21, 2023, 8:34:38 AM8/21/23
to Ophir Back, chrome-debugging-protocol
This seems to work. Thanks!

Ben Bracha

unread,
Aug 21, 2023, 8:36:53 AM8/21/23
to Ophir Back, chrome-debugging-protocol
Actually it seems to work when sending only the "char" event, no need for the keyup/keydown

Ophir Back

unread,
Aug 21, 2023, 8:38:58 AM8/21/23
to Ben Bracha, chrome-debugging-protocol
Yeah, the "char" event is the one that triggers writing the character.
However, since in some cases there might be event listeners in place (that can also expect "keyDown" / "keyUp"), it's better to also send them.


Ben Bracha

unread,
Aug 21, 2023, 8:40:06 AM8/21/23
to Ophir Back, chrome-debugging-protocol
Got it. Ok, thanks Ophir 
Reply all
Reply to author
Forward
0 new messages