Simulate key events in javascript

14

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

I have used a framework (Syn) , jsFiddle example , but I can not get through it / trigger the event of a keypress ! .

According to the documentation would be:

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

But I was not successful in detecting shift + 1 in the event.

Is there another way to use "pure" javascript?

I tried using this answer in the unsuccessful OS and realized that .shiftKey == true is deprecated and I did not find another way .

    
asked by anonymous 13.04.2014 / 22:51

2 answers

1

I made a small script, I believe that is not the solution, but I believe it can lead to one.

<!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>
    
14.04.2014 / 19:25
1

There is a nice lib for working with hotkeys jquery.hotkeys

I updated the fiddle, have a look link

    
29.04.2014 / 17:01