用于模拟按键的Jquery脚本按下不运行键盘快捷键

use*_*167 1 javascript jquery keyboard-shortcuts keypress simulate

我想提前感谢您花费的时间和精力.好的,所以我有一个脚本,应该在页面加载后3秒后模拟按键事件.我打算让这个按键运行键盘快捷键,但只需按键即可.一旦按下,我怎么能让它实际运行快捷方式?不确定这是否可行.再次谢谢.

<script>
 setTimeout( function(){
// jQuery plugin. Called on a jQuery object, not directly.
jQuery.fn.simulateKeyPress = function(character) {
  // Internally calls jQuery.event.trigger
  // with arguments (Event, data, elem). That last arguments is very important!
  jQuery(this).trigger({ type: 'keypress', which: character.charCodeAt(0) });
};

jQuery(document).ready( function($) {
  // Bind event handler
  $( 'body' ).keypress( function(e) {
    alert( String.fromCharCode( e.which ) );
    console.log(e);
  });
  // Simulate the key press
  $( 'body' ).simulateKeyPress('z');
});
 }, 3000); //3 seconds

</script>

<script type="text/javascript">
// define a handler
function doc_keyUp(e) {

    // this would test for whichever key is 40 and the ctrl key at the same time
    if (e.ctrlKey && e.keyCode == 122) {
        // call your function to do the thing
        pauseSound();
    }
}
// register the handler 
document.addEventListener('keyup', doc_keyUp, false);
</script>
Run Code Online (Sandbox Code Playgroud)

WTK*_*WTK 7

如果你试图触发一些浏览器或系统范围的键盘快捷方式,那么这是一个死胡同 - 出于安全原因无法完成.如果有可能,你会在互联网上有一些页面(例如)将它们自己添加到你的书签中,甚至不用询问(通过使用Javascript触发CTRL + B快捷键).