JSFiddle - React, Tailwind, and code Playground

HTML

<body></body>

JavaScript

// Stack that stores the last 4 key inputs, including non-arrow keys
var history = [];  

// Maps arrow key direction strings to char code
var keys = []; 

keys[37] = 'left';
keys[38] = 'top';
keys[39] = 'right';
keys[40] = 'bottom';

// Valid combination of keys
var valid_combo = ['left', 'right', 'bottom', 'top']; 

$(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();
    
    if (JSON.stringify(valid_combo) == JSON.stringify(history)) 
    {
        console.log("Valid key combination");
    }
});