Discussions » Development

request Alt key toggle script

§
Posted: 26.10.2016

request Alt key toggle script

Hi,
I want when I press Alt Key toggle
Please help me I'm a noob

if alt key press
// do stuff

§
Posted: 28.10.2016

firstly, you can use event.altKey property of mouse/keyboard event object:

onClick(event){
  if(event.altKey) { 
   /* do stuff */
  }
}
element.addEventListener("click",onClick,false);

furthermore, you can write your own code that tracks 'alt' keydown/keyup events:

var altPressed;
function onKeydown(e) {
 var k= e.keyCode; 
 if( k==18 ) { 
 // Don't run in input, select, textarea etc.
   var E = document.activeElement.tagName.toLowerCase();
   if (E == "input" || E == "select" || E == "textarea" ||
     (E=document.activeElement.contentEditable) == "true" ||
      E == "") 
   {  altPressed = 0; return; }
   altPressed = 1;   return;
 }
 if(k==0x38 && altPressed) { // Alt-'8' pressed
  e.preventDefault(),e.stopPropagation();
  alert('Alt-8 pressed'); // do stuff
 }
}
function onKeyup(e) { 
 var k= e.keyCode; 
 if( k==18 )
 { altPressed=0; return; }
}
addEventListener("keydown",onKeydown,false);
addEventListener("keyup",onKeyup,false);

Post reply

Sign in to post a reply.