简体   繁体   English

如何模拟适用于 JavaScript 中的字母数字和不可打印字符的跨浏览器按键?

[英]How do I simulate a cross browser key press that works for alphanumeric and non-printable characters in JavaScript?

This is a more specific question of Is it possible to simulate key press events programmatically?这是一个更具体的问题是否可以以编程方式模拟按键事件? Every answer is missing the following information in one way or another, reducing the question's value.每个答案都以某种方式缺少以下信息,从而降低了问题的价值。 I've tried three things on there already that didn't work in modern browsers, or were advised against by official sources.我已经在那里尝试了三件在现代浏览器中不起作用的东西,或者官方消息人士建议不要这样做。

Here is what I'm looking for:这是我要找的:

  1. At the time of answering, every example should work in modern versions of Firefox and Chrome.在回答时,每个示例都应该在现代版本的 FirefoxChrome 中工作。
  2. No examples include deprecated objects, fields, properties, or functions.没有示例包括弃用的对象、字段、属性或函数。
  3. Every answer should include a way of typing a printable character in a textfield and textarea.每个答案都应包括在文本字段和文本区域中键入可打印字符的方法。 As mentioned in a comment below, this can't be done.正如下面的评论中提到的,这是无法做到的。
  4. Every answer should include a way to type an alphanumeric character when an input does not have focus.每个答案都应包括在输入没有焦点时键入字母数字字符的方法。 eg, "a", "b", "c", etc.例如,“a”、“b”、“c”等。
  5. Every answer should include a way to type a non-printable character.每个答案都应该包括一种输入不可打印字符的方法。 eg, page down, left arrow, the F1 key (if this isn't possible, it's okay to state so), Enter, etc.例如,向下翻页、向左箭头、F1 键(如果这不可能,也可以说明)、Enter 等。
  6. Every answer should include a link to all the "codes" needed to simulate alphanumeric characters and non-printable characters, or even better, embed that info into the answer.每个答案都应包含指向模拟字母数字字符和不可打印字符所需的所有“代码”的链接,或者更好的是,将该信息嵌入到答案中。 I'm using the term "codes" loosely here: I mean whatever term is appropriate to to satisfy the 2nd point.我在这里松散地使用术语“代码”:我的意思是任何适合满足第二点的术语。

This solution fills all of the criteria:此解决方案满足所有条件:

  1. This example works in modern versions of chrome and firefox ( https://caniuse.com/dispatchevent ) ( https://caniuse.com/keyboardevent-key )此示例适用于现代版本的 chrome 和 firefox ( https://caniuse.com/dispatchevent ) ( https://caniuse.com/keyboardevent-key )
  2. This example does not include anything deprecated此示例不包含任何已弃用的内容
  3. N/A不适用
  4. Alphanumeric characters are supported支持字母数字字符
  5. This is possible with this solution这个解决方案是可能的
  6. You can set key equal to one of these values https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values (I would embed this, however, there are a lot of codes)您可以将key设置为等于这些值之一https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values (我会嵌入这个,但是,有很多代码)
  • Ctrl : Use the ctrlKey option (boolean) Ctrl :使用ctrlKey选项(布尔值)
  • Alt : Use the altKey option (boolean) Alt :使用altKey选项(布尔值)
  • Shift : Use the shiftKey option (boolean) Shift :使用shiftKey选项(布尔值)
  • Meta Key/Command Key : use the metaKey option (boolean) Meta键/命令键:使用metaKey选项(布尔)

 function simulateKeypress(target, options) { target.dispatchEvent(new KeyboardEvent('keydown', options)); } document.addEventListener("keydown", event => { console.log(event.key); }); setTimeout(() => { simulateKeypress(document, { key: "F1" }); }, 1000);

(As already stated on the linked original question, it is unclear what "simulate" means here . However:) (如已在链接的原始问题中所述, 此处“模拟”的含义尚不清楚。但是:)

We maintain a package for simulating user events for testing at @testing-library/user-event .我们在@testing-library/user-event维护了一个用于模拟用户事件以进行测试的包。

This includes userEvent.type() API for conveniently simulating keyboard input to input elements as well as userEvent.keyboard() API for simulating keyboard events anywhere.这包括userEvent.type() API为方便模拟键盘输入到输入元素以及userEvent.keyboard() API任何地方模拟键盘事件。 The library simulates the common browser behavior.该库模拟常见的浏览器行为。 It will dispatch the key events, change element values, move selection and focus - dependent on the pressed keys and possibly custom event handler that eg stop propagation.它将调度键事件、更改元素值、移动选择和焦点——取决于按下的键和可能的自定义事件处理程序,例如停止传播。

The primary target audience for the library is testing with jest and jsdom , but other environments including testing in real browsers are also supported.该库的主要目标受众是使用jestjsdom进行测试,但也支持其他环境,包括在真实浏览器中进行测试。

(For examples and further explanations see the linked docs. For missing features, bugs, etc.: Issues and PRs are always welcome at the linked Github repo :) ) (有关示例和进一步解释,请参阅链接的文档。对于缺少的功能、错误等:在链接的 Github 存储库中始终欢迎问题和 PR :))

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

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