Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useGamepad.md
1---2category: Browser3---45# useGamepad67Provides reactive bindings for the [Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API).89## Usage1011> Due to how the Gamepad API works, you must interact with the page using the gamepad before it will be detected.1213```vue14<script setup lang="ts">15import { useGamepad } from '@vueuse/core'16import { computed } from 'vue'1718const { isSupported, gamepads } = useGamepad()19const gamepad = computed(() => gamepads.value.find(g => g.mapping === 'standard'))20</script>2122<template>23<span>24{{ gamepad.id }}25</span>26</template>27```2829### Gamepad Updates3031Currently the Gamepad API does not have event support to update the state of the gamepad. To update the gamepad state, `requestAnimationFrame` is used to poll for gamepad changes. You can control this polling by using the `pause` and `resume` functions provided by `useGamepad`3233```ts34import { useGamepad } from '@vueuse/core'3536const { pause, resume, gamepads } = useGamepad()3738pause()3940// gamepads object will not update4142resume()4344// gamepads object will update on user input45```4647### Gamepad Connect & Disconnect Events4849The `onConnected` and `onDisconnected` events will trigger when a gamepad is connected or disconnected.5051```ts52import { useGamepad } from '@vueuse/core'53// ---cut---54const { gamepads, onConnected, onDisconnected } = useGamepad()5556onConnected((index) => {57console.log(`${gamepads.value[index].id} connected`)58})5960onDisconnected((index) => {61console.log(`${index} disconnected`)62})63```6465### Vibration6667> The Gamepad Haptics API is sparse, so check the [compatibility table](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator#browser_compatibility) before using.6869<!-- eslint-disable import/first -->7071```ts72import { useGamepad } from '@vueuse/core'7374const { gamepads, onConnected, onDisconnected } = useGamepad()75const gamepad = gamepads.value[0]!76// ---cut---77import { computed } from 'vue'7879const supportsVibration = computed(() => gamepad.hapticActuators.length > 0)80function vibrate() {81if (supportsVibration.value) {82const actuator = gamepad.hapticActuators[0]83actuator.playEffect('dual-rumble', {84startDelay: 0,85duration: 1000,86weakMagnitude: 1,87strongMagnitude: 1,88})89}90}91```9293### Mappings9495To make the Gamepad API easier to use, we provide mappings to map a controller to a controllers button layout.9697#### Xbox360 Controller9899```vue100<script setup>101import { mapGamepadToXbox360Controller } from '@vueuse/core'102103const controller = mapGamepadToXbox360Controller(gamepad)104</script>105106<template>107<span>{{ controller.buttons.a.pressed }}</span>108<span>{{ controller.buttons.b.pressed }}</span>109<span>{{ controller.buttons.x.pressed }}</span>110<span>{{ controller.buttons.y.pressed }}</span>111</template>112```113114Currently there are only mappings for the Xbox 360 controller. If you have controller you want to add mappings for, feel free to open a PR for more controller mappings!115116### SSR Compatibility117118This component is designed to be used in the client side. In some cases, SSR might cause some hydration mismatches.119120If you are using Nuxt, you can simply rename your component file with the `.client.vue` suffix (e.g., `GamepadComponent.client.vue`) which will automatically make it render only on the client side, avoiding hydration mismatches.121122In other frameworks or plain Vue, you can wrap your usage component with a `<ClientOnly>` component to ensure it is only rendered on the client side.123124## Type Declarations125126```ts127export interface UseGamepadOptions128extends ConfigurableWindow, ConfigurableNavigator {}129export interface UseGamepadReturn extends Supportable, Pausable {130onConnected: EventHookOn<number>131onDisconnected: EventHookOn<number>132gamepads: Ref<Gamepad[]>133}134/**135* Maps a standard standard gamepad to an Xbox 360 Controller.136*/137export declare function mapGamepadToXbox360Controller(138gamepad: Ref<Gamepad | undefined>,139): ComputedRef<{140buttons: {141a: GamepadButton142b: GamepadButton143x: GamepadButton144y: GamepadButton145}146bumper: {147left: GamepadButton148right: GamepadButton149}150triggers: {151left: GamepadButton152right: GamepadButton153}154stick: {155left: {156horizontal: number157vertical: number158button: GamepadButton159}160right: {161horizontal: number162vertical: number163button: GamepadButton164}165}166dpad: {167up: GamepadButton168down: GamepadButton169left: GamepadButton170right: GamepadButton171}172back: GamepadButton173start: GamepadButton174} | null>175export declare function useGamepad(176options?: UseGamepadOptions,177): UseGamepadReturn178```179