useKeypress

This hook will tell you if a key is currently being pressed and return a boolean value indicating that.

Usage

import React from 'react';
import { useKeypress } from '@fransvilhelm/hooks';
const Indicator = ({ trackKey }) => {
const pressing = useKeypress(trackKey);
return <p>{pressing ? 'pressing' : 'not pressing'}</p>;
};

Example

  • a: 👆
  • b: 👆
  • c: 👆
  • d: 👆
  • e: 👆
  • f: 👆
  • g: 👆
  • h: 👆
  • i: 👆
  • j: 👆
  • k: 👆
  • l: 👆
  • m: 👆
  • n: 👆
  • o: 👆
  • p: 👆
  • q: 👆
  • r: 👆
  • s: 👆
  • t: 👆
  • u: 👆
  • v: 👆
  • w: 👆
  • x: 👆
  • y: 👆
  • z: 👆

Parameters

useKeypress accepts one parameter – the key to look for.

ParamTypeRequiredDefaultDescription
keystring true-The key to look for

Returns

useKeypress returns a boolean indicating if the passed in key is currently pressed or not.

ReturnsTypeDescription
pressingbooleantrue if the key is currently pressed, false otherwise