»
EnglishFrenchVietnamese

Print - Detecting The Shift/Ctrl/Alt Keys - JavaScriptBank.com

Full version: jsB@nk » Utility » Detecting The Shift/Ctrl/Alt Keys
URL: http://www.javascriptbank.com/arrowkeys-detecting-the-shiftctrlalt-keys.html

Detecting The Shift/Ctrl/Alt Keys © JavaScriptBank.comDemonstrates a technique for detecting the shift, ctrl and alt keys: + The keydown listener sets shiftMode to true when event.keyCode == 16. + The keyup listener sets shiftMode to false when event.keyCode == 16. + The keypress listener now knows if the shift key is currently pressed.

Full version: jsB@nk » Utility » Detecting The Shift/Ctrl/Alt Keys
URL: http://www.javascriptbank.com/arrowkeys-detecting-the-shiftctrlalt-keys.html



JavaScript
<script type="text/javascript" src="x_core.js"></script><script type="text/javascript" src="x_event.js"></script><script type="text/javascript">var shiftMode = false; // if true the shift key is downvar ctrlMode = false; // if true the ctrl key is downvar altMode = false; // if true the alt key is down function winOnLoad(){  log();  xAddEventListener(document, 'keydown', docOnKeydown, false);  xAddEventListener(document, 'keyup', docOnKeyup, false);  xAddEventListener(document, 'keypress', docOnKeypress, false);}function docOnKeydown(ev){  var e = new xEvent(ev);  switch (e.keyCode) {    case 16:      shiftMode = true;      log('shift down');      break;    case 17:      ctrlMode = true;      log('ctrl down');      break;    case 18:      altMode = true; // not supported by Opera and I agree      log('alt down');      break;  }}function docOnKeyup(ev){  var e = new xEvent(ev);  switch (e.keyCode) {    case 16:      shiftMode = false;      log('shift up');      break;    case 17:      ctrlMode = false;      log('ctrl up');      break;    case 18:      altMode = false; // not supported by Opera and I agree      log('alt up');      break;  }}function docOnKeypress(ev){  var e = new xEvent(ev);  if (e.keyCode == 13) {    if (shiftMode) log('shift+enter');    if (ctrlMode) log('ctrl+enter');    if (altMode) log('alt+enter');  }}function log(s){  var ta = xGetElementById('log');  if (s) ta.value += s + '\n';  else ta.value = '';}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form onsubmit="return false"><textarea id="log" rows="20" cols="20"></textarea><br><input type="reset" value="Clear Textarea"></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


Files
http://javascriptbank.com/javascript/utility/x_core.jshttp://javascriptbank.com/javascript/utility/x_event.js