使用RxJS同时单击鼠标左键和右键

时间:2019-03-12 14:03:10

标签: javascript rxjs

我需要同时使用 RxJS 按下鼠标左右键来捕获事件。更准确地说,您需要使用鼠标左右键捕捉mouseup事件,最大间隔为300毫秒。

1 个答案:

答案 0 :(得分:0)

根据Tom Cumming的建议

import { fromEvent, combineLatest } from 'rxjs'
import { filter, map } from 'rxjs/operators'
const upLeft$ = fromEvent(document, 'mouseup').pipe(filter((e) => e.which === 1), map(()=>Date.now()))
const upRight$ = fromEvent(document, 'mouseup').pipe(filter((e) => e.which === 3), map(()=>Date.now()))

const combined$ = combineLatest(upLeft$ , upRight$).pipe(filter((e)=> (e[1]-e[0])<=300 && (e[1]-e[0])>0), map((e)=> (e[1]-e[0])));

combined$.subscribe(() => {
console.log("It work!!!")
});