Forums

This topic is locked

link on keydown

Posted 14 Apr 2011 15:11:46
1
has voted
14 Apr 2011 15:11:46 P R posted:
hi,

does everyone knows how to set up the window event that i am able to set up a shortcut (combination)?

like ALT + g or enything else


<script type="text/javascript">
function keyHandler( e )
 {
 
if( !e )
 {
 e = window.event;
 }
 
arrowUp = 70;
 arrowDown = 86;
 arrowLeft = 68;
 arrowRight = 39;
 
switch( e.keyCode )
 {
 case arrowUp:
document.location.href = "xxx.asp";
 
 break;
 case arrowDown:
document.location.href = "xxx.asp";
 break;
 case arrowLeft:
document.location.href = "xxx.asp";
 break;
 case arrowRight:
document.location.href = "xxx.asp";
 break;
 }
 }
 
document.onkeydown = keyHandler;
 
 
 </script>

Replies

Replied 18 Apr 2011 11:23:16
18 Apr 2011 11:23:16 Patrick Woldberg replied:
in the event object there are properties for the alt, ctrl and shift key.

if (e.altKey && e.keyKode == 71) {
  // ALT+g was pressed, do your code
}
Replied 18 Apr 2011 14:15:49
18 Apr 2011 14:15:49 P R replied:
Quotein the event object there are properties for the alt, ctrl and shift key.

if (e.altKey && e.keyKode == 71) {
  // ALT+g was pressed, do your code
}


thank you for help, but this works not in my case if i replace this
if(!e)

at line 5.

is it possible to use the alt or control also as variable key like the "e"?

regards
Replied 18 Apr 2011 17:38:21
18 Apr 2011 17:38:21 Patrick Woldberg replied:
Quote
Quotein the event object there are properties for the alt, ctrl and shift key.

if (e.altKey && e.keyKode == 71) {
  // ALT+g was pressed, do your code
}


thank you for help, but this works not in my case if i replace this
if(!e)

at line 5.

is it possible to use the alt or control also as variable key like the "e"?

regards


The e is the event object, the first if statement is needed for IE and should not be removed.

function keyHandler( e ) {  
  e = window.event || e; // for IE

  if (e.altKey && e.keyCode == 71) {
    // ALT-g
  }

  if (e.ctrlKey && e.keyCode == 71) {
    // CTRL-g
  }

  if (e.shiftKey && e.keyCode == 71) {
    // SHIFT-g
  }
}

document.onkeydown = keyHandler;  
Replied 18 Apr 2011 19:11:52
18 Apr 2011 19:11:52 P R replied:
thank you very much!

reagrds
This reply was removed on 8/24/2012 9:47:44 AM.
See the changelog

Reply to this topic