June 22, 201214 yr Hey, I'm looking for books and maby up to date documentaries about keyboard detection. So I don't need to have an input. I'm not sure if Javascript can handle it. I have already something on keyCode and the switch statement but they arn't working anymore. So any recommendations? Maby some kind of different language I must learn for it?
June 22, 201214 yr JS keyCode should work fine for detecting keyboard actions. It's more likely there's a problem with your code. Post your script and explain what you're trying to do with it.
June 22, 201214 yr Author #bar1 { background-image:url('bar.png'); width:221px; height:35px; } </style> </head> <body> <script type="text/javascript"> document.getElementById("bar1").onkeydown=function(e) e=e||event; { switch(e.keyCode) { case 37: // left this.style.left = parseInt(this.style.left)-this.offsetWidth+'px'; return false; case 38: // up this.style.top = parseInt(this.style.top)-this.offsetHeight+'px'; return false; case 39: // right this.style.left = parseInt(this.style.left)+this.offsetWidth+'px'; return false; case 40: // down this.style.top = parseInt(this.style.top)+this.offsetHeight+'px'; return false ; } } </script> <div id="bar1"></div> This is the script I copied and done it for mine bar code but it seems it doesn't work.
June 23, 201214 yr <div id="bar1" style="top:0px; left:0px;"></div> <script type="text/javascript"> document.body.onkeydown=function(e) { var bar = document.getElementById("bar1"), top = parseInt(bar.style.top), left = parseInt(bar.style.left), height = bar.offsetHeight, width = bar.offsetWidth; e = e || event; switch(e.keyCode) { case 37: // left bar.style.left = left - width + 'px'; break; case 38: // up bar.style.top = top - height + 'px'; break; case 39: // right bar.style.left = left + width + 'px'; break; case 40: // down bar.style.top = top + height +'px'; break; } } </script>
Create an account or sign in to comment