Trigger a keyboard shortcut through mouse click event using jQuery

一个人想着一个人 提交于 2019-12-25 02:27:44

问题


$(document).ready(function(){

    $(document).on( 'keypress', 'body', function( e ){
        var code = e.keyCode || e.which;

        if( code = 110 ) {
            console.log( 'Successfully triggered ALT+N' );
        }
    });

    $(document).on( 'click', '#copy', function(  ){
        var e = $.Event('keypress');
        e.which = 110; // Character 'A'
        console.log( e );
        $('body').trigger(e);
    });

});

The above piece of code successfully triggers the keyboard shortcut Alt+N on my document.

Alt+N keyboard shortcut does a particular action on my web page such as copying content of all form fields through Firefox plugin.

When #copy button is clicked through mouse, the keyboard action triggered through jQuery but the corresponding action is not taking place.

But when i press Alt+N from keyboard directly the action occurs.

Is it possible to trigger a keyboard shortcut through mouse click event?

来源:https://stackoverflow.com/questions/22650342/trigger-a-keyboard-shortcut-through-mouse-click-event-using-jquery

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