判断ctrl键是否按下,判断shift键是否按下,判断command(win)键是否按下

最近有个需求是判断按住ctrl,点击元素时进行多选,

问题是: 如何判断ctrl是否按下,查阅了一些资料,发现判断e的某些属性即可

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

在对应的事件中判断e的某些属性即可:

        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)
        })

复制代码

猜你喜欢

转载自juejin.im/post/7078891814584844295