views:

53

answers:

1

I've noticed an problem when trying to catch keyboard shortcut: CTRL+an arrow.

I've handled keydown event. Now when I hold CTRL key then keydown event is fired once. If I hold an arrow (so that now I'm holding CTRL+an arrow) it doesn't fire another event. Is it forbidden from any reason? I guess I've already encountered this problem in Opera a few years ago and there was an option for it in browser.

My results:

  • holding CTRL, press an arrow -- fires event for CTRL and doesn't fire an event for an arrow
  • press CTRL + an arrow at once -- fires one event but only with keycode of CTRL.
  • holding CTRL, press a letter (eg. S) -- works as expected
  • press CTRL + letter (eg. S) -- works as expected

(Results are identical in Chrome and Firefox. Is the behaviour described above a standard?)

I'm using:

  • function OnKeyDown(e) { }
  • e.ctrlKey, e.which properties of event

The question is: what might be the problem?

+3  A: 

You should check if the event.ctrlKey flag is true, something like this:

document.getElementById('element').onkeydown = function (e) { 
  e = e || window.event;
  var keyCode = e.keyCode || e.which,
      arrow = {left: 37, up: 38, right: 39, down: 40 };

  if (e.ctrlKey) {
    switch (keyCode) {
      case arrow.left:
      //... handle Ctrl-LeftArrow
      break;
      //...
    }
  }
};

Check an example here.

CMS
I'm checking event.ctrlKey. I wrote it in my question. I tried your code: http://pastebin.org/168399 - and it doesn't work - maybe something is wrong with my keyboard.
MartyIX
I posted the code to pastebin because I don't know how else respond to your answer with such a long code (comment is too short, adding the code as answer is kind of rude, adding the code to the question is non-systematic). *CONFUSED*
MartyIX
I'm playing [here](http://jsbin.com/ojaki3) with the code you posted, and it works properly, if *LeftArrow* (without Ctrl) is pressed it alets "gosh", if *Ctrl* - *LeftArrow*, alerts "heureka"...
CMS
It doesn't work for me. Maybe the problem is really in my keyboard. I'm not sure. But thank you!
MartyIX
It's really a matter of keyboard driver, I've installed another and it partially works. I'm sorry for wasting your time.
MartyIX