locked
Unable to dispatch Keyboard events in Windows Store App (WinJS)

    Question

  • On my Window Store App, I am trying to fire a NumPad 7 event on a inputBox using the following code:

    var e = document.createEvent("KeyboardEvent");
    e.initKeyboardEvent("keydown", true, true, window, "7", KeyboardEvent.DOM_KEY_LOCATION_NUMPAD, "NumLock", false, "en-US");
    document.getElementById("myInputText").dispatchEvent(e);

    however this is not working. I event tried to run this code in a execUnsafeLocalFunction since the event is not trusted.

    If I debug the code, I verified the 'event' object has "keyCode" and "which" properties set to 0. That might be the problem, but if I try to change those properties nothing happens because they are Read-only.

    Wednesday, March 27, 2013 1:04 AM

All replies

  • What's the scenario here?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 27, 2013 3:41 PM
    Moderator
  • I am building a Conversion/Calculator tool, and I don't use to use the Virtual Keyboard, since it's not going to work in a Snapped View (or visual won't make much sense).

    So I built a small keyboard with  numbers from 9-0 a "." and "delete". Now, when the user presses one of the key I want to create that event and dispatch it to the selected input box. 

    Wednesday, March 27, 2013 4:55 PM
  • 1) I don't understand why you're avoiding the OSK, it works fine in snapped view.

    2) I don't understand why you need to send a keyboard event.  Just insert the text directly into the textbox.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, March 28, 2013 2:23 PM
    Moderator
  • Hi Matt,

    thanks for replying. Let me explain my approach.

    1) Having a keyboard on the screen all the times was a design decision. I still support both virtual and physical keyboards, but like the calculator apps that we see on the Store, I would like to have a visible keypad all the time. 

    2) Simply adding the value to the control is not as simple as is sounds, just because we have to get the caret position on each input and the value to the right position in the input box. Additionally, I don't think there is a way to get the caret position on a IE browser so I bet Windows Store App API won't support it either.

    So the alternative would be to create a "TextEvent" and dispatch it to the Input box. Result, failure! Just like "KeyboardEvent" it's ignored.

    So, let me realign my question:

    - Is "KeyboardEvent" supported?

    - If not, is "TextEvent" supported?

    - If not, can I get and set input caret position?

    Thanks

    ----------------------------------------- EDIT--------------------

    Now I am finding out that .fireEvent("onchange") to trigger input change events, doesn't work also... WTH? Was the whole event API ignored or not implemented???

    Friday, March 29, 2013 2:51 AM