gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:16:39 25 4
gpt4 key购买 nike

我想提前感谢为此花费的任何时间和精力。好的,所以我有一个脚本,它应该在页面加载后 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>

最佳答案

如果您试图触发某些浏览器或系统范围的键盘快捷键,那么这是一个死胡同 - 出于安全原因不能这样做。如果可能的话,您将拥有遍布 Internet 的页面,这些页面(例如)甚至无需询问(通过使用 Javascript 触发 CTRL+B 快捷方式)就将自己添加到您的书签中。

关于javascript - 用于模拟按键的 Jquery 脚本按下不运行键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com