Simulate javascript keystroke events

Asked

Viewed 2,365 times

14

I’m looking for a way to simulate keypress of shift + 1 via javascript to create an exclamation mark ! for a series of tests (specs) in a framework in which I am involved.

I have used a framework (Syn) , example of jsFiddle, but I can’t get through it to play/fire the event of a keypress shift + 1.

According to the documentation would be:

Syn.type('JavaScriptMVC[shift]1[shift-up]', 'elementID')

But I didn’t succeed in detecting the .shiftKey == true at the event.

There is another way using "pure javascript" ?

I tried to use this answer in the OR without success and I realized that the createEvent is depressed and I have not found another way.

  • The test must necessarily be written in javascript... otherwise it could use some browser automation tool, such as the Selenium

  • Or else the Phantomjs... which allows writing javascript scripts to manipulate the browser, which is a headless Webkit.

  • Why not use Jquery and with it you make a Rigger event?

  • Managed to resolve this issue Sergio?

  • @durtto yes and no. For modern browsers Syn works. For IE<8 no.

  • I am in this situation(little like) and I am in search of the best code. About IE<8 I have no worry because I told you which browser to use.

Show 1 more comment

2 answers

1

I made a little script, I believe it’s not the solution, but I believe it might lead you to a.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form action="">
<input type="text" id="kinput" style="font-size:150%;width:600px">
<br>
<textarea style="width:600px;border:1px solid black" onfocus="this.blur()" id="textArea" rows="18"></textarea>
<br>
<input type="reset" value="Limpar" />
</form> 
    <script>
        document.getElementById('kinput').onkeydown = khandle
        document.getElementById('kinput').onkeyup = khandle
        document.getElementById('kinput').onkeypress = khandle
        function khandle(e) {
          e = e || event;
          var evt = e.type;
          while (evt.length < 10) evt += ' '
          showmesg(evt + 
            ' keyCode=' + e.keyCode + 
            ' which=' + e.which + 
            ' charCode=' + e.charCode +
            ' char=' + String.fromCharCode(e.keyCode || e.charCode) +
            (e.shiftKey ? ' +shift' : '') +
            (e.ctrlKey ? ' +ctrl' : '') +
            (e.altKey ? ' +alt' : '') +
            (e.metaKey ? ' +meta' : ''), 'key'
          )

        }
        function showmesg(t, form) {
            var area = document.getElementById('textArea');
            area.value += t + '\n';
            area.scrollTop = area.scrollHeight
        }
    </script>
</body>
</html>
  • 1

    Bruno, thank you for seeing my question. In fact what I seek is not to analyze the event nor the properties of the event object. What I want is to simulate/create/trigger an event that inserts text into my input programmatically. If you find the question unclear I regret and feel free to suggest changes. Take a look at jsFiddle I put in the question and maybe this will help clarify. Thank you.

  • Friend, maybe Voce can use Vbscript on Internetexplorer in a low security configuration. Through Vbscript Voce you can access the object of your browser and simulate a click or keypress...

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.