JavaScript 30 – Day 12: Key Sequence Detection

Written by Thorsten Frommen

Thorsten is a certified PHP engineer, web development professional and tester. He has been working on the web since 2000, and with (and on) WordPress since 2005. Thorsten is a Senior WordPress Engineer at Human Made.

We then start with an empty array of equal size to keep track of the user input:

let userInput = new Array( keySequence.length );

Finally, we listen to the keydown event, and update the user input storage by shifting all items, and appending the current key. This is done by combining slicing, spreading, and declaring the user input array.