Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:20:55+00:00 2026-06-13T01:20:55+00:00

I am trying to simulate keyboard event (for F1 – F12) and dispatch that

  • 0

I am trying to simulate keyboard event (for F1 – F12) and dispatch that to an iframe (in text.xhtml). But when I try to initialize the event, keyCode does not change and shows its default value which is zero and so, it does not understand the pressed key is one of the F keys. What I want is to set the keyboard event’s keyCode to F1-F12 keyCodes.

I have used this site’s keyCodes also, but it didn’t work too.
And here says keyCode Property sets or retrieves the Unicode. But here says keyCode Property is read only. I am really confused and tired, because I am dealing with it for 3 days. Please help or ask me for any clarification.

Thanks in advance

Here is my test.html code:

<!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>

and here is may frame.html code:

 <!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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:20:55+00:00Added an answer on June 13, 2026 at 1:20 am

    Ok I have solved the problem with help of a discussion on stackoverflow which is available here. I am writing this answer for those who deal with the same problem. Now my code is like:

    <!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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to simulate a HTTP POST that needs to combine some INPUT/TEXT fields along
I'm trying to simulate a realistic key press event. For that reason I'm using
The program I am trying to do is to simulate the mouse event of
I am trying to simulate a keyboard interface using buttons and labels... I understand
I'm trying to simulate a token ring using python with sockets, but I have
I'm trying to simulate text input into a JTextField . I've got a 1
I am trying to simulate keyboard shortcuts such as (Ctrl + A, Ctrl +
I'm trying to simulate a floating modal box type thing but having an issue
So I am trying to simulate that the phone is receiving a call. I
I'm trying to simulate an Android UI element that unfortunately doesn't exist in Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.