Feel very fluent keyboard control (with inertia)
<body> <div id= "man" ></div> <button onclick= "alert (keycache.length)" >ask</button> </body> <script> var keycache = []; var iscache = false; var cachenum = 0; Document.onkeydown = Insertkey; Document.onkeyup = function () {if (!iscache && event.keycode>=37&& event.keycode<=40) keycache.length = 0;} function Insertkey () {var kc = Event.keycode; if (kc>=37 && kc<=40) {if (kc!=keycache[keycache.length-1]) {keycache.length = 0; Keycache.push (KC,KC,KC,KC,KC,KC)//Ensure fluent multiple injection of 6 Iscache = true; Keycache.push (KC); } window.setinterval ("Writecache ()", 50); function Writecache () {if (keycache.length==0) return; var kc = Keycache.shift (); Switch (KC) {Case 37:man.style.left = parseint (man.style.left)-5;break; Case 38:man.style.top = parseint (man.style.top)-5;break; Case 39:MAN.STyle.left = parseint (man.style.left) + 5;break; Case 40:man.style.top = parseint (man.style.top) + 5;break; } if (Iscache) cachenum++; if (Cachenum = = 5) {Iscache = false; Cachenum = 0; }} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]