0.0.17 • Published 3 years ago

@~crazy/keywatch v0.0.17

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

KeyWatch

按键观察

安装

npm i @~crazy/keywatch -S

使用

import KeyWatch from '@~crazy/keywatch';

const watch = new KeyWatch();
// 监听
const removeListen = watch.listen('ctrl+c', () => {
    console.log('复制');
    // 返回 true 禁用浏览器默认行为
    return true;
});
// 移除
removeListen();

// 全局阻止默认行为
const watch = new KeyWatch(unll, true);

/**
 * 组合键
 * 先按下 ctrl+k,再按下 ctrl+u
 **/
watch.listen('ctrl+k>ctrl+u', () => {
    console.log('将文本转换为大写');
    return true;
});

// 监听多个
watch.listen(['ctrl+z', 'ctrl+a'], () => {
    console.log('撤销');
});

监听文本输入框

<input id="input" />
const watch = new KeyWatch(document.querySelector('#input'));
// 监听所有按键
watch.listen(event => {
    console.log(`按下了${event.key}`);
});

判断

// 是否按下 ctrl 键
console.log(watch.ctrl);
// 是否按下 shift 键
console.log(watch.shift);
// 是否按下 alt 键
console.log(watch.alt);
// 是否按下 meta 键
console.log(watch.meta);

// 判断其他按键
console.log(watch.isPressed('a'));
0.0.14

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.13

3 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago