Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I request MIDI device permission on Electron?

I have a MIDI device (launchpad) that I want to use with an Electron application. I was able to read events on the MIDI device using Microsoft Edge Chromium using Web MIDI API (https://github.com/djipco/webmidi). However, in order to start reading the MIDI device, permissions needed to be set, and displayed like so:

Permission

When I run npm start to start the electron project, I cannot find a way to enable permissions for MIDI devices. I cannot see any of my MIDI devices, and I am assuming that is because I am missing permissions.

I have researched the Electron documentation, and was not able to find anything. systemPreferences provides a way to request permissions, but for camera and microphone, not MIDI devices (https://electronjs.org/docs/api/system-preferences).

Thank you very much, and any help is appreciated :)

like image 728
Francisco F. Avatar asked Oct 16 '25 14:10

Francisco F.


2 Answers

we can handle this using ses.setPermissionRequestHandler.

like image 59
Matcha Sesha Abhishek Avatar answered Oct 19 '25 05:10

Matcha Sesha Abhishek


You have to implement two handlers in your main process for the window where you're enabling WebMidi:

  mainWindow.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => {
    console.log('Permission request:', permission);

    if (permission === 'midi' || permission === 'midiSysex') {
      callback(true);
    } else {
      callback(false);
    }
  })

  mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin) => {
    console.log('Permission check:', permission);

    if (permission === 'midi' || permission === 'midiSysex') {
      return true;
    }

    return false;
  });

Then you can enable WebMidi like:

WebMidi.enable({ sysex: true });
like image 40
Beau Avatar answered Oct 19 '25 04:10

Beau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!