63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
|
|
const koffi = require('koffi');
|
||
|
|
const user32 = koffi.load('user32.dll');
|
||
|
|
|
||
|
|
const SetCursorPos = user32.func('bool SetCursorPos(int x, int y)');
|
||
|
|
const mouse_event = user32.func('void mouse_event(uint32 dwFlags, uint32 dx, uint32 dy, uint32 dwData, uintptr dwExtraInfo)');
|
||
|
|
const keybd_event = user32.func('void keybd_event(uint8 bVk, uint8 bScan, uint32 dwFlags, uintptr dwExtraInfo)');
|
||
|
|
const GetSystemMetrics = user32.func('int GetSystemMetrics(int nIndex)');
|
||
|
|
|
||
|
|
const MOUSEEVENTF_LEFTDOWN = 0x0002;
|
||
|
|
const MOUSEEVENTF_LEFTUP = 0x0004;
|
||
|
|
const MOUSEEVENTF_RIGHTDOWN = 0x0008;
|
||
|
|
const MOUSEEVENTF_RIGHTUP = 0x0010;
|
||
|
|
const MOUSEEVENTF_MIDDLEDOWN = 0x0020;
|
||
|
|
const MOUSEEVENTF_MIDDLEUP = 0x0040;
|
||
|
|
const MOUSEEVENTF_WHEEL = 0x0800;
|
||
|
|
const KEYEVENTF_KEYUP = 0x0002;
|
||
|
|
|
||
|
|
const VK = {
|
||
|
|
backspace:0x08, tab:0x09, enter:0x0D, shift:0x10, control:0x11,
|
||
|
|
alt:0x12, escape:0x1B, space:0x20, pageup:0x21, pagedown:0x22,
|
||
|
|
end:0x23, home:0x24, left:0x25, up:0x26, right:0x27, down:0x28,
|
||
|
|
delete:0x2E, insert:0x2D,
|
||
|
|
f1:0x70,f2:0x71,f3:0x72,f4:0x73,f5:0x74,f6:0x75,
|
||
|
|
f7:0x76,f8:0x77,f9:0x78,f10:0x79,f11:0x7A,f12:0x7B,
|
||
|
|
};
|
||
|
|
|
||
|
|
function toVk(key) {
|
||
|
|
if (VK[key]) return VK[key];
|
||
|
|
const c = key.toUpperCase().charCodeAt(0);
|
||
|
|
return (c >= 0x30 && c <= 0x5A) ? c : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
function moveMouse(x, y) { SetCursorPos(x, y); }
|
||
|
|
|
||
|
|
function mouseClick(btn) {
|
||
|
|
const [dn, up] = btn === 'right' ? [MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP]
|
||
|
|
: btn === 'middle' ? [MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP]
|
||
|
|
: [MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP];
|
||
|
|
mouse_event(dn, 0, 0, 0, 0);
|
||
|
|
mouse_event(up, 0, 0, 0, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
function mouseToggle(state, btn) {
|
||
|
|
const down = btn === 'right' ? MOUSEEVENTF_RIGHTDOWN : btn === 'middle' ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_LEFTDOWN;
|
||
|
|
const up = btn === 'right' ? MOUSEEVENTF_RIGHTUP : btn === 'middle' ? MOUSEEVENTF_MIDDLEUP : MOUSEEVENTF_LEFTUP;
|
||
|
|
mouse_event(state === 'down' ? down : up, 0, 0, 0, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
function scrollMouse(dx, dy) {
|
||
|
|
if (dy) mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -dy * 120, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
function keyToggle(key, state) {
|
||
|
|
const vk = toVk(key);
|
||
|
|
if (vk) keybd_event(vk, 0, state === 'up' ? KEYEVENTF_KEYUP : 0, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getScreenSize() {
|
||
|
|
return { width: GetSystemMetrics(0), height: GetSystemMetrics(1) };
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { moveMouse, mouseClick, mouseToggle, scrollMouse, keyToggle, getScreenSize };
|