geely-kaipiao/keep-awake.js
2026-07-13 19:55:50 +08:00

44 lines
1.5 KiB
JavaScript

const { execSync } = require('child_process');
const POWER_SHELL = 'powershell -NoProfile -Command';
function getMousePos() {
const cmd = `${POWER_SHELL} "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Cursor]::Position"`;
const output = execSync(cmd, { windowsHide: true }).toString();
const match = output.match(/X=(\d+).*Y=(\d+)/);
if (!match) throw new Error('Failed to get mouse position');
return { x: parseInt(match[1], 10), y: parseInt(match[2], 10) };
}
function setMousePos(x, y) {
const cmd = `${POWER_SHELL} "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(${x}, ${y})"`;
execSync(cmd, { windowsHide: true });
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function main() {
console.log('已启动防息屏,每 15 秒移动鼠标 1 像素再移回原位。Ctrl+C 停止。\n');
let moving = false;
process.on('SIGINT', () => {
console.log('\n已停止。');
process.exit(0);
});
while (true) {
const orig = getMousePos();
setMousePos(orig.x + 1, orig.y);
console.log(`→ 右移 1 像素 (${orig.x}, ${orig.y}) → (${orig.x + 1}, ${orig.y})`);
await sleep(14000);
setMousePos(orig.x, orig.y);
console.log(`← 移回原位 (${orig.x + 1}, ${orig.y}) → (${orig.x}, ${orig.y})`);
await sleep(1000);
}
}
main().catch((err) => {
console.error('出错:', err.message);
process.exit(1);
});