简体   繁体   English

我怎样才能让 JavaScript 按下键盘上的键而不按它们? js

[英]How can I make JavaScript press keys on the keyboard without me pressing them? js

Here's my code这是我的代码

window.addEventListener('keypress', mod, false);
window.addEventListener('keyup', mod, false);

function mod(i) {
  
  var enter = 13;
  
  if(i.keyCode === 49) {
    //basically if key one is pressed then the enter key will be pressed without me touching the enter key
  }
}

SOO, basically is what I'm trying to say is that if 1 is pressed on the keyboard, the enter key will be pressed. SOO,基本上就是我想说的是,如果在键盘上按下 1,则将按下回车键。 Please help, I've been trying to find solutions on this for a very long time请帮忙,我一直在努力寻找解决方案很长时间

Take a look at this post about triggering keypress events programmatically.看看这篇关于以编程方式触发按键事件的帖子 Basically, you will be able to pass a keyboard event into document.dispatchEvent() inside your if statement and it will trigger the keypress.基本上,您将能够在 if 语句中将键盘事件传递给 document.dispatchEvent() ,它将触发按键。

暂无
暂无

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

相关问题 如何使用vanilla JavaScript在浏览器窗口中模拟键盘按下? - How can I emulate a keyboard press in a browser window with vanilla JavaScript? 我正在使用 jQuery 在禁用所有键盘键后如何启用它们? - I'm using jQuery How can I enable all keyboard keys when after i disable them? 如何使用Javascript(jquery,dojo)制作新闻事件? - How can i make press event with Javascript (jquery, dojo)? 如何让程序等待 javascript 中的按钮按下? - How can I make a program wait for a button press in javascript? 如何在按“向上”或“向下”箭头键时使Fancybox切换到下一个或上一个画廊? - How can I make Fancybox switch to the next or previous gallery upon pressing the 'up' or 'down' arrow keys? 我如何制作带有 0-9 数字的按钮,当我按下它们时添加到文本框中 - how can i make buttons with numbers from 0-9 that adds into a textbox when i press them 如何禁用所有键盘键? - How can I disable all keyboard keys? 如何在不显示API密钥的情况下使此代码正常工作? 我在Node.js / Angular / Express上构建为应用程序 - How can I make this code work without showing my API keys? I am building as app on Node.js/Angular/Express 我如何在不不匹配的情况下随机播放两个 js 数组 - How can i Shuffle two js arrays without mismatching them 我怎样才能在 JS 中制作一个 function 给我 [1, 2, 3, 4, 5, 6] = [4, 1, 2, 5, 3, 6] - how can I make a function in JS that gives me [1, 2, 3, 4, 5, 6] = [4, 1, 2, 5, 3, 6]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM