Changing Mouse Sensitivity in Java - java

How do you get the sensitivity of a mouse, change it, and then apply it to the mouse?
-Progress removed, showed the speed of clicking instead of the speed of moving-
I have researched this "everywhere", but there is nothing on this subject.

First of all I think arg0.getXOnScreen will give You the absolute x coordinate of the mouse, not the old position as You're assuming by defining variable named oldX. getX should give you the position within the panel or (sth like widget i do not know the api you are using). The second thing is... what do You mean 'sensitivity of mouse' Do you want to change global system settings for mouse from java ? I do not think it is even possible. Look here this will require You to add jni lib to project and invoke some native libs, so You make your code platform dependent.

You would probably have to tinker with the Robot class and listnening for mouse events. So you'd have to listen for mouse pressed events and after that use robot to move the mouse 3x more pixels than the mouse is actually moving, then you'd have to do a mouse up event via the Robot class, reposition the mouse to the original position followed by a mouse down event via the robot.
See how this can be problematic? This would be extremely use case driven and not something to do generically. I've been doing Java a long time so I could probably pull it off but it is not something a novice could probably do because other issues would come up that need resolution during the debugging process.
Noticed this thread when dealing with a 3d API that rotates the view WAY TO SLOW.

Related

Java AWT Robot places mouse clicks in completely random locations on screen [video explanation]

I am trying to write a program in which a user can create a script of clicks and key presses, and execute them in order. The most important feature of this program is being able to accurately place clicks on proper coordinates on screen. I made this program a year ago in C#, but now I am retrying it in JavaFX so that it is prettier. I am using JNativeHook for key listeners and mouse listeners.
Here is the issue I am facing:
When running robot.mouseMove(), the cursor is sent to a completely random location on screen. I am verifying this by outputting the current location of the cursor both inside of the NativeMouseListener mousePressed function, and inside of the class with robot. To get the mouse position in NativeMouseLIstener, I am using NativeMouseEvents getX and getY functions, which are completely accurate. Then for the other output, I am using PointerInfo.getPointerInfo() which has varying result. When the display DPI scaling is set to 100%, PointerInfo is completely inaccurate. It seems kind of absurd. Robot is sending the mouse ro a random location, and PointerInfo can't even read the location right. If I set the display scaling to 125%, then PointerInfo will get the same location on screen as NativeMouesListener, but the problem with robot still exists.
Are there any alternatives or external libraries I can use to simulate mouse clicks? It is unbelievable that something as dysfunctional as robot is the only thing we have to accomplish this.
Here is a video further explaining my problem:
https://www.youtube.com/watch?v=BerTofDwRUw
Why do yo even need external libraries. You can use the AWT Robot to move the mouse and simulate mouse clicks and key presses.
https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html

Java: getting the mouse's polling rate?

I'm writing code which moves the mouse in an applet by sending MouseEvent objects for it to process. In order for my code to move the mouse from one location to another, I need to generate points to fill the path so that my mouse can move through them. However, in order to create the right amount of points (i.e., to mimic movement as if done by physically moving the mouse), I need to determine the physical mouse's polling rate so I know how often it tells my machine about its position.
I looked around for ways to retrieve this value, but the best that I found was the MouseInfo class, but all that it tells me is the number of buttons on the mouse and some information about its pointer - not what I'm looking for. Does anyone know a way (preferably without some sort of external dependency) to read the physical mouse's polling rate?
I'm not sure if there's a solution for this in the API, but I suggest setting up a mouseListener and capture timestamps with System.currentTimeMillis() or System.nanoTime(), then just wave the mouse around for a bit and measure the time between events fireing. While you're moving the mouse, the MouseEvents should fire as fast as the mouse is polled. I think.

Java: Holding the cursor in an area

