7

I have an input field, where the user can write things. I want that, if the user press the key "1", to simulate another key press "F". So, if the user typer the character 1, in the field it will display 1f. How can i do it, without using jQuery?

Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101

1 Answers1

5
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent ("keypress", true, true, window,
                0, 0, 0, 0,
                13, 13); 
var canceled = !body.dispatchEvent(evt);

Documentation: https://developer.mozilla.org/en-US/docs/Web/API/event.initKeyEvent

For Webkit-based browser the initialization might be a bit different

initKeyboardEvent(in DOMString typeArg, 
              in boolean canBubbleArg, 
              in boolean cancelableArg, 
              in views::AbstractView viewArg, 
              in DOMString keyIdentifierArg, 
              in unsigned long keyLocationArg, 
              in boolean ctrlKeyArg, 
              in boolean shiftKeyArg, 
              in boolean altKeyArg, 
              in boolean metaKeyArg, 
              in boolean altGraphKeyArg);
Carlos Bergen
  • 813
  • 2
  • 8
  • 20