Jquery script for simulated key press down not running keyboard shortcut

回眸只為那壹抹淺笑 提交于 2019-11-28 02:17:52

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).

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!