Determine whether the ctrl key is pressed, determine whether the shift key is pressed, and determine whether the command (win) key is pressed

Recently, there is a need to judge and hold down ctrl to make multiple selections when clicking on an element.

The question is: How to judge whether ctrl is pressed? After consulting some information, I found that some attributes of e can be judged

e.ctrlKey   ctrl按住时为true
e.shiftKey  shift按住时为true
e.metaKey   win按住时为true,mac下win键是command
复制代码

It is enough to judge some properties of e in the corresponding event:

        document.addEventListener('click',(e)=>{
            console.log('e.ctrlKey',e.ctrlKey) //判断ctrl是否按下
            console.log('e.shiftKey',e.shiftKey) //判断shift是否按下
            console.log('e.metaKey',e.metaKey) //判断win键是否按下(mac上是command)
        })

复制代码

Guess you like

Origin juejin.im/post/7078891814584844295