无法在键盘事件init方法中初始化键盘



我正在尝试模拟键盘事件(F1 -F12),然后将其派遣到iframe(in Text.xhtml)。但是,当我尝试初始化事件时,键代码不会更改并显示其默认值为零,因此,它不理解按键是F键之一。我想要的是将键盘事件的键代码设置为F1-F12键盘。

我也使用了该网站的键编码,但它也无法使用。在这里说密码属性集或检索Unicode。但是在这里说键代码属性仅读取。我真的很困惑和疲倦,因为我正在处理3天。请帮助或要求我进行任何澄清。

预先感谢

这是我的test.html代码:

<!DOCTYPE html>
<html>
<body>
 <script>
function EventHandler(e) {
    e = e || window.event;
    var code = typeof e.which !== 'undefined' ? e.which : e.keyCode; 
    if(code == 112 || code == 113 
        || code == 114 || code == 115
        || code == 116 || code == 117
        || code == 118 || code == 119
        || code == 120 || code == 121
        || code == 122 || code == 123) {
        e.returnValue = false;
        var target2 = document.getElementById("iFrameId"); 
        var targetBody = target2.contentDocument.body; 
        var keyboardEvent = document.createEvent("KeyboardEvent");
        var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
        keyboardEvent[initMethod](
                 "keydown", // event type : keydown, keyup, keypress
                 true, // bubbles
                 true, // cancelable
                 window, // viewArg: should be window
                     false, // ctrlKeyArg
                 false, // altKeyArg
                 false, // shiftKeyArg
                 false, // metaKeyArg
                 code, // keyCodeArg : unsigned long the virtual key code, else 0
                 0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
                );
        targetBody.dispatchEvent(keyboardEvent);        
        }
    }
</script>
    <form>
    <input type="text" name="first" onkeydown="EventHandler()" > <br>
    <iframe id="iFrameId" src="frame.html" > </iframe>
    </form>
    </body>
    </html>

这是可能的帧。html代码:

 <!DOCTYPE html>
 <html>
 <body onkeydown="FrameEventHandler(event)"  >
 <script>
function FrameEventHandler(e) {
    e = e || window.event; alert(e);
    var code = typeof e.which !== 'undefined' ? e.which : e.keyCode;
    if(code == 112 )
        alert("F1");
    else if (code == 113)
        alert("F2");
    else if (code == 114)
        alert("F3");
    else if (code == 115)
        alert("F4");
    else if (code == 116)
        alert("F5");
    else if (code == 117)
        alert("F6");
    else if (code == 118)
        alert("F7");
    else if (code == 119)
        alert("F8");
    else if (code == 120)
        alert("F9");
    else if (code == 121)
        alert("F10");
    else if (code == 122)
        alert("F11");
    else if (code == 123)
        alert("F12");
    }
  </script>     
   <form id="frameForm" >
    <p>This is Frame !!!<p> 
   </form>
  </body>
  </html>

好吧,我已经在stackoverflow上的讨论解决了这个问题,此处可用。我正在为处理同样问题的人写这个答案。现在我的代码就像:

<!DOCTYPE html>
<html>
<body>
  <script>
  function EventHandler(e) {
    e = e || window.event;
    var code = typeof e.which !== 'undefined' ? e.which : e.keyCode; 
    if(code == 112 || code == 113 
        || code == 114 || code == 115
        || code == 116 || code == 117
        || code == 118 || code == 119
        || code == 120 || code == 121
        || code == 122 || code == 123) {
      e.returnValue = false;
      var target2 = document.getElementById("iFrameId"); 
      var targetBody = target2.contentDocument.body; 
      var keyboardEvent = document.createEvent("KeyboardEvent");
      Object.defineProperty(keyboardEvent, 'keyCode', {
        get : function() {
          return this.keyCodeVal;
        }
      });
      Object.defineProperty(keyboardEvent, 'which', {
        get : function() {
          return this.keyCodeVal;
        }
      });
      var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
      keyboardEvent[initMethod](
         "keydown", // event type : keydown, keyup, keypress
         true, // bubbles
         true, // cancelable
         window, // viewArg: should be window
         false, // ctrlKeyArg
         false, // altKeyArg
         false, // shiftKeyArg
         false, // metaKeyArg
         code, // keyCodeArg : unsigned long the virtual key code, else 0
         0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
      );
      keyboardEvent.keyCodeVal = code;
      targetBody.dispatchEvent(keyboardEvent);        
    }
  }
  </script>
  <form>
    <input type="text" name="first" onkeydown="EventHandler()" > <br>
    <iframe id="iFrameId" src="frame.html" > </iframe>
  </form>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新