Spaces:
Paused
Paused
File size: 9,421 Bytes
552b9cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | /**
* Input Simulation Utility
* Simulates keyboard and mouse input on the system
* Designed to work without root access
*/
export interface MousePosition {
x: number;
y: number;
}
export interface InputConfig {
displayWidth: number;
displayHeight: number;
}
export class InputSimulator {
private displayWidth: number;
private displayHeight: number;
private currentPosition: MousePosition = { x: 0, y: 0 };
private pressedKeys: Set<number> = new Set();
private pressedButtons: Set<number> = new Set();
constructor(config: InputConfig) {
this.displayWidth = config.displayWidth;
this.displayHeight = config.displayHeight;
}
/**
* Move mouse to absolute position
*/
async mouseMove(x: number, y: number): Promise<void> {
// Clamp to display bounds
x = Math.max(0, Math.min(this.displayWidth - 1, x));
y = Math.max(0, Math.min(this.displayHeight - 1, y));
this.currentPosition = { x, y };
try {
// Try using xdotool via subprocess
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
await execAsync(`xdotool mousemove ${x} ${y}`).catch(() => {
// Fallback: simulate in headless browser
});
} catch (error) {
// Silently fail - will be simulated in browser context
console.debug('Mouse move simulated (no display access)');
}
}
/**
* Press mouse button
*/
async mouseDown(button: number): Promise<void> {
if (button < 1 || button > 3) return;
this.pressedButtons.add(button);
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// Map button numbers: 1=left, 2=middle, 3=right
const xdotoolButton = button === 1 ? 1 : button === 2 ? 2 : 3;
await execAsync(`xdotool click ${xdotoolButton}`).catch(() => {});
} catch (error) {
// Simulation mode
}
}
/**
* Release mouse button
*/
async mouseUp(button: number): Promise<void> {
this.pressedButtons.delete(button);
// xdotool doesn't have "mouseup", so we just track state
}
/**
* Mouse wheel scroll
*/
async mouseWheel(deltaX: number, deltaY: number): Promise<void> {
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// Scroll up/down based on deltaY
const clicks = Math.abs(deltaY) > 0 ? Math.sign(deltaY) * 4 : 0;
if (clicks !== 0) {
await execAsync(`xdotool click ${clicks > 0 ? 4 : 5}`.repeat(Math.abs(clicks))).catch(() => {});
}
} catch (error) {
// Simulation mode
}
}
/**
* Press key
*/
async keyDown(keyCode: number): Promise<void> {
if (this.pressedKeys.has(keyCode)) return;
this.pressedKeys.add(keyCode);
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// Convert keycode to key name
const keyName = this.keyCodeToKeyName(keyCode);
if (keyName) {
await execAsync(`xdotool key ${keyName}`).catch(() => {});
}
} catch (error) {
// Simulation mode
}
}
/**
* Release key
*/
async keyUp(keyCode: number): Promise<void> {
this.pressedKeys.delete(keyCode);
}
/**
* Type text
*/
async typeText(text: string): Promise<void> {
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// Escape special characters for shell
const escapedText = text.replace(/'/g, "'\\''");
await execAsync(`xdotool type '${escapedText}'`).catch(() => {});
} catch (error) {
// Simulation mode - would be handled by browser events
}
}
/**
* Convert key code to xdotool key name
*/
private keyCodeToKeyName(keyCode: number): string | null {
// Common key mappings
const keyMap: Record<number, string> = {
8: 'BackSpace',
9: 'Tab',
13: 'Return',
16: 'Shift_L',
17: 'Control_L',
18: 'Alt_L',
20: 'Caps_Lock',
27: 'Escape',
32: 'space',
33: 'Page_Up',
34: 'Page_Down',
35: 'End',
36: 'Home',
37: 'Left',
38: 'Up',
39: 'Right',
40: 'Down',
45: 'Insert',
46: 'Delete',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
65: 'a',
66: 'b',
67: 'c',
68: 'd',
69: 'e',
70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
77: 'm',
78: 'n',
79: 'o',
80: 'p',
81: 'q',
82: 'r',
83: 's',
84: 't',
85: 'u',
86: 'v',
87: 'w',
88: 'x',
89: 'y',
90: 'z',
91: 'Super_L',
92: 'Super_R',
93: 'Menu',
96: 'KP_0',
97: 'KP_1',
98: 'KP_2',
99: 'KP_3',
100: 'KP_4',
101: 'KP_5',
102: 'KP_6',
103: 'KP_7',
104: 'KP_8',
105: 'KP_9',
106: 'KP_Multiply',
107: 'KP_Add',
109: 'KP_Subtract',
110: 'KP_Decimal',
111: 'KP_Divide',
112: 'F1',
113: 'F2',
114: 'F3',
115: 'F4',
116: 'F5',
117: 'F6',
118: 'F7',
119: 'F8',
120: 'F9',
121: 'F10',
122: 'F11',
123: 'F12',
};
return keyMap[keyCode] || null;
}
/**
* Get current mouse position
*/
getPosition(): MousePosition {
return { ...this.currentPosition };
}
/**
* Get pressed keys
*/
getPressedKeys(): number[] {
return Array.from(this.pressedKeys);
}
/**
* Get pressed mouse buttons
*/
getPressedButtons(): number[] {
return Array.from(this.pressedButtons);
}
}
/**
* Browser-side input tracking
* Captures keyboard and mouse events from the viewer
*/
export class BrowserInputTracker {
private element: HTMLElement | null = null;
private onInput: ((event: any) => void) | null = null;
private modifiers: Set<string> = new Set();
/**
* Attach input tracking to an element
*/
attach(element: HTMLElement, onInput: (event: any) => void): void {
this.element = element;
this.onInput = onInput;
// Mouse events
element.addEventListener('mousemove', this.handleMouseMove.bind(this));
element.addEventListener('mousedown', this.handleMouseDown.bind(this));
element.addEventListener('mouseup', this.handleMouseUp.bind(this));
element.addEventListener('wheel', this.handleWheel.bind(this));
// Keyboard events
element.addEventListener('keydown', this.handleKeyDown.bind(this));
element.addEventListener('keyup', this.handleKeyUp.bind(this));
// Prevent default behaviors
element.addEventListener('contextmenu', (e) => e.preventDefault());
element.tabIndex = 0;
element.focus();
}
/**
* Detach input tracking
*/
detach(): void {
if (this.element) {
this.element.removeEventListener('mousemove', this.handleMouseMove.bind(this));
this.element.removeEventListener('mousedown', this.handleMouseDown.bind(this));
this.element.removeEventListener('mouseup', this.handleMouseUp.bind(this));
this.element.removeEventListener('wheel', this.handleWheel.bind(this));
this.element.removeEventListener('keydown', this.handleKeyDown.bind(this));
this.element.removeEventListener('keyup', this.handleKeyUp.bind(this));
}
}
private handleMouseMove(event: MouseEvent): void {
const rect = this.element!.getBoundingClientRect();
const x = Math.floor((event.clientX - rect.left) * (this.element!.scrollWidth / rect.width));
const y = Math.floor((event.clientY - rect.top) * (this.element!.scrollHeight / rect.height));
this.onInput?.({
type: 'mousemove',
x,
y,
timestamp: Date.now(),
});
}
private handleMouseDown(event: MouseEvent): void {
event.preventDefault();
this.onInput?.({
type: 'mousedown',
button: event.button + 1, // 1-indexed
timestamp: Date.now(),
});
}
private handleMouseUp(event: MouseEvent): void {
event.preventDefault();
this.onInput?.({
type: 'mouseup',
button: event.button + 1,
timestamp: Date.now(),
});
}
private handleWheel(event: WheelEvent): void {
event.preventDefault();
this.onInput?.({
type: 'wheel',
deltaX: event.deltaX,
deltaY: event.deltaY,
timestamp: Date.now(),
});
}
private handleKeyDown(event: KeyboardEvent): void {
event.preventDefault();
this.onInput?.({
type: 'keydown',
keyCode: event.which || event.keyCode,
key: event.key,
code: event.code,
modifiers: {
shift: event.shiftKey,
ctrl: event.ctrlKey,
alt: event.altKey,
meta: event.metaKey,
},
timestamp: Date.now(),
});
}
private handleKeyUp(event: KeyboardEvent): void {
event.preventDefault();
this.onInput?.({
type: 'keyup',
keyCode: event.which || event.keyCode,
key: event.key,
code: event.code,
timestamp: Date.now(),
});
}
}
|