5.4.0

Keyboard Controls

WASD on QWERTY keyboards or equivalent keys on non-QWERTY keyboards

<KeyboardControls /> is a simple keyboard controller for the camera. The camera's movements are bound to:

  • WASD on QWERTY keyboards or equivalent keys on non-QWERTY keyboards
  • Arrow keys
KeyboardControls uses PointerLockControls under the hood. You can use PointerLockControls props and events.

Usage

<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { KeyboardControls, Box } from '@tresjs/cientos'
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera />
    <Box :position-y="0.5" />
    <KeyboardControls />
    <TresGridHelper />
  </TresCanvas>
</template>
Is really important that the Perspective camera is set first in the canvas. Otherwise might break.

Props

PropDescriptionDefault
moveSpeedSpeed movement.0.2
makeDefaultIf true, the controls will be set as the default controls for the scene.true
cameraThe camera to control.undefined
domElementThe DOM element to listen to.undefined
selectorAccept an id element as string. If set, the new element will be used as the triggerundefined

Events

<KeyboardControls @change="onChange" @is-lock="(state) => isActive(state)" />
EventDescription
isLockReturn true if "lock", false if "unlock" events are triggered.
changeDispatched when the control changes.