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
Related
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
I am building a program that records a "kill feed" in the top right of a full-screen game and performs two actions as a result:
Update a counter I've painted to the screen with an always-on-top, transparent window that represents the number of players alive on each team (in the style of "2 v 1"), and
render a "hitmarker" graphic when my in-game username appears in the killfeed, indicating that I've killed my opponent
I've been programming in Java for three years, but I still don't know how to achieve this. I am able to use Graphics2D to paint graphics such as a "hitmarker" and draw strings such as the player count I mentioned above. However, I do not know how to recognize colors or text on screen, aside from Java's built-in Java's built-in Robot's getPixelColor() method (which I've never used before).
My best guess is to use java.util.Robot's createScreenCapture() method to save an image of the screen 60 times per second and do some OCR magic to recognize my username, then perform the two actions I detailed above.
Because I'm relatively inexperienced, specific and detailed answers would be much appreciated.
EDIT: I know it's not feasible to save the screen 60 times a second. My intention was for another method to be proposed. Also, as clarification, I am drawing over a game, and reading a killfeed that is a part of the game, not my program. I'm not rendering the feed, just the player counter and hitmarker (killmarker) because it's not a feature of the game like the killfeed is.
I recently got into Slick2D and Java game development and I've come across a "dead end" in my programming skills. I wish to invert the x and y axis (or only x/only y) for the mouse direction, so if the user moves his mouse upwards, the cursor would go downwards, if he moves it to the left, the cursor goes towards the right etc.
After doing some thorough research, it doesn't seem like people wish to do this quite often and I haven't found any relevant information.
I don't think showing my code is necessary since there isn't anything related to what I wish to do in it, but if you need to see it I will be happy to comply.
(Furthermore, another problem came to mind: the cursor would go outside the window and unfocus the program if the user clicks, is there a way to "imprison" the mouse/cursor inside an area in the window? So the user wouldn't be able to leave the window, unless he presses escape to open the pause state etc.)
Since I am still a novice with Slick2D and Java in general, if you could be very specific in your answers by telling me where my code should be modified (init, render, update...) That would be delightful.
If you want to invert a any integer or float you can always do this B = (A *= -1) the B is an inverted/negated version of A.
In case of the mouse it would look something like this.
int RegularMouseX = Mouse.getX();
int InvertedMouseX = (RegularMouseX *= -1);
Asuming you get the new mouse position in the update method like any sane person would do. Recreate or re-set the inverted mouse variable every update after getting the mouse Position.
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.
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.