Skip to content

Commit

Permalink
global hotkey mute
Browse files Browse the repository at this point in the history
  • Loading branch information
steveseguin committed Feb 8, 2021
1 parent 350332b commit 80476e1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
43 changes: 39 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ process.on('uncaughtException', function (error) {
error.log(error);
});

const {app, BrowserWindow, ipcMain, screen, shell} = require('electron')
const {app, BrowserWindow, ipcMain, screen, shell, globalShortcut , session, desktopCapturer} = require('electron')
const path = require('path')
const contextMenu = require('electron-context-menu');

Expand Down Expand Up @@ -75,6 +75,22 @@ function createWindow (URL=url) {
counter+=1;
currentTitle = title + " " +(counter.toString());
}

const ret = globalShortcut.register('CommandOrControl+M', () => {
console.log('CommandOrControl+N is pressed')
if (mainWindow) {
mainWindow.webContents.send('postMessage', {'mic':'toggle'})
}
})

ipcMain.on('postMessage', () => {
console.log('We received a postMessage from the preload script')
})

if (!ret) {
console.log('registration failed')
}


let factor = screen.getPrimaryDisplay().scaleFactor;

Expand All @@ -87,16 +103,18 @@ function createWindow (URL=url) {
fullscreenable: true,
titleBarStyle: 'customButtonsOnHover',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
// zoomFactor: 1.0 / factor,
nodeIntegration: true
preload: path.join(__dirname, 'preload.js'),
// zoomFactor: 1.0 / factor,
nodeIntegration: true // this could be a security hazard, but useful for enabling screen sharing and global hotkeys
},
title: currentTitle
});

mainWindow.on('close', function(e) {
e.preventDefault();
mainWindow.destroy();
globalShortcut.unregister('CommandOrControl+M');
globalShortcut.unregisterAll();
//app.quit();
});

Expand Down Expand Up @@ -126,11 +144,28 @@ function createWindow (URL=url) {
// Windows?
}


session.fromPartition("default").setPermissionRequestHandler((webContents, permission, callback) => {
let allowedPermissions = ["audioCapture", "desktopCapture", "pageCapture", "tabCapture", "experimental"]; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest

if (allowedPermissions.includes(permission)) {
callback(true); // Approve permission request
} else {
console.error(
`The application tried to request permission for '${permission}'. This permission was not whitelisted and has been blocked.`
);

callback(false); // Deny
}
});

try {
mainWindow.loadURL(URL);
} catch (e){
app.quit();
}


}

// This method will be called when Electron has finished
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "OBSN.Electron.Capture.App",
"version": "1.2.0",
"version": "1.3.0",
"description": "Simple tool to allow for playing frameless videos for screen capture",
"author": "Steve Seguin",
"main": "main.js",
Expand Down Expand Up @@ -103,7 +103,7 @@
"run-script-os-fix": "^1.0.4"
},
"dependencies": {
"electron-context-menu": "^2.3.0",
"electron-context-menu": "^2.4.0",
"electron-is-dev": "^1.0.1",
"yargs": "^15.4.1"
}
Expand Down
28 changes: 26 additions & 2 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const { ipcRenderer } = require('electron')

window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
Expand All @@ -10,3 +10,27 @@ window.addEventListener('DOMContentLoaded', () => {
replaceText(`${type}-version`, process.versions[type])
}
})



window.addEventListener('message', ({ data }) => {
ipcRenderer.send('postMessage', data)
})

ipcRenderer.on('postMessage', (event, ...args) => {
try{
if ("mic" in args[0]) { // this should work for the director's mic mute button as well. Needs to be manually enabled the first time still tho.
if (args[0].mic === true) { // unmute
session.muted = false; // set
toggleMute(true); // apply
} else if (args[0].mic === false) { // mute
session.muted = true; // set
toggleMute(true); // apply
} else if (args[0].mic === "toggle") { // toggle
toggleMute();
}
}
}catch(e){
console.error(e);
}
})

0 comments on commit 80476e1

Please sign in to comment.