简体   繁体   English

如何通过单击 Javascript 的按钮来模拟按键?

[英]How to simulate keypress with a button click on Javascript?

I am currently working on a calculator and wondered if there is a way to link keypresses to buttons so when I click a button, it simuliates the key being pressed and sends that info to the textbox.我目前正在研究一个计算器,想知道是否有办法将按键链接到按钮,所以当我单击一个按钮时,它会模拟按下的键并将该信息发送到文本框。

When I google, all the info I get is enabling buttons by keypresses but not the other way around.当我用谷歌搜索时,我得到的所有信息都是通过按键启用按钮,而不是相反。

If someone could help me cause my code is already a mess with all the conversations and keypress events.如果有人可以帮助我,因为我的代码已经被所有的对话和按键事件弄得一团糟。

I believe this is answered in this Stackoverflow thread: Is it possible to simulate key press events programmatically?我相信这个 Stackoverflow 线程已经回答了这个问题: 是否可以通过编程方式模拟按键事件? . .

To add it to the textbox you can listen to the keydown event and add the character accordingly.要将其添加到文本框中,您可以监听 keydown 事件并相应地添加字符。

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

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