reactuals
Version:
A useful package providing a collection of 50+ React hooks and utilities to simplify React development.
25 lines (24 loc) • 845 B
JavaScript
import { useEffect, useState } from "react";
export function useInputDevice() {
const [deviceType, setDeviceType] = useState("unknown");
useEffect(() => {
const handlePointerDown = (e) => {
if (e.pointerType === "mouse") {
setDeviceType("mouse");
}
else if (e.pointerType === "touch") {
setDeviceType("touch");
}
};
const handleKeyDown = () => {
setDeviceType("keyboard");
};
window.addEventListener("pointerdown", handlePointerDown);
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("pointerdown", handlePointerDown);
window.removeEventListener("keydown", handleKeyDown);
};
}, []);
return deviceType;
}