Javascript/jQuery Keypress logging

前端 未结 7 1776
日久生厌
日久生厌 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:52

    This maybe?

    var seq = "rainbow"
    var input = ""
    window.addEventListener("keypress", function(e) {
        input += String.fromCharCode(e.keyCode)
    
        for (var i = 0; i < seq.length; i++) {
            if (input[i] != seq[i] && input[i] != undefined) {
                input = ""
            }
        }
    
        if (input == seq) {
            alert("EASTER EGG!")
    
            input = ""
        }
    })
    

提交回复
热议问题