使用electron自带的 globalShortcut 监听快捷键,监听函数不做处理即可禁用快捷键
此方法无法禁用win键(即Super键),若需禁用win键 可使用 xmodmap 禁用(全局无效,非当前项目无效)
main.js
javascript
const { app, BrowserWindow, ipcMain, globalShortcut } = require('electron')
const path = require('path')
// 需要无效化的键位
const keysDisabled = ['alt+f4', 'f11', 'ctrl+shift+i']
// 加载地址
const loadUrl =
process.env.MODE === 'devlopment'
? 'http://localhost:3000/'
: `file://${path.join(__dirname, '../dist/index.html')}`
function createWindow() {
const win = new BrowserWindow({
// width: 800,
// height: 600,
frame: false, // 隐藏窗口边框和标题栏
fullscreen: true, // 设置全屏
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, './preload.js'),
nodeIntegration: false,
}
})
win.loadURL(loadUrl)
keysDisabled.map((key) => {
globalShortcut.register(key, () => {
console.log(key)
})
})
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})