javascript - Keep history/order of keys pressed -


i have array keyboard combinations , should validate press keys in order of array example

var listkeys = ['left', 'right', 'bottom', 'top'];  if(validate key){     continue } 

the order of array important if wrong pressing in order of arrows continued not more , sends error

i newbie in javascript hope can enormously grateful

typically, don't respond posts ask do-it-all solutions, wrote out , didn't want go waste. here little something started.

// valid combination of keys var valid_combo = ['left', 'right', 'bottom', 'top'];   // stack stores last 4 key inputs, including non-arrow keys var history = [];    // maps arrow key direction strings char code var keys = [];   keys[37] = 'left'; keys[38] = 'top'; keys[39] = 'right'; keys[40] = 'bottom';  $(window).keyup(function(e) {      var key = e.which;      if (key >= 37 && key <= 40)      {         history.push(keys[key]);     }     else     {         history.push("derp");     }      if (history.length > 4) history.shift();      // array equality using option 1 - http://stackoverflow.com/a/10316616/773702     if (json.stringify(valid_combo) == json.stringify(history))      {         console.log("valid key combination");     } }); 

see in action on jsfiddle


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -