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

@solid-primitives/keyboard

v1.2.8

Published

A library of reactive promitives helping handling user's keyboard input.

Downloads

58,312

Readme

@solid-primitives/keyboard

lerna size version stage

A library of reactive promitives helping handling user's keyboard input.

  • useKeyDownEvent — Provides a signal with the last keydown event.
  • useKeyDownList — Provides a signal with the list of currently held keys
  • useCurrentlyHeldKey — Provides a signal with the currently held single key.
  • useKeyDownSequence — Provides a signal with a sequence of currently held keys, as they were pressed down and up.
  • createKeyHold — Provides a signal indicating if provided key is currently being held down.
  • createShortcut — Creates a keyboard shotcut observer.

Installation

npm install @solid-primitives/keyboard
# or
pnpm add @solid-primitives/keyboard
# or
yarn add @solid-primitives/keyboard

useKeyDownEvent

Provides a signal with the last keydown event.

This is a singleton root primitive that will reuse event listeners and signals across dependents.

How to use it

useKeyDownEvent takes no arguments, and returns a signal with the last keydown event.

import { useKeyDownEvent } from "@solid-primitives/keyboard";

const event = useKeyDownEvent();

createEffect(() => {
  const e = event();
  console.log(e); // => KeyboardEvent | null

  if (e) {
    console.log(e.key); // => "Q" | "ALT" | ... or null
    e.preventDefault(); // prevent default behavior or last keydown event
  }
});

useKeyDownList

Provides a signal with the list of currently held keys, ordered from least recent to most recent.

This is a singleton root primitive that will reuse event listeners and signals across dependents.

How to use it

useKeyDownList takes no arguments, and returns a signal with the list of currently held keys

import { useKeyDownList } from "@solid-primitives/keyboard";

const keys = useKeyDownList();

createEffect(() => {
  console.log(keys()); // => string[] — list of currently held keys
});

<For each={keys()}>
  {key => <kbd>{key}</kdb>}
</For>

useCurrentlyHeldKey

Provides a signal with the currently held single key. Pressing any other key at the same time will reset the signal to null.

This is a singleton root primitive that will reuse event listeners and signals across dependents.

How to use it

useCurrentlyHeldKey takes no arguments, and returns a signal with the currently held single key.

import { useCurrentlyHeldKey } from "@solid-primitives/keyboard";

const key = useCurrentlyHeldKey();

createEffect(() => {
  console.log(key()); // => string | null — currently held key
});

useKeyDownSequence

Provides a signal with a sequence of currently held keys, as they were pressed down and up.

This is a singleton root primitive that will reuse event listeners and signals across dependents.

How to use it

useKeyDownSequence takes no arguments, and returns a single signal.

import { useKeyDownSequence } from "@solid-primitives/keyboard";

const sequence = useKeyDownSequence();

createEffect(() => {
  console.log(sequence()); // => string[][] — sequence of currently held keys
});

// example sequence of pressing Ctrl + Shift + A
// [["Control"], ["Control", "Shift"], ["Control", "Shift", "A"]]

createKeyHold

Provides a boolean signal indicating if provided key is currently being held down.

Holding multiple keys at the same time will return false — holding only the specified one will return true.

How to use it

createKeyHold takes two arguments:

  • key keyboard key to listen for
  • options additional configuration:
    • preventDefault — call e.preventDefault() on the keyboard event, when the specified key is pressed. (Defaults to true)
import { createKeyHold } from "@solid-primitives/keyboard";

const pressing = createKeyHold("Alt", { preventDefault: false });

<p>Is pressing Alt? {pressing() ? "YES" : "NO"}</p>;

createShortcut

Creates a keyboard shotcut observer. The provided callback will be called when the specified keys are pressed.

How to use it

createShortcut takes three arguments:

  • keys — list of keys to listen for
  • callback — callback to call when the specified keys are pressed
  • options — additional configuration:
    • preventDefault — call e.preventDefault() on the keyboard event, when the specified key is pressed. (Defaults to true)
    • requireReset — If true, the shortcut will only be triggered once until all of the keys stop being pressed. Disabled by default.
import { createShortcut } from "@solid-primitives/keyboard";

createShortcut(
  ["Control", "Shift", "A"],
  () => {
    console.log("Shortcut triggered");
  },
  { preventDefault: false, requireReset: true },
);

Preventing default

When preventDefault is true, e.preventDefault() will be called not only on the keydown event that have triggered the callback, but it will optimistically also prevend the default behavior of every previous keydown that will have the possibility to lead to the shotcut being pressed.

E.g. when listening for Control + Shift + A, all three keydown events will be prevented.

DEMO

Working demo of some of the primitives in keyboard package:

https://codesandbox.io/s/solid-primitives-keyboard-demo-s2l84k?file=/index.tsx

Changelog

See CHANGELOG.md