UNPKG

system-user-input

Version:

A robust Node.js library for capturing and simulating system-wide user input events like a pro! 🚀

103 lines • 3.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InputSimulator = void 0; const child_process_1 = __importDefault(require("child_process")); const process_1 = __importDefault(require("process")); const path_1 = __importDefault(require("path")); const __1 = require(".."); class InputSimulator { process = null; started = false; mouse; keyboard; constructor() { this.mouse = new MouseSimulator(this); this.keyboard = new KeyboardSimulator(this); } async start() { if (this.started) return false; this.started = true; this.process = child_process_1.default.spawn(path_1.default.resolve(__1.modulePath, process_1.default.platform === "win32" ? "./key-listener.exe" : "./key-listener"), ["SIMULATION"], { shell: true }); this.process.stdin.setDefaultEncoding("utf-8"); return true; } async stop() { if (!this.started) return false; this.started = false; this.process?.kill(); this.process?.removeAllListeners(); this.process = null; return true; } send(data) { if (!this.process) return; this.process.stdin.write(`${JSON.stringify(data)}\n`); } } exports.InputSimulator = InputSimulator; class MouseSimulator { simulator; constructor(simulator) { this.simulator = simulator; } click(button = "left") { this.simulator.send({ event_type: "mouse", action: "click", button }); } press(button = "left") { this.simulator.send({ event_type: "mouse", action: "press", button }); } release(button = "left") { this.simulator.send({ event_type: "mouse", action: "release", button }); } async move(x, y, duration = 100, ease = "linear") { this.simulator.send({ event_type: "mouse", action: "move", x, y, duration_ms: duration, ease }); if (duration) await new Promise((r) => setTimeout(r, duration)); } async scroll(deltaX, deltaY, duration = 100, ease = "linear") { this.simulator.send({ event_type: "mouse", action: "scroll", delta_x: deltaX, delta_y: deltaY, duration_ms: duration, ease }); if (duration) await new Promise((r) => setTimeout(r, duration)); } } class KeyboardSimulator { simulator; constructor(simulator) { this.simulator = simulator; } press(key) { this.simulator.send({ event_type: "key", action: "press", key }); } release(key) { this.simulator.send({ event_type: "key", action: "release", key }); } tap(key) { this.simulator.send({ event_type: "key", action: "tap", key }); } type(text) { this.simulator.send({ event_type: "text", text }); } } //# sourceMappingURL=InputSimulator.js.map