In this lesson I learned how to listen for a specific combination of inputs before firing a function.
First I initialise an empty array 'pressed[]', as well as a 'secretCode' variable.
I listen for a 'keyup' event on the 'window' object. I pass the event into a function ((e) => {}).
In the function(e) - Push the key event 'e.key' into the 'pressed[]' array. Then trim the array to ensure it is never longer than the maximum number of characters in 'secretCode'. Next join the 'pressed' array together into one string (.join('')) and check if it includes 'secretCode' (.includes(secretCode)), if so, the 'secretCode' is detected and the function can proceed.
See the Pen
JavaScript30 - 12 - Key Sequence Detection by Corey Noble (@CoreyNoble)
on CodePen.