PHP前端开发

使用 JavaScript 模拟键盘输入

百变鹏仔 3天前 #JavaScript
文章标签 键盘输入

在 javascript 中模拟键盘输入对于自动化任务或测试应用程序非常有用。 keyboardevent api 允许开发者以编程方式触发键盘事件。

示例

const event = new keyboardevent('keydown', {  key: 'enter',  ctrlkey: true,  bubbles: true,});document.dispatchevent(event);
const event = new KeyboardEvent('keydown', {  key: 'Enter',  shiftKey: true,  bubbles: true,});document.querySelector('input').dispatchEvent(event);