me preguntaba cómo desencadenar el evento keyCode compuesto por Ctrl+z
y el código clave del evento compuesto por Ctrl+Shift+z
?jQuery Trigger keyCode Ctrl + Shift + z & Ctrl + z en wysiwyg textarea
Respuesta
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);
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.
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');
}
});
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.
- 1. Cómo enviar ctrl + z
- 2. jquery trigger ctrl + clic
- 3. VMWare ctrl-z vinculante, cómo quitar
- 4. Anular MAYÚS + CTRL + Z en el Bloc de notas ++
- 5. Cómo ir hacia atrás (Ctrl + Z) en vi/vim
- 6. ¿Por qué falla Ctrl-Z en Windows Git bash shell?
- 7. Cómo capturar Ctrl + Tab y Ctrl + Shift + Tab en WPF?
- 8. Cómo usar Ctrl + Z y Ctrl + Y con todos los componentes de texto?
- 9. cómo deshabilitar Ctrl-Z, Ctrl-C de salir de un script php
- 10. Manejar eventos de teclas Ctrl + Tab y Ctrl + Shift + Tab
- 11. ¿Puede Emacs diferenciar entre ctrl-r y ctrl-shift-r?
- 12. Ctrl + Shift + Espacio equivalente en Qt Creator?
- 13. Inspección Eclipse (Ctrl + Shift + I) no funciona
- 14. ¿Cómo puedo poner un proceso en segundo plano después de su ejecución con CTRL + Z roto?
- 15. cómo enviar CTRL + Z keyEvent en java usando la clase de robot
- 16. de Eclipse Visual Studio Ctrl + Tab y Ctrl + Shift + Tab Equivalente
- 17. Cómo asignar Ctrl + A y Ctrl + Shift + A de manera diferente?
- 18. Ctrl + Shift + R no está trabajando en Eclipse
- 19. El botón Ctrl no funciona en Eclipse
- 20. jQuery y CSS - Z-index
- 21. jQuery mango redimensionable z-index
- 22. Obtener estados shift/ctrl/alt de un evento de mouse?
- 23. Eclipse: Cambiar el bloque de comentario estilo de Ctrl + Shift +/
- 24. Parece que no se puede deshacer de la combinación de teclas Ctrl-x Ctrl-z en Emacs para minimizar la ventana ('suspend-frame)
- 25. vim: mapeo ctrl-0 a ctrl-9?
- 26. contentEditable, CTRL-B CTRL-I y guardado
- 27. teclas Ctrl Vim-Izquierda/Derecha Ctrl-comportamiento
- 28. Visual Extendiendo Ctrl + K estudio, Ctrl + D
- 29. Ctrl + Enter jQuery en el área de texto
- 30. Deshabilitar ctrl-clic en jquery ui seleccionable
para una mejor explicación, me gustaría saber cómo crear los clásicos botones Deshacer y Rehacer para un texto en wysiwyg ... la mejor práctica – sbaaaang