Javascript/jQuery Keypress logging

前端 未结 7 1769
日久生厌
日久生厌 2020-12-30 14:34

I would like to be able to log the key presses on a specific page, trying to implement an \'Easter egg\' type functionality where when the correct keys are pressed in the co

7条回答
  •  一个人的身影
    2020-12-30 14:43

    Using jQuery:

    $(document).keyup( function(e) {
        if (e.keyCode == 27) {
            alert("You pressed escape");
        }
    });
    

    This captures key presses on the whole page not just a specific element.

    This blog post details how to capture CTRL+Key shortcuts.

提交回复
热议问题