events - Javascript - trigger specific keyboard keys - Stack Overflow

I have this piece of code:window.addEventListener('keydown', function (e) { console.log(e.whi

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.

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.

Share Improve this question edited Aug 3, 2015 at 10:40 Filippo oretti asked Aug 3, 2015 at 10:28 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges 3
  • 2 I tried google javascript keyevent chrome, it should be a bug from this. The link is from Post. – fuyushimoya Commented Aug 3, 2015 at 10:44
  • If you only need Chrome as you mented elsewhere, this is a duplicate of this question which has a working answer. – T.J. Crowder Commented Aug 3, 2015 at 10:51
  • Object.defineProperty(evObj , "which", {"value" : 666}) will overwrite the which as a workaround. – Mouser Commented Aug 3, 2015 at 10:53
Add a ment  | 

3 Answers 3

Reset to default 5

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 patibility
   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));

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./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./a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Note: In Firefox, the keyCode property does not work on the onkeypress event (will only return 0). For a cross-browser solution, use the which property together with keyCode, e.g:

var x = event.which || event.keyCode;  // Use either which or keyCode, depending on browser support

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265223a4619413.html

相关推荐

  • events - Javascript - trigger specific keyboard keys - Stack Overflow

    I have this piece of code:window.addEventListener('keydown', function (e) { console.log(e.whi

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信