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

后端 未结 6 1719
慢半拍i
慢半拍i 2020-12-11 06:37

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

6条回答
  •  自闭症患者
    2020-12-11 07:16

    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.

提交回复
热议问题