FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Use Key Press

20mins

Implement a hook useKeyPress(targetKey, callback) that listens for keyboard events and triggers a callback when a specific key is pressed.

The hook should:

  • Accept a key (string) to listen for and a callback function.
  • Call the callback whenever the target key is pressed.
  • Properly add and remove the event listener to prevent memory leaks.

Example

let count = 0; useKeyPress("Enter", () => { count += 1; }); // Pressing "Enter" increases count

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests