Ctrl+z
로 구성된 이벤트 keyCode와 Ctrl+Shift+z
으로 구성된 이벤트 키 코드를 어떻게 트리거합니까?jQuery 트리거 키 코드 wysiwyg 텍스트 영역의 Ctrl + Shift + z 및 Ctrl + z
5
A
답변
5
그런 다음 이벤트를 트리거 할 경우는 다음과 같이해야한다 :
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);
01
<!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>
자바 스크립트
HTML
하지만 작동하지 않는 것 같습니다. 나는 이것에 쓸 시간이 없지만, 이것은 보안 문제의 일종입니다. 나는이 문서를 MSDN, W3C 및 MDN에 표시하여이를 수행 할 실제 방법이 있는지 확인합니다.
9
jquery로 표준화 된 크로스 브라우저 인 e.which
을 사용하십시오.
$(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');
}
});
3
Ctrl 및 Shift 키는 키 이벤트에 포함되어 있지만 키 코드는 어떤 키를 누르는지를 판단합니다. Ctrl과 Shift는 컨트롤 키이고 키 이벤트에는 고유 한 키가 있습니다. 당신이 Ctrl+Shift+Z
다음를 keyDown 이벤트를 누르면 예를 들어
이 될 것입니다 :
{
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
}
당신이 Z
를 누른 상태에서 그 키를 누르면 되었기 때문에 해당 Ctrl
및 Shift
키에 대한 두 가지 핵심이 볼 수 있듯이.
그래서 당신은이 같은 이벤트를 감지 할 수 있습니다 :
document.addEventListener('keydown', function(event){
if(event.keyCode == 90 && event.ctrlKey && event.shiftKey){
// do your stuff
}
}, false);
참고 : 여러 키 키보드 단축키에 대한 keydown
을 청취해야한다. keyup
가 작동하지 않습니다.
더 나은 설명을 위해 wysiwyg textarea에 대한 고전적인 실행 취소 및 다시 실행 단추를 만드는 방법을 알고 싶습니다. 가장 좋은 방법은 – sbaaaang