question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useKeyPress doesn't track repeated combination keypresses until the entire combination is released

See original GitHub issue

Describe the Bug

When using useKeyPress to track combinations of keys, for example Meta+z, repeated keypresses are not captured if you continue to hold down the Meta key.

Your Example Website or App

https://codesandbox.io/s/recursing-driscoll-stvwou?file=/src/App.js

Steps to Reproduce the Bug or Issue

When const comboPressed = useKeyPress("Meta+z");:

  • Press and hold Meta
  • Press and hold z
  • comboPressed is true
  • Release z
  • comboPressed is still true and so additional presses of z can’t be detected
  • Release Meta
  • comboPressed is now false

The inverse does not have the same behaviour:

  • Press and hold z
  • Press and hold Meta
  • comboPressed is true
  • Release Meta
  • comboPressed is now false and so additional presses of z can be detected

Expected behavior

As a user I would expect that after I press Meta and z, and then continue to hold Meta but release z that the pressed state of useKeyPress("Meta+z") is false.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 103.0.5060.53

Additional context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
moklickcommented, Aug 2, 2022

Ok, thanks. We will need to check how other libs handle this

0reactions
allenhwkimcommented, Nov 20, 2022

IMO, it should use keydown event instead of keypress event. The following code is what I have tried without useKeyPress API, and it seems working

Note. this example is using state management library

import * as React from 'react';
import {KeyboardEvent} from 'react';
import ReactFlow from 'reactflow';
import 'reactflow/dist/style.css';

import useStore from './store';

function Flow() {
  const { nodes, edges, onNodesChange, onEdgesChange, onConnect } = useStore();

  const onKeyDown = (event : KeyboardEvent) => {
    const ctrl = event.ctrlKey ? 'Control-' : '';
    const alt = event.altKey ? 'Alt-' : '';
    const meta = event.metaKey ? 'Meta-' : '';
    const shift = event.shiftKey ? 'Shift-' : '';
    const key = `${ctrl}${alt}${shift}${meta}${event.key}`;
    if (!['Alt', 'Shift', 'Meta', 'Control'].includes(event.key)) {
      console.log(`${key} pressed`);
    }
  }

  return (
    <ReactFlow 
      tabIndex={0}
      onKeyDown={(e) => onKeyDown(e)}
      nodes={nodes}
      edges={edges}
      onNodesChange={onNodesChange}
      onEdgesChange={onEdgesChange}
      onConnect={onConnect}
    ></ReactFlow>
  );
}

export default Flow;
Read more comments on GitHub >

github_iconTop Results From Across the Web

useKeyPress React Hook - useHooks
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to...
Read more >
Detecting combination keypresses (Control, Alt, Shift)?
The keypress event is fired as soon as a key has been pressed. If you are pressing multiple keys ...
Read more >
Detect single and multiple keypress events: JavaScript
When we use a 'keydown' event we are listening for the moment when a key is pressed. We can similarly react when a...
Read more >
UI Events - W3C
The DOM Event Model allows a DOM implementation to support multiple modules of events. The model has been designed to allow addition of...
Read more >
Keyboard shortcuts, hotkeys, and special keys (Windows)
You can use key press combinations to perform common tasks in Windows. ... Copy a picture of the current window (not the entire...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found