node-system-bridge
Version:
A cross-platform system automation library for Node.js and Electron, written in Rust
144 lines (125 loc) • 4.45 kB
JavaScript
/**
* Mouse Operations - Complete Example and Documentation
*
* Features demonstrated:
* - Get mouse position
* - Instant and smooth movement
* - Click, double-click, right-click
* - Drag operations
* - Scroll wheel
* - Smooth movement parameter adjustments
*
* API Reference:
* - Mouse.getPosition() - Get current mouse position
* - Mouse.moveTo(x, y) - Move instantly to specified coordinates
* - Mouse.moveToSmooth(x, y, durationMs?, useBezier?, curveIntensity?, addJitter?) - Smooth movement
* - Mouse.click(x, y, button?, clickType?) - Click at coordinates
* - Mouse.mouseDown(button?) / Mouse.mouseUp(button?) - Press/release mouse button
* - Mouse.scroll(amount) - Scroll wheel
*
* Parameters:
* - button: 'left' | 'right' | 'middle' (default: 'left')
* - clickType: 'single' | 'double' (default: 'single')
* - durationMs: Movement duration in milliseconds, auto-calculated if not specified
* - useBezier: Whether to use Bezier curve (default: true)
* - curveIntensity: Curve intensity 0.0-1.0 (default: 0.2)
* - addJitter: Whether to add random jitter (default: true)
*
* Permissions:
* - macOS: System Preferences → Security & Privacy → Privacy → Accessibility
* - Windows: Run as administrator (if needed)
* - Linux: Requires X11 environment
*/
import { Mouse } from '../index.js'
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
async function main() {
try {
console.log('🖱️ Mouse Operations Demo\n')
// 1. Get position
console.log('【Position】')
const startPos = Mouse.getPosition()
console.log(` Current position: (${startPos.x}, ${startPos.y})\n`)
await delay(500)
// 2. Basic movement
console.log('【Movement】')
console.log(' Instant move to (500, 300)')
Mouse.moveTo(500, 300)
await delay(500)
console.log(' Smooth move to (800, 400)')
Mouse.moveToSmooth(800, 400, 1000)
await delay(1200)
console.log()
// 3. Click operations
console.log('【Click】')
console.log(' Left click at (600, 300)')
Mouse.click(600, 300)
await delay(500)
console.log(' Right click at (650, 300)')
Mouse.click(650, 300, 'right')
await delay(500)
console.log(' Double click at (700, 300)')
Mouse.click(700, 300, 'left', 'double')
await delay(500)
console.log()
// 4. Drag demo
console.log('【Drag】')
console.log(' Smooth drag: (400, 400) → (600, 500)')
Mouse.moveToSmooth(400, 400, 500)
await delay(600)
Mouse.mouseDown('left')
await delay(100)
Mouse.moveToSmooth(600, 500, 800)
await delay(900)
Mouse.mouseUp('left')
await delay(500)
console.log()
// 5. Scroll wheel
console.log('【Scroll】')
console.log(' Scroll up')
Mouse.scroll(5)
await delay(300)
console.log(' Scroll down')
Mouse.scroll(-5)
await delay(500)
console.log()
// 6. Smooth movement comparison
console.log('【Smooth Movement Comparison】')
console.log(' Linear movement (no curve)')
Mouse.moveToSmooth(300, 300, 600, false)
await delay(700)
console.log(' Bezier curve movement (default)')
Mouse.moveToSmooth(700, 400, 800)
await delay(900)
console.log(' High curvature movement')
Mouse.moveToSmooth(300, 500, 1000, true, 0.4)
await delay(1100)
console.log()
// 7. Continuous operations
console.log('【Continuous Operations】')
const points = [
[400, 300],
[600, 300],
[600, 400],
[400, 400]
]
for (let i = 0; i < points.length; i++) {
const [x, y] = points[i]
Mouse.moveToSmooth(x, y, 500)
await delay(600)
}
console.log(' ✓ Rectangle path completed\n')
// Return to starting position
console.log('【Complete】')
Mouse.moveToSmooth(startPos.x, startPos.y, 1000)
await delay(1100)
console.log('✅ All demos completed!\n')
console.log('💡 Tips:')
console.log(' - moveTo() instantly moves, suitable for quick positioning')
console.log(' - moveToSmooth() moves smoothly, simulates human operation')
console.log(' - Parameters can be adjusted: duration, curve, intensity, jitter\n')
} catch (error) {
console.error('❌ Error:', error.message)
process.exit(1)
}
}
main()