How to detect pressed keys on the click of AngularJS

后端 未结 5 1575
渐次进展
渐次进展 2021-02-07 00:44

I\'m using angularjs on a web application that I need to figure out how can I detect is keys like ctrl, shift or alt are pressed when I click so

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 01:36

    Since I'm not sure what each browser provides, I would do it this way:

    shiftPressed = event.shiftKey || (event.keyCode === 16);
    

    On Chorme for example I can't see any event.keyCode on the click event:

    altKey: false
    bubbles: true
    button: 0
    buttons: 0
    cancelBubble: false
    cancelable: true
    clientX: 753
    clientY: 193
    ctrlKey: false
    currentTarget: null
    defaultPrevented: false
    detail: 1
    eventPhase: 0
    fromElement: null
    isDefaultPrevented: ()
    isImmediatePropagationStopped: ()
    isTrusted: true
    isTrusted: true
    layerX: 4
    layerY: -15
    metaKey: false
    movementX: 0
    movementY: 0
    offsetX: 4
    offsetY: -15
    pageX: 1381
    pageY: 193
    path: Array[16]
    relatedTarget: null
    returnValue: true
    screenX: 753
    screenY: 278
    shiftKey: true
    srcElement: span.glyphicon.glyphicon-plus
    stopImmediatePropagation: ()
    target: span.glyphicon.glyphicon-plus
    timeStamp: 1445613423528
    toElement: span.glyphicon.glyphicon-plus
    type: "click"
    view: Window
    webkitMovementX: 0
    webkitMovementY: 0
    which: 1
    x: 753
    y: 193
    

提交回复
热议问题