SHARE
TWEET

Untitled

a guest Oct 13th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Konami Code Event Listener
  3.  * By: Daryl Pinto
  4.  *
  5.  * Example usage:
  6.  * ```
  7.  * import "konami-code-listener.js";
  8.  *
  9.  * document.addEventListener("konamicode", function() {
  10.  *  alert("Hello world!");
  11.  * });
  12.  * ```
  13.  */
  14.  
  15. export default (() => {
  16.     //Prevent IE from failing to load page
  17.     if(!window || typeof window.CustomEvent != "function") return;
  18.  
  19.     // Keys
  20.     const LEFT = 37;
  21.     const UP = 38;
  22.     const RIGHT = 39;
  23.     const DOWN = 40;
  24.     const B = 66;
  25.     const A = 65;
  26.     const START = 13;
  27.  
  28.     const valid_seq = [UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A, START].toString();
  29.  
  30.     // Event
  31.     let konamicode = new Event("konamicode");
  32.     let seq = [];  
  33.  
  34.     document.addEventListener("keydown", function(e){
  35.         seq.push(e.keyCode);   
  36.         if(!valid_seq.startsWith(seq.toString())) seq = [];
  37.         else if(valid_seq === seq.toString()) document.dispatchEvent(konamicode);
  38.     });
  39. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top