Programming Exercises

Create a React component that counts how many times a specific key (e.g., 'a') is pressed on the keyboard.

Easy

Problem Statement

Create a React component that counts how many times a specific key (e.g., 'a') is pressed on the keyboard.

Sample Output

Key 'a' pressed: 0 (increases each time 'a' is pressed).

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
import React, { useState, useEffect } from "react"; const KeyPressCounter = () => { const [count, setCount] = useState(0); useEffect(() => { const handleKeyPress = (event) => { // Increment the count if "a" is pressed }; // Add event listener return () => { // Cleanup event listener }; }, []); return <p>Key "a" pressed: {count}</p>; }; export default KeyPressCounter;
Log in or sign up to run or submit your code.Log inSign up
Expected Output
Key 'a' pressed: 0 (increases each time 'a' is pressed).