Skip to content

EZ Web Audio / useInteractionMethods

Function: useInteractionMethods()

useInteractionMethods(key, player): Promise<() => void>

Defined in: packages/core/src/index.ts:1482

Attach play/stop handlers to an element for touch and mouse interactions.

Binds touchstart/mousedown to play() and touchend/mouseup/mouseleave to stop(). Automatically initializes audio on first interaction.

Parameters

key

HTMLElement

HTML element to attach handlers to

player

InteractionTarget

Object with play() and stop() methods

Returns

Promise<() => void>

Remarks

Like preventEventDefaults, this is a plain DOM convenience — not a framework hook — despite the use* prefix (R1#7). Framework-agnostic; safe to call from vanilla JS, React, Vue, etc.

Example

typescript
import { useInteractionMethods, createOscillator } from 'ez-web-audio'

const synth = await createOscillator({ frequency: 440 })
const pianoKey = document.getElementById('key-a4')

await useInteractionMethods(pianoKey, synth)
// Now touching/clicking the element plays the synth