(node:13114) UnhandledPromiseRejectionWarning: TypeError: Error processing argument at…
- User
(node:13114) UnhandledPromiseRejectionWarning: TypeError: Error processing argument at index 0, conversion failure from Command+Alt at /Users/sergeilogachev/PycharmProjects/gpt/my-chat-app/main.js:34:30
const { app, BrowserWindow, globalShortcut } = require('electron'); const path = require('path');
let mainWindow;
function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, show: false, // Начинаем с скрытого окна webPreferences: { nodeIntegration: true, // Будьте осторожны с этой опцией в продакшен-приложениях contextIsolation: false, }, });
mainWindow.loadFile('index.html');
// Обработчик события закрытия окна mainWindow.on('close', (event) => { event.preventDefault(); mainWindow.hide(); });
mainWindow.on('closed', () => { mainWindow = null; }); }
app.whenReady().then(() => { createWindow();
// Регистрация глобального сочетания клавиш Option + Command const ret = globalShortcut.register('Command+Alt', () => { if (mainWindow.isVisible()) { mainWindow.hide(); } else { mainWindow.show(); mainWindow.focus(); } });
if (!ret) { console.log('Не удалось зарегистрировать глобальное сочетание клавиш'); }
app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow(); }); });
app.on('will-quit', () => { // Отмена регистрации всех сочетаний клавиш globalShortcut.unregisterAll(); });
app.on('window-all-closed', function () { // На macOS приложения обычно остаются открытыми до явного выхода if (process.platform !== 'darwin') app.quit(); });
- Assistant
The error you're encountering is due to the way the keyboard shortcut (accelerator) is specified in the
globalShortcut.registermethod.In Electron, when registering a global shortcut, the accelerator string must include at least one key code (such as a letter, number, or function key) in addition to any modifiers (like
Command,Control,Alt,Shift).In your code, you're using
'Command+Alt'as the accelerator. This includes two modifiers (CommandandAlt) but doesn't include a key code. This is causing the error:TypeError: Error processing argument at index 0, conversion failure from Command+AltTo fix this error, you need to include a key code in the accelerator. For example, if you want to register the shortcut
Command+Alt+S, you would modify your code as follows:// Регистрация глобального сочетания клавиш Command + Alt + S const ret = globalShortcut.register('Command+Alt+S', () => { if (mainWindow.isVisible()) { mainWindow.hide(); } else { mainWindow.show(); mainWindow.focus(); } });Here's the corrected portion of your code:
app.whenReady().then(() => { createWindow(); // Регистрация глобального сочетания клавиш Command + Alt + S const ret = globalShortcut.register('Command+Alt+S', () => { if (mainWindow.isVisible()) { mainWindow.hide(); } else { mainWindow.show(); mainWindow.focus(); } }); if (!ret) { console.log('Не удалось зарегистрировать глобальное сочетание клавиш'); } app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow(); }); });Explanation:
- Include a Key Code: The accelerator must include at least one non-modifier key. Modifiers like
CommandandAltspecify which modifier keys to use, but you also need to specify which key is being pressed along with these modifiers. - Valid Accelerator Format: The accelerator should follow the format
Modifier+Key, whereKeyis a key code likeA,S,F1,Space, etc.
Additional Notes:
- Avoid System Reserved Shortcuts: Be cautious when choosing the key combination. Some shortcuts may be reserved by the operating system or other applications, which can prevent your application from receiving the shortcut event.
- Check Accelerator Syntax: Refer to the Electron Accelerator Documentation for the correct syntax and available key codes.
Example of Valid Accelerators:
'CommandOrControl+X''Alt+F4''Shift+Command+Right''Control+Alt+Delete'
By updating your accelerator to include a key code, the
globalShortcut.registermethod should work without throwing an error. - Include a Key Code: The accelerator must include at least one non-modifier key. Modifiers like