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

The last pressed key is [Enter]

-
-); +export class App extends React.PureComponent { + state = { + pressedKey: '', + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handleKeyPress); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handleKeyPress); + } + + handleKeyPress(event: KeyboardEvent): void { + this.setState({ pressedKey: event.key }); + } + + render() { + return ( +
+

+ { + !this.state.pressedKey + ? 'Nothing was pressed yet' + : `The last pressed key is [${this.state.pressedKey}]` + } +

+
+ ); + } +}