2011-10-12 2 views

답변

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); 

LOOK AT JSBIN LINK

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, W3CMDN에 표시하여이를 수행 할 실제 방법이 있는지 확인합니다.

+0

thx 아무 문제가없는 사람! ;) – sbaaaang

+3

이 코드는 모두 작동하지 않으므로? 점은 무엇인가. –

+0

@azizpunjani 죄송합니다. 무료 코드 소스가 이번엔 작동하지 않았습니다. 어쩌면 당신은 문서를 읽을 필요가 있습니다! – Mohsen

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'); 
     }   
}); 
+0

이 코드를 테스트 했습니까? 이것은 작동하지 않습니다 – Mohsen

+0

피들 http://jsfiddle.net/pZgB2/1/ –

+0

나는 당신의 편집을 보았습니다. 이제 좋아. 여전히 ctrl + shift + z는 Ctrl + z를 실행합니다. 수정되었습니다 : http://jsfiddle.net/pZgB2/2/ – Mohsen

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를 누른 상태에서 그 키를 누르면 되었기 때문에 해당 CtrlShift 키에 대한 두 가지 핵심이 볼 수 있듯이.

그래서 당신은이 같은 이벤트를 감지 할 수 있습니다 :

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

참고 : 여러 키 키보드 단축키에 대한 keydown을 청취해야한다. keyup가 작동하지 않습니다.