我可以在 javascript 或 jquery 中模拟按键(CTRL+F1)吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21982351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:08:52  来源:igfitidea点击:

Can I simulate keypress(CTRL+F1) in javascript or jquery?

javascriptjqueryhtml

提问by Victtor888

Hi want to simulate the press of the keys CTRL+F1 when i do click on a button. For example:

嗨,当我点击一个按钮时,我想模拟按下 CTRL+F1 键。例如:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js">
        function simulateKeyPress(){

        }
    </script>

    <button class="boton3d" onclick="simulateKeyPress()"> 
           <img src="img/phone.png">       
    </button> 

Thanks...

谢谢...

回答by Milind Anantwar

You can try:

你可以试试:

  var e = jQuery.Event("keydown");
  e.which = 112;       // # F1 code value
  e.ctrlkey = true;     // control key pressed
  $(document).trigger(e);// trigger event on document

回答by Francois Borgies

you can use .keypress method : doc

您可以使用 .keypress 方法:doc

Sample :

样本 :

$("#MyDiv").keypress();

Will trigger a keypress on #MyDiv. If you'd like to also select which key was pressed, you can use .trigger : doc

将触发#MyDiv 上的按键。如果您还想选择按下了哪个键,可以使用 .trigger : doc

var e = $.Event("keydown", { keyCode: 112}); 
$("body").trigger(e);

you can find all the key here : http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

你可以在这里找到所有的钥匙:http: //www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

hope this help you.

希望这对你有帮助。