Click with robotjs when the speed of the mouse is low
What am I trying to achieve?
I'm trying to make a program (like sofType) with auto-clicking tools and on-screen keyboard for disabled persons.
Why am I doing this? I'm doing this because sofType cost alot and some countries doesn't help these people financially with this program.
Will this be free? Yes, open-sourced as well (Github-link here).
What's my issue for today?
Currently behavior: The mouse is clicking even if its speed is higher than expected.
Expect behavior: As soon as the speed of the mouse is == 100
then it should click and do the rest.
The code index.js
const robot =require('robotjs');
const mousemoved = require('iohook');
let {isPressed, hasBeenUsed,windowHasBeenLoaded} = false;
let {lastX,lastY,cursorX, cursorY} = 0;
let mouseMove,prev;
mousemoved.on('mousemove',ev => mouseMove(ev));
mouseMove = (events)=>{
if(hasBeenUsed) return;
if(!windowHasBeenLoaded){
windowHasBeenLoaded = true;
prev ={x:events.x,y:events.y,prevTime : Date.now()};
event = events
}
lastY = events.y;
lastX = events.x;
console.log(calculator(events,prev).mSpeed)
if((calculator(events,prev).mSpeed == 100 && calculator(events,prev).mSpeed > 0) && (calculator(events,prev).mVelocity <= 0.04) && (calculator(events,prev).mMovement <= 1 && calculator(events,prev).mMovement > 0) && !isPressed){
hasBeenUsed=true;
mousemoved.removeAllListeners('mousemove')
setTimeout(() => {
robot.mouseClick()
mousemoved.on('mouseclick', (e) => {
cursorX = e.x;
cursorY = e.y;
});
isPressed = true; hasBeenUsed = false
mousemoved.on('mousemove',event => mouseMove(event))
}, 1000);
}
if((diff(cursorX,lastX) > 57 || diff(lastY,cursorY) > 57) && (isPressed && !hasBeenUsed)){
isPressed = false;
}
prev ={x:events.x,y: events.y,prevTime : Date.now()};
}
mousemoved.start();
functions.js
function squareroot(number) {
let lo = 0, hi = number;
while(lo <= hi) {
let mid = Math.floor((lo + hi) / 2);
if(mid * mid > number) hi = mid - 1;
else lo = mid + 1;
}
return hi;
}
let calculator=(currentEvent,prevEvent)=>{
let time = Date.now();
let movementX=Math.abs(currentEvent.x-prevEvent.x);
let movementY=Math.abs(currentEvent.y-prevEvent.y);
let movement=squareroot(movementX*movementX+movementY*movementY);
let speed=100*movement;
let velocity = (movement / (time - prevEvent.prevTime));
return {mSpeed:speed,mVelocity:velocity,mMovement:movement}
}
let diff=(num1, num2)=>{
return (num1 > num2)? num1-num2 : num2-num1
}
Other information
- Electron version: 5.0.0
- Node version: 10.16.3
- Robotjs version: 0.5.1
- IoHook version: 0.4.6