Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

Add `useKeyPress` hook

cihatf4ee70122540
diff --git a/src/hooks/useKeyPress.tsx b/src/hooks/useKeyPress.tsx
new file mode 100644
index 00000000..c0767c00
--- /dev/null
+++ b/src/hooks/useKeyPress.tsx
@@ -0,0 +1,27 @@
+import { useState, useEffect } from "react";
+
+const useKeyPress = (targetKey) => {
+ const [keyPressed, setKeyPressed] = useState<boolean>(false);
+ function downHandler({ key }) {
+ if (key === targetKey) {
+ setKeyPressed(true);
+ }
+ }
+ const upHandler = ({ key }) => {
+ if (key === targetKey) {
+ setKeyPressed(false);
+ }
+ };
+ useEffect(() => {
+ window.addEventListener("keydown", downHandler);
+ window.addEventListener("keyup", upHandler);
+
+ return () => {
+ window.removeEventListener("keydown", downHandler);
+ window.removeEventListener("keyup", upHandler);
+ };
+ }, []);
+ return keyPressed;
+};
+
+export default useKeyPress;