0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】特定のキーが入力された時に処理を行う方法

Posted at

Escが入力された際の処理

以下のように記述するとEscが入力された際に実行される処理を定義できます。

import { useEffect } from 'react';

const Sample = () => {
  useEffect(() => {
    const handleEscape = (event) => {
      if (event.key === 'Escape') {
        // エスケープキーが押された時の処理をここに記述する
        console.log('Escape key was pressed!');
      }
    };

    document.addEventListener('keydown', handleEscape);

    return () => {
      document.removeEventListener('keydown', handleEscape);
    };
  }, []);

  return <div>sample</div>;
};

export default MyComponent;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?