2011-10-12 41 views

Respuesta

5

Si desea activar el evento, entonces debería ser algo como esto:

HTML

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <input type=button value=CTRL+SHIFT+Z id=bcsz /> 
    <input type=button value=CTRL+Z id=bcz /> 
    <textarea id=t ></textarea> 
</body> 
</html> 

JavaScript

var t = document.getElementById('t'), //textarea 
    bcsz = document.getElementById('bcsz'), //button ctrl shift z 
    bsz = document.getElementById('bcz'), // button ctrl z 
    csz = document.createEvent('KeyboardEvents'), //ctrl shift z event 
    cz = document.createEvent('KeyboardEvents'); // ctrl z event 

csz.initKeyboardEvent(
      'keydown', 
      true,  // key down events bubble 
      true,  // and they can be cancelled 
      document.defaultView, // Use the default view 
      true,  // ctrl 
      false,  // alt 
      true,  //shift 
      false,  //meta key 
      90,   // keycode 
      0 
     ); 
cz.initKeyboardEvent(
      'keydown', 
      true,  // key down events bubble 
      true,  // and they can be cancelled 
      document.defaultView, // Use the default view 
      true,  // ctrl 
      false,  // alt 
      false,  //shift 
      false,  //meta key 
      90,   // keycode 
      0 
     ); 

bcz.addEventListener('click', function(){ 
    t.dispatchEvent(cz); 
}, false); 

bcsz.addEventListener('click', function(){ 
    t.dispatchEvent(csz); 
}, false); 

LOOK AT JSBIN LINK

Pero parece que no funciona. No tengo más tiempo para gastar en esto, pero sí es un problema de seguridad. Vería estos documentos en MSDN, W3C y MDN para ver si hay una manera real de hacerlo.

+0

thx no hay problema hombre! ;) – sbaaaang

+3

así que todo este código y no funciona? Cuál es el punto de. –

+0

@azizpunjani oh, lo siento, su código fuente no funcionó esta vez. ¡Quizás necesites leer documentaciones! – Mohsen

9

Use e.which que se ha normalizado cross browser por jquery.

$(document).keydown(function(e){ 
     if(e.which === 90 && e.ctrlKey && e.shiftKey){ 
     console.log('control + shift + z'); 
     } 
     else if(e.which === 90 && e.ctrlKey){ 
     console.log('control + z'); 
     }   
}); 
+0

¿Has probado este código? Esto no funciona – Mohsen

+0

Fiddle http://jsfiddle.net/pZgB2/1/ –

+0

Vi tu edición. Es bueno ahora. Aún ctrl + shift + z dispara ctrl + z. Esta es la solución: http://jsfiddle.net/pZgB2/2/ – Mohsen

3

Las teclas Ctrl y Shift están incluidas en los eventos clave, pero el código clave es el arbitraje de la tecla presionada. Ctrl y Shift son teclas de control y tienen sus propias teclas en eventos clave.

Por ejemplo, si se pulsa Ctrl+Shift+Z evento luego keydown sería la siguiente:

{ 
    altGraphKey: false 
    altKey: false 
    bubbles: true 
    cancelBubble: false 
    cancelable: true 
    charCode: 0 
    clipboardData: undefined 
    ctrlKey: true 
    currentTarget: null 
    defaultPrevented: true 
    detail: 0 
    eventPhase: 0 
    keyCode: 90 
    keyIdentifier: "U+004C" 
    keyLocation: 0 
    layerX: 0 
    layerY: 0 
    metaKey: false 
    pageX: 0 
    pageY: 0 
    returnValue: false 
    shiftKey: true 
    srcElement: HTMLTextAreaElement 
    target: HTMLTextAreaElement 
    timeStamp: 1318460678544 
    type: "keydown" 
    view: DOMWindow 
    which: 90 
    __proto__: KeyboardEvent 
} 

Como se puede ver, hay dos claves para Ctrl y Shift claves que son verdaderas porque esas teclas fueron presionadas mientras presiona Z.

para que pueda detectar este evento como este:

document.addEventListener('keydown', function(event){ 
    if(event.keyCode == 90 && event.ctrlKey && event.shiftKey){ 
    // do your stuff 
    } 
}, false); 

Nota: Usted debe escuchar a keydown para múltiples combinaciones de teclas del teclado. keyup no funcionaría.

Cuestiones relacionadas