Using the Keyboard for Map Movement
January 9, 2008 – 7:15 pmThis is just a little snippet of code I used to use on one of my games that had a map to explore. If you also have a map in your game, and if you don’t support keyboard movement, shame on you!
Playing with keyboard movement is much easier and funner than using a mouse, and also makes the game feel less like a simple text game.
Read more to view the code!
<script language="JavaScript1.2"> <!-- if (navigator.appName.indexOf('Netscape')!=-1) document.captureEvents(Event.KEYDOWN); document.onkeydown=function(e) { e=document.all?event.keyCode:e.which; switch (e) { case 37: e='left';break; case 38: e='up';break; case 39: e='right';break; case 40: e='down';break; case 104: e='up';break; case 100: e='left';break; case 102: e='right';break; case 98: e='down';break; default: e=String.fromCharCode(e);break; } switch (e.toLowerCase()) { case "up": window.location.href="process/map.php?cmd=n"; break; case "left": window.location.href="process/map.php?cmd=w"; break; case "right": window.location.href="process/map.php?cmd=e"; break; case "down": window.location.href="process/map.php?cmd=s"; break; }} //--> </script>
Just change the URLs that you want to redirect to, so that the different actions can be processed. If you are familiar with JavaScript and AJAX, you might be able to change this code so that the page doesn’t need to be completely refreshed!
If you do use this code, please leave a comment and show us where you used it! (Hint: Opportunity to show off your game ;))

2 Responses to “Using the Keyboard for Map Movement”
Weird, I just spent the whole morning working on the exact same thing and came up with an almost identical way of using the keyboard for navigational purposes (not a map, but simply page-navigation).
Btw, how would I post code-snippets?
By Sinzygy on Jan 10, 2008
You can try using this tag: <pre lang=”JAVASCRIPT”> for posting javascript, otherwise you could try the <code> tag.
Just change the JAVASCRIPT to PHP to use PHP syntax highlighting.
By Andy on Jan 10, 2008