This is just a technology testing project based on Create React App and TailwindCSS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
465B

  1. import { useEffect } from 'react';
  2. /**
  3. * useKeyPress
  4. * @param {string} key - the name of the key to respond to, compared against event.key
  5. * @param {function} action - the action to perform on key press
  6. */
  7. export default function useKeypress(key, action) {
  8. useEffect(() => {
  9. function onKeyup(e) {
  10. if (e.key === key) action()
  11. }
  12. window.addEventListener('keyup', onKeyup);
  13. return () => window.removeEventListener('keyup', onKeyup);
  14. });
  15. }