Comments (33)
Hi Max!
Could you try putting a sleep between the two? I believe the mouse event is just taking longer on your computer. The tests confirm that getting and setting like that should work as expected.
from robotjs.
\node_modules\robotjs\test>node mouse.js
TAP version 13
Get the initial mouse position.
ok 1 successfully retrieved mouse position.
ok 2 mousepos.x is a valid value.
ok 3 mousepos.y is a valid value.
Move the mouse.
ok 4 successfully moved the mouse.
not ok 5 mousepos.x is correct.
operator: ok
expected: true
actual: false
...
not ok 6 mousepos.y is correct.
operator: ok
expected: true
actual: false
...
1..6
tests 6
pass 4
fail 2
from robotjs.
Yeah those tests failing mean it's the mouse delay. :/
You could use RobotJS from GitHub which includes a setMouseDelay function. The default delay is currently 10ms and I knew this was going to be an issue.
from robotjs.
robot.moveMouse(10, 10);
setTimeout(function(){
console.log(robot.getMousePos());
},1000);
// { x: 9, y: 9 }
It seems like mouse always try to reach 0,0 coordinates.
from robotjs.
Hmm that's super odd! Thanks for trying that.
@Deltatiger could you try the above code when you get a chance? It works in my VM, I wonder if it's a Windows 10 issue.
from robotjs.
I compiled robotjs from github master sources using node-gyp.
Sad, but it works same. :(
from robotjs.
I also will try this later on other Windows.
from robotjs.
Interesting: moveMouse(1,1) moves mouse precisely on very left top corner (0,0)
I think this is not getMousePos problem, but moveMouse issue...
from robotjs.
var pos = robot.getMousePos();
robot.moveMouse(pos.x, pos.y);
This should not do anything, but it moves cursor a bit to left and top according to current position (depends of current position distance from 0,0, very far - very large step)
from robotjs.
SetCursorPos(point.x, point.y); instead of mouse_event in mouse.c works very nice.
from robotjs.
Thanks that's good to know! I believe mouse _event is being deprecated anyway. I'll use this as an opportunity to test different mouse setting functions ( I believe there's 4 ways on Windows). If I remember correctly, SendInput works really well.
from robotjs.
I tested SendInput and it worked like mouse_event - weird 65535 divisions seems to be not so precise. Maybe this depends on some mouse or screen configuration.
from robotjs.
I found solution: #78
This will work for SendInput too.
from robotjs.
@maxdeepfield I think SendInput should be used in windows. It supports multiple input devices and is recommended by MS.
@octalmage I will test things out again in Windows 7 and 10. Just to check if things work out. Also will set up a branch on my repo to convert stuff to SendInput(Mouse and Keyboard). If that works out then it should be better.
from robotjs.
@Deltatiger agree, but SendInput also must use 65536.0f instead of 65536 or 0xFFFF
from robotjs.
Strange, but this 65536.0f correction doesnot work very fine using over remote desktop. Its is now going closer, but not so precise as SetCursorPos
from robotjs.
I was afraid of that, since the current code works for some users.
from robotjs.
The mouse code has some issues, first of all why is mouse_event being used instead of SendInput? According to MSDN mouse_event has been deprecated for quite some time. As for mouse movement why not use SetCursorPos here?
from robotjs.
As mentioned in discussion above - SendInput should be used, but this does not help in some cases, SetCursorPos working best for me.
from robotjs.
@maxdeepfield I tried tweaking stuff a bit. Try this out.
point.x = ceil(point.x * 65536.0 / GetSystemMetrics(SM_CXSCREEN));
point.y = ceil(point.y * 65536.0 / GetSystemMetrics(SM_CYSCREEN));
I still am using the mouse_event but this gives me close to accurate readings.
from robotjs.
@octalmage @maxdeepfield So the issue was mouse_event was dealing with the screen by normalizing it between values 0 to 65536. Very inaccurate if you ask me. The rounding issue fixes it a bit. But to completely fix it we would have to migrate to SendInput. That deals with pixels directly.
from robotjs.
Has there been any update to this?
I had issues with my mouse drifting when using moveMouse on Windows 10.
var mouse = robot.getMousePos();
robot.moveMouse(mouse.x, mouse.y);
This will make your mouse drift to the top left corner when running it in a loop.
from robotjs.
@DomiStyle This is an issue with Windows API that is being used currently. The problem has already been identified. Will send a PR soon so that it can be fixed.
from robotjs.
Should we maybe use SetCursorPos for now? I would like to get #105 finished and this is blocking it.
from robotjs.
Actually I'm going to go ahead and merge the appveyor branch. This is broken so the tests should fail.
from robotjs.
Thanks to AutoHotkey, I got it! Looks like they went through the same thing we did:
Man I LOVE open source.
Anyway, this needs more testing but I'm going to merge it soon.
from robotjs.
We can switch to SendInput later, it should be easy now that we have the conversion down. I'll open a new issue for this.
from robotjs.
@maxdeepfield could you confirm if the code in the master branch resolves this for you? In my tests moveMouse was accurate.
from robotjs.
@octalmage Works fine for me now.
from robotjs.
Awesome thanks @DomiStyle! I'll release a new version later today.
from robotjs.
v0.3.2 published to npm which fixes this issue.
from robotjs.
Thanks for fixing this @octalmage! 👍
from robotjs.
Of course! That's why I'm here!
from robotjs.
Related Issues (20)
- How to make screen of visible window on macos? HOT 2
- git clone npm i why error HOT 2
- All key events continuously hold down keys on Windows only. HOT 1
- Does not work on windows 11 HOT 1
- Using the typeString() method does not let you find the name of a file.
- Installing for nw.js - "A dynamic link library (DLL) initialization routine failed."
- Robotjs doesn't work with Electron, current time 2023.11.27 HOT 3
- or
- In the multi-instance tool, RobotJS is not working. There are no issues with reading properties, but moving the mouse or pressing the keyboard is not functioning correctly.
- Is there anyway to use this library in Nextjs app
- I cannot install robotjs HOT 7
- UTF-8 client type string HOT 1
- Focused program does not react to keyup event
- Mouse is not moving when using Remote Desktop Connection with Windows HOT 1
- Badges on README.md are broken
- Error, Help! HOT 1
- When using typeString() to output consecutive identical unicode characters, only the first character will be output
- I'm reading the documentation thoroughly. It does not capture mouse click?
- Electron combined with robotjs on Win10 and Win11 systems: scrollMouse method does not work HOT 3
- keyTap('right_alt') actually send left alt instead of right alt
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from robotjs.