From 99c33220b882fe077d89ffc6647699027e4c9618 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 13 Jul 2026 20:25:24 +0800 Subject: [PATCH] test --- keep-awake.js | 30 ++++++++++++----------------- send-move.ps1 | 41 ++++++++++++++++++++++++++++++++++++++++ test-move.ps1 | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ test-size.ps1 | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 send-move.ps1 create mode 100644 test-move.ps1 create mode 100644 test-size.ps1 diff --git a/keep-awake.js b/keep-awake.js index 2a1ef99..0360de3 100644 --- a/keep-awake.js +++ b/keep-awake.js @@ -1,37 +1,31 @@ const { execSync } = require('child_process'); +const path = require('path'); -const POWER_SHELL = 'powershell -NoProfile -Command'; +const SCRIPT = path.join(__dirname, 'send-move.ps1'); +const PS = `powershell -NoProfile -ExecutionPolicy Bypass -File "${SCRIPT}"`; -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(/(\d+)\s+(\d+)\s*$/m); - if (!match) throw new Error('Failed to get mouse position: ' + output); - 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 }); +function moveMouse(dx) { + execSync(`${PS} ${dx}`, { windowsHide: true }); } const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); async function main() { - console.log('已启动防息屏,每 15 秒移动鼠标 1 像素再移回原位。Ctrl+C 停止。\n'); + console.log('已启动防息屏,每 15 秒触发真实鼠标事件(SendInput)。Ctrl+C 停止。\n'); process.on('SIGINT', () => { console.log('\n已停止。'); process.exit(0); }); + let tick = 0; while (true) { - const orig = getMousePos(); - setMousePos(orig.x + 1, orig.y); - console.log(`→ 右移 1 像素 (${orig.x}, ${orig.y}) → (${orig.x + 1}, ${orig.y})`); + tick++; + moveMouse(1); + console.log(`【${tick}】→ 右移 1 像素 (SendInput)`); await sleep(14000); - setMousePos(orig.x, orig.y); - console.log(`← 移回原位 (${orig.x + 1}, ${orig.y}) → (${orig.x}, ${orig.y})`); + moveMouse(-1); + console.log(`【${tick}】← 移回原位 (SendInput)`); await sleep(1000); } } diff --git a/send-move.ps1 b/send-move.ps1 new file mode 100644 index 0000000..c0f4eec --- /dev/null +++ b/send-move.ps1 @@ -0,0 +1,41 @@ +param([int]$dx = 1) + +Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; + +public class MouseInput { + [StructLayout(LayoutKind.Sequential)] + public struct MOUSEINPUT { + public int dx; + public int dy; + public uint mouseData; + public uint dwFlags; + public uint time; + public IntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + public struct INPUT { + public uint type; + public MOUSEINPUT mi; + } + + [DllImport("user32.dll")] + public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); + + public const uint INPUT_MOUSE = 0; + public const uint MOUSEEVENTF_MOVE = 0x0001; +} +"@ + +$input = New-Object MouseInput+INPUT +$input.type = [MouseInput]::INPUT_MOUSE +$input.mi.dx = $dx +$input.mi.dy = 0 +$input.mi.dwFlags = [MouseInput]::MOUSEEVENTF_MOVE +$input.mi.time = 0 +$input.mi.dwExtraInfo = [IntPtr]::Zero + +$size = [Runtime.InteropServices.Marshal]::SizeOf($input) +[MouseInput]::SendInput(1, @($input), $size) | Out-Null diff --git a/test-move.ps1 b/test-move.ps1 new file mode 100644 index 0000000..18eb844 --- /dev/null +++ b/test-move.ps1 @@ -0,0 +1,52 @@ +Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; + +public class MouseInput { + [StructLayout(LayoutKind.Sequential)] + public struct MOUSEINPUT { + public int dx; + public int dy; + public uint mouseData; + public uint dwFlags; + public uint time; + public IntPtr dwExtraInfo; + } + + [StructLayout(LayoutKind.Sequential)] + public struct INPUT { + public uint type; + public MOUSEINPUT mi; + } + + [DllImport("user32.dll")] + public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); + + public const uint INPUT_MOUSE = 0; + public const uint MOUSEEVENTF_MOVE = 0x0001; +} +"@ + +Add-Type -AssemblyName System.Windows.Forms + +$pos = [System.Windows.Forms.Cursor]::Position +Write-Host "当前位置: X=$($pos.X), Y=$($pos.Y)" + +$input = New-Object MouseInput+INPUT +$input.type = [MouseInput]::INPUT_MOUSE +$input.mi.dx = 1 +$input.mi.dy = 0 +$input.mi.dwFlags = [MouseInput]::MOUSEEVENTF_MOVE +$input.mi.time = 0 +$input.mi.dwExtraInfo = [IntPtr]::Zero + +$size = [Runtime.InteropServices.Marshal]::SizeOf($input) +$result = [MouseInput]::SendInput(1, @($input), $size) +Write-Host "SendInput 返回: $result (右移1像素)" + +Start-Sleep -Seconds 2 + +$input.mi.dx = -1 +$result = [MouseInput]::SendInput(1, @($input), $size) +Write-Host "SendInput 返回: $result (移回原位)" +Write-Host "测试完成" diff --git a/test-size.ps1 b/test-size.ps1 new file mode 100644 index 0000000..4783684 --- /dev/null +++ b/test-size.ps1 @@ -0,0 +1,40 @@ +Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; + +public class MI { + [StructLayout(LayoutKind.Sequential)] + public struct MOUSEINPUT { + public int dx; + public int dy; + public uint mouseData; + public uint dwFlags; + public uint time; + public IntPtr dwExtraInfo; + } + [StructLayout(LayoutKind.Sequential)] + public struct INPUT { + public uint type; + public MOUSEINPUT mi; + } +} +"@ + +$type = [MI+INPUT] +Write-Host "Type: $($type.FullName)" +Write-Host "IsValueType: $($type.IsValueType)" + +try { + $s = [Runtime.InteropServices.Marshal]::SizeOf($type) + Write-Host "SizeOf(type): $s" +} catch { + Write-Host "SizeOf(type) failed: $($_.Exception.Message)" +} + +$inst = [Activator]::CreateInstance($type) +try { + $s2 = [Runtime.InteropServices.Marshal]::SizeOf($inst) + Write-Host "SizeOf(inst): $s2" +} catch { + Write-Host "SizeOf(inst) failed: $($_.Exception.Message)" +}