Jquery script for simulated key press down not running keyboard shortcut

前端 未结 2 1765
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 05:43

Id like to give thanks in advance for any time and effort spent on this. Ok so I have a script thats supposed to simulate a key press down event after 3 seconds once the pa

相关标签:
2条回答
  • 2020-12-07 06:19

    you don't add your handler first....do that

    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
    

    example to test: http://jsfiddle.net/x8a25/1/

    0 讨论(0)
  • 2020-12-07 06:32

    If you're trying to fire some browser or system wide keyboard shortcut then it's a dead end - it can't be done for security reasons. If it would be possible, you would have pages all over the Internet that would (for example) add themself to your bookmarks without even asking (by firing CTRL+B shortcut using Javascript).

    0 讨论(0)
提交回复
热议问题