New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@~crazy/keywatch

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@~crazy/keywatch

用户按键输入观察者

latest
npmnpm
Version
0.0.17
Version published
Maintainers
1
Created
Source

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'));

Keywords

JavaScript

FAQs

Package last updated on 14 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts