REF / Music API / HID

Ascene . Agraph . Aengine
Anode . Modulator . Envelope
Songbook . Song . TimeKeeper
Voices . SampleMgr . StreamMgr
Loader . Async . Plot
Random . Rhythm . Util
MIDI . OSC . HID . Piano
Music Theory with Tonal

right-click to navigate to page sections


HID stands for Human Interface Device and is used by The USB Standard to refer to a wide variety of USB-based input devices you can connect to your computer.

We currently support mouse and keyboard events and HID allows you to subscribe to them in your programs.

It's easy to include human gestures from keyboard and mouse in your songs. NB: these events are only available to your script when your mouse is over its active sandbox window. Keyboard events are only available when the sandbox window has focus (ie: you've clicked on its contents).

HID API

The HID API is very simple. Instantiate a HID object and add one or more event subscribers. Your callback function(s) will be called on each event with a simple object describing the event.

In order for these events to trigger, the keyboard focus must be set on the sandbox window associated with your program. Similarly only mouse events that occur within this window trigger activity.

Method Description
new Hid() constructor
Subscribe(callback) Adds a callback function to the HID subscribers list. Returns a subscription id that can be used to cancel it.
Unsubscribe(subid) Cancels the subscription created by Subscribe

HID Callback Data

All event objects contain the field, event, whose value is one of the following.

Event Fields
KeyDown key, metakeys
KeyUp key, metakeys
MouseDown mouseX, mouseY, mouseB1, metakeys
MouseUp mouseX, mouseY, mouseB1, metakeys
MouseMove mouseX, mouseY, mouseB1, metakeys

Example

let hid = new HID();

hid.Subscribe((event) =>
{
    console.log(JSON.stringify(event));
});

Here's some typical output:

16:43 note {"event":"MouseMove","mouseX":0.6743772241992882,"mouseY":0.8314176245210728,"mouseB1":0,"metakeys":[]}
16:43 note {"event":"MouseDown","mouseX":0.6120996441281139,"mouseY":0.8199233716475096,"mouseB1":1,"metakeys":[]}
16:43 note {"event":"MouseMove","mouseX":0.6120996441281139,"mouseY":0.8199233716475096,"mouseB1":1,"metakeys":[]}
16:43 note {"event":"MouseUp","mouseX":0.6120996441281139,"mouseY":0.8199233716475096,"mouseB1":0,"metakeys":[]}
16:43 note {"event":"MouseDown","mouseX":0.6120996441281139,"mouseY":0.8199233716475096,"mouseB1":1,"metakeys":[]}
16:43 note {"event":"MouseUp","mouseX":0.6120996441281139,"mouseY":0.8199233716475096,"mouseB1":0,"metakeys":[]}
16:43 note {"event":"MouseMove","mouseX":0.6156583629893239,"mouseY":0.8199233716475096,"mouseB1":0,"metakeys":[]}
16:43 note {"event":"MouseMove","mouseX":0.6245551601423488,"mouseY":0.8122605363984674,"mouseB1":0,"metakeys":[]}
16:43 note {"event":"MouseMove","mouseX":0.6797153024911032,"mouseY":0.789272030651341,"mouseB1":0,"metakeys":[]}
16:45 note {"event":"KeyDown","key":"S","metakeys":["Shift"]}
16:45 note {"event":"KeyUp","key":"S","metakeys":["Shift"]}
16:45 note {"event":"KeyDown","key":"V","metakeys":["Shift"]}
16:45 note {"event":"KeyUp","key":"V","metakeys":["Shift"]}
16:45 note {"event":"KeyUp","key":"Shift","metakeys":[]}
16:45 note {"event":"KeyDown","key":"F1","metakeys":[]}
16:45 note {"event":"KeyUp","key":"F1","metakeys":[]}
16:45 note {"event":"KeyDown","key":"h","metakeys":[]}
16:45 note {"event":"KeyUp","key":"h","metakeys":[]}
16:45 note {"event":"KeyDown","key":"s","metakeys":[]}
16:45 note {"event":"KeyUp","key":"s","metakeys":[]}
16:45 note {"event":"MouseMove","mouseX":0.44661921708185054,"mouseY":0.2950191570881226,"mouseB1":0,"metakeys":[]}
16:45 note {"event":"MouseMove","mouseX":0.44661921708185054,"mouseY":0.29118773946360155,"mouseB1":0,"metakeys":[]}
16:45 note {"event":"MouseMove","mouseX":0.44661921708185054,"mouseY":0.2835249042145594,"mouseB1":0,"metakeys":[]}
16:45 note {"event":"MouseMove","mouseX":0.44661921708185054,"mouseY":0.2720306513409962,"mouseB1":0,"metakeys":[]}
16:45 note {"event":"MouseMove","mouseX":0.44661921708185054,"mouseY":0.2681992337164751,"mouseB1":0,"metakeys":[]}
1

See Also

HID Example . Piano Ref

home .. topics .. interface .. reference .. examples .. tipjar