繁体   English   中英

Google Docs 模拟键盘

[英]Google Docs simulate keyboard

我需要使用 JavaScript 在 google docs 中模拟键盘,以便能够在光标位置打印或擦除字符。
不幸的是,模拟按键事件的解决方案对我不起作用。 我尝试使用和不使用 jQuery。
经过一番调查,我发现 Google Docs 有虚拟键盘。 单击虚拟键调用此函数:

C.MOa = function(a) {
  this.dispatchEvent(new Q(Td, {keyCode: a}))
};

其中Td是字符串“action”, Q某个 Event 类。
用java脚本发送这个事件的正确方法是什么? 还有其他方法可以在 Google Docs 中模拟键盘吗?

将以下代码粘贴到 google docs 的控制台中。

const input = document.querySelector(".docs-texteventtarget-iframe").contentDocument.activeElement;
    
// Insert the character in the document and trigger the save API call
const eventObj = document.createEvent("Event");
eventObj.initEvent("keypress", true, true);
eventObj.keyCode = 105;
input.dispatchEvent(eventObj);

您将看到在文档中插入了字符“i”。

似乎 Google Docs 有特殊的 iframe 来处理键盘事件。 这是它的内容:

<html>
    <head></head>
    <body spellcheck="false" role="textbox" aria-label="Document content" contenteditable="true" style="background-color: transparent;"></body>
</html>

只需将键盘事件分派到此文档即可在 google doc 上打印字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM