Intercepting all key presses with jQuery before they are processed

前端 未结 2 1716
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 14:01

I have a web page and I need to intercept all keypresses before they get dispatched to whatever element in the dom has the focus. When the user types something like ~

2条回答
  •  甜味超标
    2021-01-19 14:51

    You will probably have better luck with keydown it should fire before the event

    also do this

    $(document).keydown(function(e) {
        e.preventDefault();
    });
    

    calling preventDefault will stop normal process of the key press. Actually, you may be able to stay with keypress and just use the preventDefault to finish your interception needs.

提交回复
热议问题