Labels

Tuesday, December 31, 2013

Javascript Event Keys

Ways to get the keycode (or charcode) of keys that were pressed.

onkeypress:
  • which (i.e. e.which)
onkeyup/onkeydown:
  •  keyCode (i.e. e.keyCode)

<script>
     function testkeydown(e)
     {
          alert("Key pressed was " + e.keyCode);
     }

     function testkeyup(e)
     {
          testkeydown(e);
     }

     function testkeypress(str, e)
     {
          alert("Key pressed was " + e.which + " and the additional parameter was " + str + ".");
     }
</script>

<input type="text" onkeydown="testkeydown(event);" />
<input type="text" onkeyup="testkeyup(event);" />
<input type="text" onkeypress="testkeypress("additional parameter", event);" />