test
This commit is contained in:
parent
7081eae1a3
commit
99c33220b8
@ -1,37 +1,31 @@
|
|||||||
const { execSync } = require('child_process');
|
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() {
|
function moveMouse(dx) {
|
||||||
const cmd = `${POWER_SHELL} "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Cursor]::Position"`;
|
execSync(`${PS} ${dx}`, { windowsHide: true });
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('已启动防息屏,每 15 秒移动鼠标 1 像素再移回原位。Ctrl+C 停止。\n');
|
console.log('已启动防息屏,每 15 秒触发真实鼠标事件(SendInput)。Ctrl+C 停止。\n');
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
console.log('\n已停止。');
|
console.log('\n已停止。');
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let tick = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
const orig = getMousePos();
|
tick++;
|
||||||
setMousePos(orig.x + 1, orig.y);
|
moveMouse(1);
|
||||||
console.log(`→ 右移 1 像素 (${orig.x}, ${orig.y}) → (${orig.x + 1}, ${orig.y})`);
|
console.log(`【${tick}】→ 右移 1 像素 (SendInput)`);
|
||||||
await sleep(14000);
|
await sleep(14000);
|
||||||
setMousePos(orig.x, orig.y);
|
moveMouse(-1);
|
||||||
console.log(`← 移回原位 (${orig.x + 1}, ${orig.y}) → (${orig.x}, ${orig.y})`);
|
console.log(`【${tick}】← 移回原位 (SendInput)`);
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
send-move.ps1
Normal file
41
send-move.ps1
Normal file
@ -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
|
||||||
52
test-move.ps1
Normal file
52
test-move.ps1
Normal file
@ -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 "测试完成"
|
||||||
40
test-size.ps1
Normal file
40
test-size.ps1
Normal file
@ -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)"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user