For those of you who have played Madness Interactive, one of the most frustrating things is when the cursor leaves the game area, and you accidentally click. This causes the game to defocus and your character dies in a matter of seconds. To fix this, I'd like to make a java application that I can run in the background that will hold the cursor inside the screen until I press a key, like ESC or something.
I see two ways of implementing this, but I don't know if either of them are workable.
Make an AWT frame that matches the size of Madness Interactive's render area, and control the cursor using that.
Use some out-of-context operating system calls to keep the cursor in a given area.
Advantage of approach #1: Much easier to implement resizing of the frame so that user can see the shape and position of the enclosed area.
Potential Problems with approach #1: The AWT Frame would likely need to steal focus from the browser window the game is running in, making the whole solution pointless.
My question is, are either of these approaches viable? If not, is there a viable option?
EDIT: I am willing to use another programming language if necessary.
EDIT2: I might develop a browser plugin for this, but I've never done that kind of development before. I'll research it.
If you're still interested in working in Java, here's a possible solution for you.
First, in order to limit the cursor within an area, you could use the Java Robot class.
mouseMove(int x, int y);
Then, you could use AWT's MouseInfo to get the position of the mouse cursor.
PointerInfo mouseInfo = MouseInfo.getPointerInfo();
Point point = mouseInfo.getLocation();
int x = (int) point.getX();
int y = (int) point.getY();
Then, whenever the x and y value of the mouse cursor go beyond a certain point, move them back using the Java Robot class.
If this is for a browser-based game, consider writing a greasemonkey script, which acts as a browser extension that can be filtered to only run on the game's site.
In the simplest case, assume the clickable regions are (0,0) - (300,400), then you can add the following event handler to the page:
$(document).on('click', function(event) {
if (event.pageX > 300 || event.pageY > 400) {
return false;
}
});
You can further refine your script to do the following:
resize the browser to be the perfect size for playing the game
instead of checking the absolute x,y coords of the click, check if it is inside an element of the page that you don't want to receive the click
add custom key bindings to umm.. help you at the game
write a javascript bot that can play the game itself

Differentiate Between Robot mouseclick and human mouse click

I have an applet that I'm running online and I want to make sure people can't use the java Robot class to operate the applet. I know that yahoo does this on several of their game platforms and I was wondering if anyone knew how they accomplished it.
Watch mouse movement, and make sure you're not seeing "jumps" from one place to another, but movement over time instead. Sun/Oracle's J2SE tutorials show how to follow mouse movement events: http://download.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
Keep in mind that this would potentially fail to detect the difference between a robot and a person on something like a touch screen, or tablet input device.
One more thing to watch for is whether the user is clicking the same pixel, or just in the same vicinity. Humans are fairly imprecise, robots generally aren't unless programmed to be.
I would also put in a gesture logger for good measure that compiles this information, and keeps track of the actual movements of your users. If you suspect someone of cheating, you can then look at what their actual mouse movements looked like, and compare that with a known person. That will give you a better idea of what you need to look for than any of us can come up with off the tops of our heads.
keep track of the distribution of mouse positions over time. Humans move the mouse differently than a robot that knows exactly where to position it every single time it is clicked. Of course, a smarter robot can counter this defense.

Drawing/plot is not continuous (mouse events involved)

I'm having issues with a java class project. The first step consist in drawing a pattern, so I thought well, this can't be hard. And it isn't, but there one thing that really bothers me. First, check the screenshot below :
Screenshot
My problem is that this was done without releasing the mouse, so the drawing should be continuous. Instead of this, there's holes in it. I'm thinking this is related to the way mouse events are transmitted, But I have no idea how to tweak this.
The drawing zone is a JPanel. There is a set of Points that is used to paint the container on mouse events. Pressing add the first point, dragging the others, released clear the drawing zone.
Hope I was specific enough. Thanks for your time!
edit : Forgot the code. http://pastebin.com/RyXiGsvm
StanislavL's right that mouseMove/mouseDrag events are not generated for every pixel you move the mouse cursor over. Why don't you want to use lines? If the issue is that the result is ugly and jagged, you might consider using cubic splines instead. GeneralPath.curveTo provides an easy way to do this. If getting the control points right is a pain, you can also use GeneralPath.quadTo; this is a quadratic approximation that won't look quite as good, but you can easily just pass in the last three points from mouseDrags.
I guess you store mouse points obtained in mouseDrag processing. Mouse drag happens from after some time interval so if you move mouse relatively fast you just got points. TO drai it you can use drawLine() passing pairs of point. So you'll have lines.

Categories