Javascript/jQuery Keypress logging

前端 未结 7 1704
日久生厌
日久生厌 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 15:02

    here's an easy way to require the ctrl key to be held down while the user enters a sequence of characters. a bit easier than some of the responses. in the case below pressing and holding ctrl key while typing "quack" (no quotes) will trigger the alert. hth someone, someday.

         var easterEgg = [81, 85, 65, 67, 75]; //quack
         var eggLength = easterEgg.length;
         var currentPosition = 0;
         $(document).keydown(function (e) {
             if (e.ctrlKey && e.keyCode == easterEgg[currentPosition]) {
                 e.preventDefault();
                 if (++currentPosition == eggLength) {
                     currentPosition = 0;
                     alert('oh yeah, right there');
                 }
             } else {
                 currentPosition = 0;
             }
         });
    
    0 讨论(0)
提交回复
热议问题