A playable keyboard
Updated
The keys A to K hold notes from C4 to C5 on an Audio Tone: attack on key down and release on key up, like a piano.
Click the scene, then hold the keys A, S, D, F, G, H, J and K to play from C4 to C5.
A playable keyboard
#pragma lifecycle(startup, update)
// Scene: an Audio Tone entity "Piano".const KEYS = [ [Key.A, 'C4'], [Key.S, 'D4'], [Key.D, 'E4'], [Key.F, 'F4'], [Key.G, 'G4'], [Key.H, 'A4'], [Key.J, 'B4'], [Key.K, 'C5'],];let piano;const held = new Set();
function startup() { piano = findEntity('Piano').getComponent(AudioToneComponent); piano.setProps({ oscillator: 'triangle', attack: 0.01, decay: 0.4, sustain: 0.5, release: 0.8, voices: 8 });}
function update() { for (const [key, note] of KEYS) { const down = Input.keyboard.isKeyPressed(key); if (down && !held.has(note)) { held.add(note); piano.attack(note); } else if (!down && held.has(note)) { held.delete(note); piano.release(note); } }}How it works
Section titled “How it works”The script is explained step by step in A playable keyboard.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
KeyboardPiano.xrproj