diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..015162529 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,37 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +interface State { + PressedKey: string; +} + +export class App extends React.Component { + state: Readonly = { + PressedKey: '', + }; + + componentDidMount(): void { + document.addEventListener('keyup', (event: KeyboardEvent) => { + this.setState({ PressedKey: event.key }); + }); + } + + componentWillUnmount() { + document.removeEventListener('keyup', (event: KeyboardEvent) => { + this.setState({ PressedKey: event.key }); + }); + } + + render() { + const { PressedKey } = this.state; + + return ( +
+

+ {PressedKey + ? `The last pressed key is [${PressedKey}]` + : `Nothing was pressed yet`} +

+
+ ); + } +}