0

我正在尝试模拟键盘事件(对于 F1 - F12)并将其发送到 iframe(在 text.xhtml 中)。但是当我尝试初始化事件时,keyCode 不会改变并显示其默认值为零,因此它不明白按下的键是 F 键之一。我想要的是将键盘事件的 keyCode 设置为 F1-F12 keyCodes。

我也使用过这个网站的keyCodes,但也没有用。这里说keyCode 属性设置或检索 Unicode。但是这里说keyCode 属性是只读的。我真的很困惑和累,因为我要处理 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>

这是可能的 frame.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>
4

1 回答 1

0

好的,我已经通过关于 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>
于 2012-10-18T10:36:57.537 回答