130 lines
3.4 KiB
JavaScript
130 lines
3.4 KiB
JavaScript
// main.js
|
||
const { app, BrowserWindow, globalShortcut, screen, ipcMain } = require('electron'); // 添加 ipcMain
|
||
const path = require('path');
|
||
|
||
let mainWindow;
|
||
let allowQuit = false; // 添加一个标志来控制是否允许退出
|
||
|
||
function createWindow () {
|
||
// 获取主显示器尺寸
|
||
const primaryDisplay = screen.getPrimaryDisplay();
|
||
const { width, height } = primaryDisplay.size;
|
||
|
||
mainWindow = new BrowserWindow({
|
||
// --- 关键修改:设置为全屏无框 ---
|
||
width: width,
|
||
height: height,
|
||
x: 0,
|
||
y: 0,
|
||
fullscreen: true,
|
||
kiosk: true, // 关键:kiosk 模式,隐藏菜单栏、Dock
|
||
alwaysOnTop: true,
|
||
resizable: false,
|
||
movable: false,
|
||
fullscreenable: false,
|
||
frame: false, // 隐藏窗口框架和标题栏
|
||
titleBarStyle: 'hidden', // 隐藏标题栏
|
||
trafficLightPosition: { x: -100, y: -100 }, // 将红绿灯按钮移出窗口可见区域
|
||
backgroundColor: '#ffffff', // 设置背景为纯白色
|
||
hasShadow: false, // 移除窗口阴影
|
||
webPreferences: {
|
||
nodeIntegration: false,
|
||
contextIsolation: true,
|
||
enableRemoteModule: false,
|
||
preload: path.join(__dirname, 'preload.js')
|
||
}
|
||
});
|
||
|
||
mainWindow.loadFile('student_app_fullscreen.html');
|
||
|
||
// 禁用窗口关闭按钮
|
||
mainWindow.on('close', (event) => {
|
||
if (!allowQuit) {
|
||
event.preventDefault();
|
||
}
|
||
});
|
||
|
||
// 启动时就注册全局快捷键拦截
|
||
registerGlobalShortcuts();
|
||
|
||
// 添加退出应用的IPC处理程序
|
||
ipcMain.handle('exit-app', async () => {
|
||
allowQuit = true;
|
||
app.quit();
|
||
});
|
||
}
|
||
|
||
// 注册全局快捷键拦截
|
||
function registerGlobalShortcuts() {
|
||
// 移除 Esc 退出功能
|
||
|
||
// 尝试注册其他快捷键(在无辅助功能权限时可能无效,但无害)
|
||
const shortcuts = [
|
||
'CommandOrControl+Tab',
|
||
'CommandOrControl+Shift+Tab',
|
||
'Command+Q',
|
||
'Command+W',
|
||
'Command+R',
|
||
'F5',
|
||
'F11',
|
||
'F12',
|
||
'Alt+Tab',
|
||
'Alt+F4',
|
||
'Control+Escape',
|
||
'CommandOrControl+W',
|
||
'CommandOrControl+R',
|
||
'Control+R',
|
||
'Alt+Space', // Windows打开菜单
|
||
'Command+Space', // Mac打开Spotlight
|
||
'Command+M', // Mac最小化
|
||
'Command+H', // Mac隐藏
|
||
'Command+Option+H', // Mac隐藏其他
|
||
'Command+Option+M', // Mac最小化所有
|
||
'Command+Option+Escape', // Mac强制退出
|
||
'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', // 功能键
|
||
'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', 'F21', 'F22', 'F23', 'F24' // 功能键
|
||
];
|
||
|
||
shortcuts.forEach(key => {
|
||
try {
|
||
globalShortcut.register(key, () => {
|
||
// 吃掉事件,什么都不做
|
||
console.log(`⚠️ 阻止了快捷键:`, key);
|
||
});
|
||
} catch (e) {
|
||
console.warn('Failed to register:', key);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 注销全局快捷键拦截
|
||
function unregisterGlobalShortcuts() {
|
||
globalShortcut.unregisterAll();
|
||
}
|
||
|
||
app.whenReady().then(() => {
|
||
createWindow();
|
||
|
||
app.on('activate', function () {
|
||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||
});
|
||
});
|
||
|
||
app.on('window-all-closed', function () {
|
||
// 注销所有快捷键
|
||
unregisterGlobalShortcuts();
|
||
if (process.platform !== 'darwin') app.quit();
|
||
});
|
||
|
||
// 防止应用通过常规方式退出
|
||
app.on('before-quit', (event) => {
|
||
// 只有通过退出按钮才能真正退出
|
||
if (!allowQuit) {
|
||
event.preventDefault();
|
||
}
|
||
});
|
||
|
||
app.on('will-quit', (event) => {
|
||
// 清理工作
|
||
unregisterGlobalShortcuts();
|
||
}); |