electron锁定窗口需要设置一个属性:kiosk属性设为true
例子如下:
const {app,BrowserWindow} = require('electron');
function createWindow(){
win = new BrowserWindow({
kiosk:true,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});
function createWindow(){
win = new BrowserWindow({
kiosk:true,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});
也可以使用setKiosk()方法来动态设置锁定模式,isKiosk()方法可以判断当前是否处于锁定模式
例子如下:
点击某个按钮,进入到锁定模式
html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>hello world</title>
<script src="./event.js"></script>
</head>
<body>
<button onclick="onClick()" id="button">进入锁定模式</button>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>hello world</title>
<script src="./event.js"></script>
</head>
<body>
<button onclick="onClick()" id="button">进入锁定模式</button>
</body>
</html>
event.js代码:
const remote = require('electron').remote;
function onClick(){
const button = document.getElementById('button');
const win = remote.getCurrentWindow();
if(win.isKiosk()){
//解除锁定模式
win.setKiosk(false);
button.innerText = "进入锁定模式";
}else{
//进入锁定模式
win.setKiosk(true);
button.innerText = "退出锁定模式";
}
}
function onClick(){
const button = document.getElementById('button');
const win = remote.getCurrentWindow();
if(win.isKiosk()){
//解除锁定模式
win.setKiosk(false);
button.innerText = "进入锁定模式";
}else{
//进入锁定模式
win.setKiosk(true);
button.innerText = "退出锁定模式";
}
}
需要注意的是:在mac系统下,设置窗口设为锁定模式后,只能通过command+q来退出