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

The last pressed key is [Enter]

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

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

+
+ ); + } +}