react-use-gamepad
v0.4.7
Published
Event driven functionality for the GamepadAPI
Downloads
8
Maintainers
Readme
React useGamepad
A very simple library for using the Gamepad API. The Gamepad API expects the consumer to poll its state constantly. While this is useful for games, where you want granular control over user interaction, it doesn't make for very easy to implement HMIs.
This library takes care of the polling for you and just emits events, as if you were interacting with a mouse or keyboard in the browser.
Installation
yarn add react-use-gamepad
Use
- Add
<GamepadEventDispatcher/>
at the root of your app. This is where the polling occurs and events are emitted. - Import
useGamepad()
to hook into the Gamepad's events
Example
const Page = () => {
const ref = useRef<HTMLParagraphElement>(null)
const toast = useToast({
position: 'top',
isClosable: true,
duration: 1000,
})
useGamepad({
onButtonDown: (buttonNumber) => {
if (ref.current) {
ref.current.innerHTML = `${buttonNumber}<br/>${ref.current.innerHTML}`
}
},
})
return (
<div>
<p ref={ref} />
<GamepadEventDispatcher />
</div>
)
}
export default testpage