faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

138 of 216 people (64%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Can I capture the arrow keys with client side JavaScript?
Can I capture the arrow keys with client side JavaScript?
Can I capture the arrow keys with client side JavaScript?

Mar 16th, 2000 11:31
Martin Honnen,


IE4/5 allow that in onkeydown, NN6 allows that too while NN4 is not 
supporting it. Here is example code capturing keys in a textarea and 
identifying the arrow keys:

<HTML>
<HEAD>
<SCRIPT>
function checkArrows (field, evt) {
  var keyCode = 
    document.layers ? evt.which :
    document.all ? event.keyCode :
    document.getElementById ? evt.keyCode : 0;
  var r = '';
  if (keyCode == 39)
    r += 'arrow right';
  else if (keyCode == 40)
    r += 'arrow down';
  else if (keyCode == 38)
    r += 'arrow up';
  else if (keyCode == 37)
    r += 'arrow left';
  r += ' ' + keyCode;
  window.status = r;
  return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<TEXTAREA NAME="aTextArea" ROWS="5" COLS="20" WRAP="soft"
          ONKEYDOWN="return checkArrows(this, event)"
>
Kibology for all.
Kibology for all.
</TEXTAREA>
</FORM>
</BODY>
</HTML>