0

收集JS双击shift键的方法,越多越好,例如:

let ot = 0;
document.onkeydown = (e) => {
    if (e.shiftKey) {
        let nt = new Date().getTime();
        let ct = nt - ot;
        if (ct > 0 && ct < 600) {
            console.log(e.keyCode);
        }
        ot = nt;
    }
};

2个回答

2

推荐 rxjs ,用了就不再想手动维护状态了哈哈哈

const shiftPress$ = Rx.Observable.fromEvent(document, 'keydown')
  .filter(({ key }) => key === 'Shift')

const shiftDoublePress$ = shiftPress$.buffer(shiftPress$.debounceTime(600))
  .map(group => group.length)
  .filter(count => count >= 2)

shiftDoublePress$.subscribe(console.log)

https://jsfiddle.net/straybug...

1

双击鼠标的是ondblclick,
连按两下键盘,有个想法是,监听keyup事件,每次按下记变量keynum, +1,keynum为2按二下,一定时间内没有再按第二下清空keynum

撰写答案