How can I tell if an event comes from right Ctrl key?
Asked Answered
Y

6

5

I have an event listener in Javascript, I can tell whether a key event is Ctrl (e.keyCode == 17), but how can I know this Ctrl comes from the right one or left one?

Yeorgi answered 4/1, 2011 at 5:22 Comment(1)
You can't. I'm sorry. I feel your pain.Munshi
T
3

I don't think the keyCode is different.

You can use e.ctrlKey for a better way to determine if the control key was pressed.

It seems Flash can not tell which one is pressed either (either that or coded incorrectly).

Touchy answered 4/1, 2011 at 5:26 Comment(3)
They are abstracted. E.g. I can choose a ton of different options for each of them in my Ubuntu Keyboard settings.Wedurn
You can definitely write software that differentiates between the two on a low level, so no, it can't be abstracted on the hardware level. It's probably abstracted on the OS level, though.Chattanooga
MSIE can differentiate the left and right Ctrl key, see my answer on that point.Mayenne
F
6

Just a quick note: I wouldn't base an architecture / design on the availability of the right control key - many laptop keyboards may not have two control keys.

Frolicsome answered 4/1, 2011 at 5:33 Comment(0)
T
3

I don't think the keyCode is different.

You can use e.ctrlKey for a better way to determine if the control key was pressed.

It seems Flash can not tell which one is pressed either (either that or coded incorrectly).

Touchy answered 4/1, 2011 at 5:26 Comment(3)
They are abstracted. E.g. I can choose a ton of different options for each of them in my Ubuntu Keyboard settings.Wedurn
You can definitely write software that differentiates between the two on a low level, so no, it can't be abstracted on the hardware level. It's probably abstracted on the OS level, though.Chattanooga
MSIE can differentiate the left and right Ctrl key, see my answer on that point.Mayenne
M
1

MSIE provides a ctrlLeftproperty on most events. The property values are:

  • true if the left key was pressed during the event
  • false if the left key was not pressed.

You can combine event.ctrlKey and event.ctrlLeft to determine if the right Ctrl key was pressed:

if (event.ctrlKey) {
    if (event.ctrlLeft) {
        // left Ctrl key pressed
    } else {
        // right Ctrl key pressed
    }
} else {
    // no Ctrl key pressed
}

Note that the ctrlLeftproperty in a keyup is undefined because the Ctrl key is not pressed anymore.

Tested under MSIE7 and MSIE9. Does not work under Firefox.

See http://help.dottoro.com/ljqlvhuf.php for details.

Mayenne answered 7/2, 2013 at 15:39 Comment(1)
Not very useful unfortunately if it's IE only.Touchy
P
1

If you traced it you will find the same key is used for both (17) .. I think it is not possible to differentiate

Plainsong answered 17/11, 2014 at 21:2 Comment(0)
Q
1

I don't know if it was available when this was asked, but you can distinguish left- from right-ctrl, as well as alt and shift. You can now use the KeyboardEvent.DOM_KEY_LOCATION_* properties to make this distinction.

See Can javascript tell the difference between left and right shift key?

Be aware though, I discovered that Chrome appears to have a defect in its implementation. See How can I distinguish left- and right- shift, ctrl, and alt keys onkeyup in Chrome with Javascript

Qua answered 10/12, 2014 at 5:44 Comment(0)
M
1

There is event.location property for left ctrl key it will be 1 for right one 2, you can check browser support on canIuse

if (e.which == 17) {
   if (event.location == 1) {
      // left ctrl key
   } else if (event.location == 2) {
      // right ctrl key
   }
}
Magnetics answered 16/1, 2017 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.