2

Is there a way to programmatically (using JS) start a keyboard event flow on an input[type=text] or textarea?
I mean that I want to make the element write a character as if the user has used the keyboard to do it. Meaning, it will consider the caret position, text selection, overwrite\insert state, CAPS-LOCK state, and so on.

Existing solutions (and questions) I've found don't give me that result, but just trigger a keydown or some other keyboard event on that element. I want to do something like:

document.getElementById('input').pressKey('e');

The solution has to be cross-browser (Firefox, Chrome and IE8+), and should not contain external plugins such as Adobe Flash - only things that can be sent with a web page.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
EZLearner
  • 1,614
  • 16
  • 25
  • All I know is the jQuery way, if you're interested -- http://stackoverflow.com/questions/3276794/jquery-or-pure-js-simulate-enter-key-pressed-for-testing – tymeJV Aug 16 '13 at 18:38
  • Like this? http://thezillion.wordpress.com/2012/02/27/javascript-typing-effect/ – DevlshOne Aug 16 '13 at 18:40
  • 1
    Neither method acts as if the user had pressed a key. – EZLearner Aug 16 '13 at 18:47
  • I don't see this being possible as detecting some of the states you require is beyond the scope of javascript. Caps lock could potentially be inferred if you take a keycode event, check for shift and see if the character is upper case, but there is no such way to know from a high level script what the low level states are. This is a security risk for javascript to have access to the OS level and its just not allowed. – James LeClair Aug 16 '13 at 18:57
  • I don't mean infer all those properties, just to use a browser API, if there is one. That is not a security risk though that does not necessarily mean accessing the OS level - it means only accessing the browser. – EZLearner Aug 16 '13 at 19:05
  • @EZSlaver yes, I know you don't want to infer them, but without reaching to OS level, you can't access this information other than inference. There are no browser api's that offer this. Especially if you want to support as far back as ie8. Perhaps a re-visit of the strategy could produce a different way to achieve your overall goal with better results. – James LeClair Aug 16 '13 at 19:14
  • LOL this is the strategy revisited. I don't *have* to have an answer, but it will greatly help me. That's why I'l keep it open for a while. Thanks. – EZLearner Aug 16 '13 at 22:58

0 Answers0