8000 GitHub - Yukiniro/toukey at v1.1.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

👻 Toukey is a simple and efficient keyboard events library.

License

Notifications You must be signed in to change notification settings

Yukiniro/toukey

Repository files navigation

Toukey

npm GitHub npm bundle size

Toukey is a simple and efficient keyboard events library. That's toukey's doc site.

Install

npm i toukey --save

Use

Browser

You could download and link toukey.js in your HTML, It can also be downloaded via UNPKG | jsDelivr

<script src="https://unpkg.com/toukey/dist/toukey.umd.min.js"></script>
<script>
  toukey.subscribe('space', function() {
    console.log('space');
  });
</script>

Basic Use

You will need Node.js installed on your system.

import { subscribe } from 'toukey';

subscribe('scope', () => {
  console.log('scope');
});

Unsubscribe

The subscribe interface returns a function that can be called to cancel the current event listener.

import { subscribe } from 'toukey';

const unsubscribe = subscribe('scope', () => {
  console.log('scope');
});

unsubscribe();

Multiple Key

Multiple event listeners can be created at once when using toukey.

import { subscribe } from 'toukey';

subscribe('scope, a', () => {
  console.log('scope or a');
});

Compose key

Through '+', you can monitor the key combination, and you can also customize the key combination separator.

import { subscribe } from 'toukey';

subscribe('ctrl+a', () => {
  console.log('ctrl+a');
});

Scope

When calling, the third parameter can specify the scope of the event. "default" is the default scope.

import { subscribe, setScope } from 'toukey';

const defaultHandler = () => {
  console.log('scope in default');
};

const subHandler = () => {
  console.log('scope in sub');
};

subscribe('scope', defaultHandler, 'default');
subscribe('scope', subHandler, { scope: 'sub' });

setScope

The current effective scope can be set through the setScope interface.

import { setScope } from 'toukey';

setScope('sub');

getScope

The current effective scope can be obtained through the getScope interface.

import { getScope } from 'toukey';

console.log(getScope());

deleteScope

The specified scope can be deleted through the deleteScope interface.

import { deleteScope } from 'toukey';

deleteScope('main');

Special scope *

If scope is specified as "*", the listener function will ignore the scope setting.

import { subscribe, setScope } from 'toukey';

const handler = () => {
  console.log('scope in default');
};

const subHandler = () => {
  console.log('scope in sub');
};

subscribe('scope', defaultHandler, '*');
subscribe('scope', subHandler, 'sub');
setScope('sub');

keydown and keyup

You can set the type for handler call.

import { subscribe, setScope } from 'toukey';

const downHandler = () => {
  console.log('scope keydown');
};

const upHandler = () => {
  console.log('scope keyup');
};

subscribe('scope', downHandler, {'keydown'});
subscribe('scope', upHandler, {'keyup'});

clearAll

You could clear all listeners by the clearAll()

import { clearAll } from 'toukey';

clearAll();
0