Online Shopping : 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?

114 of 148 people (77%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I capture Alt-key and Ctrl-key combinations?
But how to get 'event.ctrlKey' in Mozilla if I need to pass more parameters like onclickhandl(some i

Apr 16th, 2000 08:18
Martin Honnen, Wim Schuiling, Vladimir Kelman,


IE has the following boolean properties for the window.event object
  event.altKey  
  event.ctrlKey
  event.shiftKey
NN6 also has this properties but for the event object passed to the 
event handler.
NN4 has some
  event.modifiers
bit flag which you can bitwise and with predefined values
  Event.CONTROL_MASK
  Event.ALT_MASK
  Event.SHIFT_MASK
Note that while these flags exists NN4 doesn't seem to set the modifier 
for the alt key at all and only restricted for the ctrl key.
Here is a code example, see for yourself which combinations are caught 
and which not:
<HTML>
<HEAD>
<SCRIPT>
document.onkeypress = function (evt) {
  var r = '';
  if (document.all) {
    r += event.ctrlKey ? 'Ctrl-' : '';
    r += event.altKey ? 'Alt-' : '';
    r += event.shiftKey ? 'Shift-' : '';
    r += event.keyCode;
  }
  else if (document.getElementById) {
    r += evt.ctrlKey ? 'Ctrl-' : '';
    r += evt.altKey ? 'Alt-' : '';
    r += evt.shiftKey ? 'Shift-' : '';
    r += evt.charCode;
  }
  else if (document.layers) {
    r += evt.modifiers & Event.CONTROL_MASK ? 'Ctrl-' : '';
    r += evt.modifiers & Event.ALT_MASK ? 'Alt-' : '';
    r += evt.modifiers & Event.SHIFT_MASK ? 'Shift-' : '';
    r += evt.which;
  }
  alert(r);
  return true;
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>