files/en-us/web/api/midiaccess/statechange_event/index.md
{{securecontext_header}}{{APIRef("Web MIDI API")}}
The statechange event of the {{domxref("MIDIAccess")}} interface is fired when a new MIDI port is added or when an existing port changes state.
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
addEventListener("statechange", (event) => { })
onstatechange = (event) => { }
A {{domxref("MIDIConnectionEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("MIDIConnectionEvent")}}
The {{domxref("Navigator.requestMIDIAccess()")}} method returns a promise that resolves with a {{domxref("MIDIAccess")}} object. When a port changes state, information about that port is printed to the console.
navigator.requestMIDIAccess().then((access) => {
access.onstatechange = (event) => {
console.log(event.port.name, event.port.manufacturer, event.port.state);
};
});
{{Specifications}}
{{Compat}}