npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@longo-andrea/keybind

v0.9.0

Published

A Typescript library for easily binding keyboard keys

Downloads

2

Readme


KeyBind is a library written in Typescripts, that aim to provide an easy way to bind keyboard keys and create amazing and interactive web applications.

  • 😷 Type safety with Typescript
  • 📋 IDE integration with Typedoc
  • ✨ Framework agnostic

Install

Install the package with your favourite package manager. For example:

npm i keybind

Usage

Start using Keybind is pretty simple:

import { initKeybind, addKey } from "keybind";

// Init KeyBind library in document.body
initKeybind(document.body);

// Then we can add all the biding we want
addKey(
	"S",
	{
		keyupCallback: () => {
			console.log("Hello from keyup S");
		},
		keydownCallback: () => {
			console.log("Hello from keydown S");
		},
	},
	{ preventRepeatOnKeyDown: true }
);

For a live demo take a look to the demo folder

API

AddKey

Add a new key to the binding list. Let you specify which key should be binded, keyup and keydown callback, and eventually some options.

Parameters

| Name | Type | Mandatory | Default | | --------- | ------------------- | --------- | ----------------------------------- | | stringKey | string | true | - | | callbacks | KeyBindingCallbacks | true | - | | options | KeyBindingOptions | false | { preventRepeatOnKeyDown: false } |

Returns

void

Usage

import { addKey } from "keybind";

// Init KeyBind and do some stuff ...

// Then we can add all the biding we want
addKey(
	"S",
	{
		keyupCallback: () => {
			console.log("Hello from keyup S");
		},
		keydownCallback: () => {
			console.log("Hello from keydown S");
		},
	},
	{ preventRepeatOnKeyDown: true }
);

ClearKeys

Remove all the bindend keys from the list.

Parameters

No parameters required.

Returns

void

Usage

import { clearKeys } from "keybind";

// Init KeyBind and do some stuff ...

// Then we can add all the biding we want
clearKeys();

InitKeybind

Init keybind library in a given target.

Parameters

| Name | Type | Mandatory | Default | | ------ | ----------- | --------- | ------- | | target | HTMLElement | true | - |

Returns

void

Usage

import { initKeybind } from "keybind";

initKeybind(document.body);

IsBindingEnabled

Returns whether the binding is enabled or not.

Parameters

No parameters required

Returns

boolean - whether the binding is enabled

Usage

import { isBindingEnabled } from "keybind";

// Init KeyBind and do some stuff ...

// Then we can add all the biding we want
if (isBindingEnabled()) {
	console.log("Bindining enabled");
} else {
	console.log("Bindining disabled");
}

IsKeyBinded

Returns whether a given key is binded or not.

Parameters

| Name | Type | Mandatory | Default | | --------- | ------ | --------- | ------- | | stringKey | string | true | - |

Returns

boolean - returns whether the given key is binded

Usage

import { isKeyBinded } from "keybind";

// Init KeyBind and do some stuff ...

if (isKeyBinded("S")) {
	console.log("S is binded");
} else {
	console.log("S isn't binded");
}

RemoveKey

Remove the given key (if binded) from the binding list.

Parameters

| Name | Type | Mandatory | Default | | --------- | ------ | --------- | ------- | | stringKey | string | true | - |

Returns

void

Usage

import { removeKey } from "keybind";

// Init KeyBind and do some stuff ...

// Then we can add remove the biding we want
removeKey("S");

ToggleBinding

Allows to enable/disable binding.

Parameters

| Name | Type | Mandatory | Default | | ------ | ------- | --------- | ------- | | enable | boolean | true | - |

Returns

void

Usage

import { toggleBinding } from "keybind";

// Init KeyBind and do some stuff ...

// Let's turn off binding
toggleBinding(false);

// ... and then turn on again
toggleBinding(true);

UnbindListeners

Unbind Keybind library.

Parameters

| Name | Type | Mandatory | Default | | ------ | ----------- | --------- | ------- | | target | HTMLElement | true | - |

Returns

void

Usage

import { unbindListeners } from "keybind";

// Init KeyBind and do some stuff ...

unbindListeners(document.body);

Types

KeyBindingCallbackMap

Represents the key binded by the library.

Properties

| Name | Type | Mandatory | | --------- | ------------------- | --------- | | callbacks | KeyBindingCallbacks | true | | options | KeyBindingOptions | false |

KeyBindingCallbacks

Represents the callbacks binded by the library.

Properties

| Name | Type | Mandatory | | --------------- | --------------------------- | --------- | | keyupCallback | (e?: KeyboardEvent) => void | false | | keydownCallback | (e?: KeyboardEvent) => void | true |

KeyBindingOptions

Represent the available options for binding.

Properties

| Name | Type | Mandatory | | ---------------------- | ------- | --------- | | preventRepeatOnKeyDown | boolean | false |

Demo

You can try a live demo of Keybind here: https://csjgt3.csb.app/

The full code of the demo is available here: https://github.com/longo-andrea/keybind/tree/main/demo

Licence

MIT License © 2022 Andrea Longo