1.0.7 • Published 2 years ago

@wgsdk/keyboard v1.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Comfortable using keyboard

Install with npm:

$ npm install --save @wgsdk/keyboard

Usage

Keydown, Keyup, Keypress

Clicking on specific button

import {keyboard} from '@wgsdk/keyboard';

const qDownCallback = (keyboardEvent) => {
    console.log('Q down');
}

const qUpCallback = (keyboardEvent) => {
    console.log('Q up');
}

const qPressCallback = (keyboardEvent) => {
    console.log('Q press');
}

keyboard.onDown(Key.Q, qDownCallback);
keyboard.onUp(Key.Q, qUpCallback);
keyboard.onPress(Key.Q, qPressCallback);

// click on Q
// 'Q down'
// 'Q press'

// release the key Q
// 'Q up'

Remove listeners

keyboard.offDown(Key.A, aDownCallback);
keyboard.offUp(Key.S, sUpCallback);
keyboard.offPress(Key.D, dPressCallback);

Document listeners

const callback = (keyboardEvent) => {
    console.log(e.code);    
}

keyboard.onKeyDown(callback);
keyboard.onKeyUp(callback);

Longpress

const callback = (keyboardEvent) => {
    console.log('F long pressed');    
}

keyboard.onLongPress(Key.F, callback);

// will call the function after 800 milliseconds of holding the key

Also, you can configure your longpress delay this way

keyboard.configure({longPressDelayMs: 1200});

Keystroke check

keyboard.isPressed(Key.G);
keyboard.isPressed(Key.Enter);
keyboard.isShiftPressed();
keyboard.isLeftAltPressed();
keyboard.isMetaPressed();

// returns boolean

Waiting keydown

async function wait() {
    // Some actions ...
    console.log('Waiting for Enter key');
    await keyboard.waitKey(Key.Enter);
    console.log('Enter clicked!');
    // Some actions ...
}

Pause/Resume

keyboard.pause();

// ...

keyboard.resume();

The MIT License

Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago