Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 125 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,118 @@
# react-gamepad [![npm][npm-image]][npm-url]
# react-gamepad

[npm-image]: https://img.shields.io/npm/v/react-gamepad.svg
[npm-url]: https://npmjs.org/package/react-gamepad

A Gamepad handler as a React component
A Gamepad handler as a React component, now with vibration control

```bash
npm install react-gamepad --save
yarn add "https://github.com/talon2295/react-gamepad"
```

This component provides handlers for gamepad events, to use directly in your React components


## Usage example

```js
import Gamepad from 'react-gamepad'

class App extends Component {
connectHandler(gamepadIndex) {
console.log(`Gamepad ${gamepadIndex} connected !`)
}

disconnectHandler(gamepadIndex) {
console.log(`Gamepad ${gamepadIndex} disconnected !`)
}

buttonChangeHandler(buttonName, down) {
console.log(buttonName, down)
}

axisChangeHandler(axisName, value, previousValue) {
console.log(axisName, value)
}

buttonDownHandler(buttonName) {
console.log(buttonName, 'down')
}

buttonUpHandler(buttonName) {
console.log(buttonName, 'up')
}

render() {
return (
<Gamepad
onConnect={this.connectHandler}
onDisconnect={this.disconnectHandler}

onButtonChange={this.buttonChangeHandler}
onAxisChange={this.axisChangeHandler}
/>
)
}
```tsx
import Gamepad, { Axis, Button, useVibration } from "react-gamepad";

export default function Joypad() {
const { vibrate } = useVibration(0);

const onJoyConnection = () => {};
const onJoyDisconnection = () => {};
const onButtonChange = (button: Button, pressed: boolean) => {
switch (button) {
case "A":
break;
case "B":
break;
case "X":
break;
case "Y":
break;
case "Start":
break;
case "Back":
break;
case "LT":
break;
case "RT":
break;
case "LB":
break;
case "RB":
break;
case "LS":
break;
case "RS":
break;
case "DPadUp":
break;
case "DPadDown":
break;
case "DPadLeft":
break;
case "DPadRight":
break;
}
};
const onAxisChange = (axisName: Axis, value: number, previousValue: number) => {
switch (axisName) {
case "RightStickX":
break;
case "RightStickY":
break;
case "LeftTrigger":
break;
case "RightTrigger":
break;
case "LeftStickX":
break;
case "LeftStickY":
break;
}
};

return (
<Gamepad
onConnect={onJoyConnection}
onDisconnect={onJoyDisconnection}
onButtonChange={onButtonChange}
onAxisChange={onAxisChange}
/>
);
}

```

## API

### Props
### useVibration

```ts
useVibration(gamepadIndex: number) ;
```

#### gamepadIndex

The index of the gamepad to use, from 0 to 4
**Default: 0**



```ts
vibrate: (intensity: number, duration?: number = 300) => void;
```

#### intensity

The force of the vibration, float number between 0 and 1

#### duration

Duration of the vibration in ms



### Gamepad

```js
<Gamepad
Expand Down Expand Up @@ -100,82 +154,101 @@ class App extends Component {
```

#### gamepadIndex

The index of the gamepad to use, from 0 to 4
**Default: 0**

#### stickThreshold

Threshold above which `onUp`, `onDown`,`onLeft`,`onRight` are triggered for the left stick
**Default: 0.75**

#### deadZone

Threshold below which the axis values will be rounded to `0.0`
**Default: 0.08**

#### layout

Layout to use, from `Gamepad.layouts`. For now, only `Gamepad.layouts.XBOX` exists
**Default: Gamepad.layouts.XBOX**

#### onConnect

`onConnect(gamepadIndex: Number)` triggered when the gamepad connects. Will be triggered at least once after the `Gamepad` component is mounted.

#### onDisconnect

`onDisconnect(gamepadIndex: Number)` triggered when the gamepad disconnects.

#### onButtonDown

`onButtonDown(buttonName: String)` triggered when a button is pushed. `buttonName` is one of `A`, `B`, `X`, `Y`, `Start`, `Back`, `LT`, `RT`, `LB`, `RB`, `LS`, `RS`, `DPadUp`, `DPadDown`, `DPadLeft`, `DPadRight`

#### onButtonUp

`onButtonUp(buttonName: String)` triggered when a button is released. `buttonName` is one of `A`, `B`, `X`, `Y`, `Start`, `Back`, `LT`, `RT`, `LB`, `RB`, `LS`, `RS`, `DPadUp`, `DPadDown`, `DPadLeft`, `DPadRight`

#### onButtonChange

`onButtonChange(buttonName: String, pressed: Bool)` triggered when a button is pushed or released. `buttonName` is one of `A`, `B`, `X`, `Y`, `Start`, `Back`, `LT`, `RT`, `LB`, `RB`, `LS`, `RS`, `DPadUp`, `DPadDown`, `DPadLeft`, `DPadRight`

#### onAxisChange

`onAxisChange(axisName: String, value: Number, previousValue: Number)` triggered when an axis is changed. `axisName` is one of `LeftStickX`, `LeftStickY`, `RightStickX`, `RightStickY`, `LeftTrigger`, `RightTrigger`

#### onA

`onA()` triggered when the `A` button is pressed.

#### onB

`onB()` triggered when the `B` button is pressed.

#### onX

`onX()` triggered when the `X` button is pressed.

#### onY

`onY()` triggered when the `Y` button is pressed.

#### onLT

`onLT()` triggered when the `LT` button is pressed.

#### onRT

`onRT()` triggered when the `RT` button is pressed.

#### onLB

`onLB()` triggered when the `LB` button is pressed.

#### onRB

`onRB()` triggered when the `RB` button is pressed.

#### onLS

`onLS()` triggered when the `LS` button is pressed.

#### onRS

`onRS()` triggered when the `RS` button is pressed.

#### onUp

`onUp()` triggered when the `D Pad Up` button is pressed or the Left Stick is pushed up (above `stickThreshold`).

#### onDown

`onDown()` triggered when the `D Pad Down` button is pressed or the Left Stick is pushed down (above `stickThreshold`).

#### onLeft

`onLeft()` triggered when the `D Pad Left` button is pressed or the Left Stick is pushed left (above `stickThreshold`).

#### onRight
`onRight()` triggered when the `D Pad Right` button is pressed or the Left Stick is pushed right (above `stickThreshold`).

## Support on Beerpay
Hey dude! Help me out for a couple of :beers:!

[![Beerpay](https://beerpay.io/SBRK/react-gamepad/badge.svg?style=beer-square)](https://beerpay.io/SBRK/react-gamepad) [![Beerpay](https://beerpay.io/SBRK/react-gamepad/make-wish.svg?style=flat-square)](https://beerpay.io/SBRK/react-gamepad?focus=wish)
`onRight()` triggered when the `D Pad Right` button is pressed or the Left Stick is pushed right (above `stickThreshold`).
Loading