MIDIAccess: statechange event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The statechange event of the MIDIAccess interface is fired when a new MIDI port is added or when an existing port changes state.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("statechange", (event) => {});

onstatechange = (event) => {};

Event type

Event properties

MIDIConnectionEvent.port Read only

Returns a reference to a MIDIPort instance for a port that has been connected or disconnected.

Example

The Navigator.requestMIDIAccess() method returns a promise that resolves with a MIDIAccess object. When a port changes state, information about that port is printed to the console.

js
navigator.requestMIDIAccess().then((access) => {
  access.onstatechange = (event) => {
    console.log(event.port.name, event.port.manufacturer, event.port.state);
  };
});

Specifications

Specification
Web MIDI API
# dom-midiaccess-onstatechange

Browser compatibility

BCD tables only load in the browser