我想提前感谢你在这件事上花费的时间和精力。好的,所以我有一个脚本,它应该模拟一个按键按下事件后,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>
发布于 2012-12-11 21:19:27
如果你试图触发某个浏览器或系统范围的键盘快捷键,那么这是一条死胡同--出于安全原因,这是不可能的。如果可能的话,你会在互联网上到处都有页面,这些页面甚至不需要询问就会自动添加到你的书签中(通过使用Javascript触发CTRL+B快捷方式)。
发布于 2012-12-11 21:19:20
您不会将处理程序first....do添加到
jQuery(document).ready(function($) {
// Bind event handler
$('body').keypress(function(e) {
alert(String.fromCharCode(e.which));
});
});
jQuery.fn.simulateKeyPress = function(character) {
jQuery(this).trigger({
type: 'keypress',
which: character.charCodeAt(0)
});
};
setTimeout(function() {
$('body').simulateKeyPress('z');
}, 3000); //3 seconds
要测试的示例:http://jsfiddle.net/x8a25/1/
https://stackoverflow.com/questions/13821071
复制相似问题