Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - trigger specific keyboard keys

I have this piece of code:

window.addEventListener('keydown', function (e) { 
   console.log(e.which); 
   console.log(e.keyCode); 
});

 var evObj = new KeyboardEvent('keydown', {key:65});
 window.dispatchEvent(evObj);

Why i see 0 in console and not 65 ??

Also both e.keyCode and e.which are 0 and not 65, i am on Chrome latest version

thank you a lot.

like image 597
itsme Avatar asked Aug 03 '15 10:08

itsme


People also ask

How do you trigger a keypress event?

HTML and CSS layout: In the given layout, when an input is made in the field, the keyCode value will be logged in the box. The events related to keypresses are as follows : keydown: This event is triggered when a key is pressed down. keypress: This event is triggered when a key is pressed.

What are keyboard events in JavaScript?

KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type ( keydown , keypress , or keyup ) identifies what kind of keyboard activity occurred.

When user release key event are triggered?

The jQuery method of keyup() corresponds the keyup event, it is triggered when the user releases an key. It will not fire until the key is released. The difference between this one and the others (keypress and keydown) is that when a key is held down, this event will not fire until the key is released.


2 Answers

There is a bug in chrome, keyCode and which are not configurable.

Possible workarkaround: define a custom getter

 window.addEventListener('keydown', function (e) { console.log(e.which); });
 
 (function(o,k){
    //use createEvent for better compatibility
   var evObj = document.createEvent('HTMLEvents');
    evObj.initEvent('keydown', true, false);
    Object.defineProperty(evObj, 'keyCode', {
      get: function() {
        return k;
      }
    });
    Object.defineProperty(evObj, 'which', {
      get: function() {
        return k;
      }
    });
    o.dispatchEvent(evObj); 
 }(window,65));
like image 177
Dr.Molle Avatar answered Oct 08 '22 04:10

Dr.Molle


Also both e.keyCode and e.which are 0 and not 65, i am on Chrome latest version

Because you're setting key, not keyCode and which. According to MDN, key is a representation of the key, not a keycode. To initialize keyCode and/or which, you should...do that (see MDN's article on KeyboardEvent).

var evObj = new KeyboardEvent('keydown', {keyCode:65, which:65});

Here's an example, but it doesn't appear to work in Chrome (still get 0) — that's a Chrome bug, workaround below. Does work in Firefox. Fails in IE11 because IE11 doesn't like new KeyboardEvent:

window.addEventListener("keydown", function(e) {
  snippet.log("keyCode = " + e.keyCode + ", which = " + e.which);
}, false);
setTimeout(function() {
  var evObj = new KeyboardEvent('keydown', {keyCode:65, which:65});
  snippet.log("Sending event");
  window.dispatchEvent(evObj);
}, 300);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

You can work around the Chrome bug using the technique from this answer:

window.addEventListener("keydown", function(e) {
  snippet.log("keyCode = " + e.keyCode + ", which = " + e.which);
}, false);
setTimeout(function() {
  var evObj = new KeyboardEvent('keydown', {keyCode:65, which:65});
  // Chrome bug workaround:
  if (evObj.keyCode != 65) {
    Object.defineProperty(evObj, "keyCode", {
      value: 65
    });
    Object.defineProperty(evObj, "which", {
      value: 65
    });
  }
  snippet.log("Sending event");
  window.dispatchEvent(evObj);
}, 300);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
like image 4
T.J. Crowder Avatar answered Oct 08 '22 04:10

T.J. Crowder