12:23:04 AM
Thursday
Sep 09
Su M Tu W Th F Sa
567891011
12131415161718
19202122232425
262728293012
3456789

Play My Typing Game

View Some of My Artwork

Determining Keypresses
There are three javascript event handlers to consider when dealing with keyboard input. onkeypress, onkeydown, onkeyup.

<script type="text/javascript">
document.onkeypress = listener;
function listener(e)
{
 var evtobj=window.event ? event : e;
 var unicode=evtobj.charCode ? evtobj.charCode : evtobj.keyCode;
 var actualkey=String.fromCharCode(unicode);
 alert('Unicode: '+unicode+'\nActual Key: '+actualkey);
}
</script>


This knowledge should get you started towards your own experimentation/implementation of what to do with keypresses. With game development in mind, I needed to know if multiple keys were being pressed simultaneously. Like up and right pressed together should be calmly recognized and direct you diagonally. Here's an example of dealing with multiple keypresses, only listening for 4 specific keys.

<script type="text/javascript">
var key_up = 0;
var key_down = 0;
var key_left = 0;
var key_right = 0;

// listen for keyboard input
document.onkeydown = keydown_listener;
document.onkeyup = keyup_listener;
var t = setInterval("timed_listener()", 50);

// key was pressed
function keydown_listener(e)
{
 var evtobj=window.event ? event : e;
 var unicode=evtobj.charCode ? evtobj.charCode : evtobj.keyCode;
 var actualkey=String.fromCharCode(unicode);
 if(unicode==38){ key_up = 1; }
 if(unicode==40){ key_down = 1; }
 if(unicode==37){ key_left = 1; }
 if(unicode==39){ key_right = 1; }
}

// key was depressed
function keyup_listener(e)
{
 var evtobj=window.event ? event : e;
 var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
 var actualkey=String.fromCharCode(unicode);
 if(unicode==38){ key_up = 0; }
 if(unicode==40){ key_down = 0; }
 if(unicode==37){ key_left = 0; }
 if(unicode==39){ key_right = 0; }
}

function timed_listener()
{
 if(key_up==1){ /* move sprite up */ }
 if(key_down==1){ /*move sprite down */ }
 if(key_right==1){ /*move sprite right */ }
 if(key_left==1){ /*move sprite left */ }
}

</script>


Read Previous:
A function to read the value of a radio input
Read Next:
determining page scroll
Table of Contents


Give a listen to my music...
My Best Recordings
How my services and skills can help you:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local
    • Most all the above, but also:
      • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
      • Recording Live Music Audio
      • Video Streaming live music and other live events anywhere that can provide wifi
      • Training Video Streaming to your venue's trusted soundperson
Skills:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local, Most all the above, but also:
    • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
    • Being your venue's soundman
    • Video Streaming live music and other live events anywhere that can provide wifi
    • Training Video Streaming to your venue's trusted soundperson