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

use-wasd

v2.0.6

Published

Easy and agnostic react hook to handle keys and key-combinations on your keyboard.

Downloads

35

Readme

Version Build Size Downloads

Easy and agnostic react hook to handle keys and key-combinations on your keyboard.

npm install use-wasd

This hook returns an object with the keys and combos and their pressed state.

import useWASD from "use-wasd";

export default function App() {
  const keyboard = useWASD();

  return (
    <pre>
      <code>{JSON.stringify(keyboard)}</code>
    </pre>
  );
}

Try it yourself.


Table of Content


Options

You can pass an optional options object.

const options = { allow: ["w", "a", "s", "d"] };

export default function App() {
  const { w, a ,s ,d }  = useWASD(options);
  ...
}

Available options are:

  • allow
  • block
  • combos
  • initialValue
  • preventDefault
  • ref

allow/block

You can and should explicitly allow or block keys.

const options = {
  // either
  allow: ["w", "shift", "c"],
  // or
  block: ["c"],
};

Do not use both.


combos

You can define custom combos.

const options = {
  allow: ["w", "shift", "space"],
  combos: { sprint: ["w", "shift"], sprintJump: ["w", "shift", "space"] }
};

export default function App() {
  const { sprint, sprintJump } = useWASD(options);
  ...
}

You don´t need to also allow combos, it´s enough if the keys for the combo are allowed and not blocked.

Try it yourself.


initialValue

You can initially fill the object.

const options = {
  initialValue: { w: true, shift: false, sprint: false },
};

Note that the "keydown" event will always set keys true, while the "keyup" event will always set to false. Initially setting a key to true will not reverse the mechanism.

Try it yourself.


preventDefault

You can call event.preventDefault() to prevent default actions for keys.

const options = { preventDefault: ["arrowup", "arrowdown"] };

You can also set it to true to prevent the default function for every key.

const options = { preventDefault: true };

Be aware that by doing so you can jeopardize the a11y

Try it yourself.


ref

By default the EventListener will be added to the document, if you want it to be added to another element, you can pass it as ref.

export default function App() {
  const ref = useRef();
  const keyboard = useWASD({...options, ref});
  ...
}

Try it yourself.


Performance

Destructuring

We recommend destructuring the object returned by useWASD.


export default function App() {
-  const keyboard  = useWASD();
+  const { w, a ,s ,d }  = useWASD();
  ...
}

Memoization

We recommend memoizing the options object.

Here are 3 common examples of passing the options object:

  1. Declare it outside the Component.
const options = {...};

export default function App() {
  const keyboard = useWASD(options);
  ...
}
  1. Using useMemo hook.

export default function App() {
  const options = useMemo(() => ({...}), []);
  const keyboard = useWASD(options);
  ...
}
  1. Using useRef hook.

export default function App() {
  const options = useRef({...});
  const keyboard  = useWASD(options.current);
  ...
}

Do not pass the object directly into the hook, this would cause unnecessary rerenders.


export default function App() {
  const keyboard = useWASD({...});
  ...
}

Examples

Basic Example

combos Example

initialValue Example

preventDefault Example

ref Example


Learn

useWASD vanilla source

if you are familiar with typescript, you can also find the source code on github.