witch@&*weaves

30daysJS-12 - Key Sequence Detection (KONAMI CODE)

这个很简单,主要是利用数组方法和KeyboardEvent.key

yMJQmY.gif
const serctCode = 'star'
      const arr = []
      const main = document.querySelector('main')

      window.addEventListener('keydown',code)
      
      function code(e){
        const key = e.key
        console.log(key);
        arr.push(key)
        
        arr.splice(
          -serctCode.length -1,arr.length - serctCode.length -1
        )

        if(arr.join('').includes(serctCode)){
          alert('wow!')
          main.innerHTML = '<img src="../img/432968wwi0anufp3.gif">'
        }

      }

知识点

数组方法

KeyboardEvent.key

返回用户按下的物理按键的值。

#code