locked
Fire keyboard events from javascript RRS feed

  • Question

  • <table>
                                <tr>
                                    <td>
                                        <input type="button" title="A" value="left click" onclick="btn_Onclick('<');" /></td>
                                    <td>
                                        <input type="button" title="A" value="right click" onclick="btn_Onclick('>');" /></td>
                                    <td>
                                        <input type="button" title="A" value="x click" onclick="btn_Onclick('x');" /></td>
                                    </tr>
                            </table>


    Hi guys, I am trying to fire keyboardevents from javascript.  Why am I doing this?  I have a embedded iframe in a javascript app, that requires controlling via keyboard, if the user does not have a keybpoard, it needs to be support via onscreen keyboard.

    As I do not want to include all the buttons, I was hoping to do this myself.  Has anyone got any ideas?

     

    So far,

    <script type="text/javascript">
            function btn_Onclick(letter) {
                var keyboardEvent = document.createEvent("KeyboardEvent");
                var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
    
                var keyCode;
    
                switch (letter) {
                    case "<": keycode = 37; break;
                    case ">": keycode = 39; break;
                    case "x": keycode = 120; break;
                    default:
    
                }
    
                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
                                    keycode, // keyCodeArg : unsigned long the virtual key code, else 0
                                    keycode // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
                );
                document.getElementById('game').focus()
                document.dispatchEvent(keyboardEvent);
            }
        </script>
     


    <table>
                                <tr>
                                    <td>
                                        <input type="button" title="A" value="left click" onclick="btn_Onclick('<');" /></td>
                                    <td>
                                        <input type="button" title="A" value="right click" onclick="btn_Onclick('>');" /></td>
                                    <td>
                                        <input type="button" title="A" value="x click" onclick="btn_Onclick('x');" /></td>
                                    </tr>
                            </table>

    Thank you guys,

    kelvin


    Kind regards, Kelvin

    Wednesday, April 3, 2013 8:04 PM

All replies

  • Hi Kelvin,

    It looks like you have some code already.  I don't understand what you are asking.  What is not working and what troubleshooting have you done so far?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 4, 2013 6:50 PM
    Moderator
  • Thanks for the reply.  I think there may be security reason to explain why it is not working.

    I have created a number of buttons and onclick of the button it fires the above JS function.  The JS functions work, however I want to fire the keyboard event in the context of the iframe (i.e. iframe.contentWindow), sadly the keyboard event only appears to work rom its current location.

    kelvin


    Kind regards, Kelvin

    Friday, April 5, 2013 3:36 PM