KeyStroke equivalant class for Mouse in Java 6? - java

Is there a Mouse equivalent class for KeyStroke? I'm looking for some kind of wrapper around a a MouseEvent to describe which button was pushed (I can get this through SwingUtilities), and any modifiers used as well. The idea is I'm already catch AWT mouse events through a listener, but then taking that event and processing it for 3D picking in a virtual world. I'd like to try and map mouse bindings in such a way (similar to InputMap and ActionMap with swing controls). KeyStroke has been a god-send, anyone know of a MouseStroke or similar?

A "mouse" is traditionally defined as having a maximum of three buttons. That is also what Java supports by MouseEvent.getButton().
More advanced mice with multiple buttons are usually installed as multiple HID devices. Meaning they install two drivers, for a mouse AND for a keyboard. For these mice, you can set what other buttons mean, and usually it's something like a key press (for example, shift) or double click. Those are events that Java can catch, either as MouseEvent of KeyEvent.
More advanced functionality, such as "Open an application" or "change DPI", is implemented in the driver. Being device-specific, there is nothing Java can do to catch those events (you would have to write your own native listener for these events, provided the driver support that).

Related

Who handles a event created by a component in java

I have read that that a event created by a component(like a button) is handled by a action listener(for a button) but one of my teacher says that every event is handled by the operating system so I am confused to as who actually handles the event, is it the operating system or does java do that work.
I think the two instances of the word handle here mean quite different things.
Your code handles events that are raised. So imagine the button says "Oh I've been clicked!" and you go "Right, I'll handle it!" and you do this by doing something like this:
button.addActionListener(e -> {...});
The OS handles the raising of events. Your mouse pointer is not part of your program, right? So the OS will detect where your mouse pointer is when a "The left mouse button has been clicked" signal is sent to the OS. The OS will say "The mouse pointer is located at this position on the screen, and there is a button there. That means that the button should be clicked. Hey! that button over there! The user clicked on you at (someX, someY)!". And then the JButton class will first try to animate a click animation and raise the "action performed" event.
Certain types of events occur as a direct result of user input. When the user types a key or moves the mouse, for example, a KeyEvent or MouseEvent is generated. Similarly, when the user resizes a window or transfers keyboard focus to a new component, a FocusEvent or ComponentEvent is generated. These types of events represent event notifications generated by the underlying native windowing system or operating system. Other types of events, such as ActionEvent and PopupMenuEvent, do not originate in the native windowing system. Instead, these events are generated directly by AWT and Swing components

I am unsure how to record how many times a mouse was clicked in a certain area

I'm fluent in java, and have messed with vb.net, but I prefer java. I wish to make a program that ++'s a variable everytime I click my mouse in a certain coordinate on my screen. Not sure how to record when a mouse click has happened outside of the program's forms.
This can't be done in pure Java but people have written JNI libraries that can capture global mouse and keyboard events. Take a look at
https://github.com/kwhat/jnativehook

Reduce sensitivity of mouse click detection

I've had this problem repeatedly over the years with AWT and Swing based interfaces: some mouse clicks events do not trigger mouseClicked in a MouseListener because the mouse moved at least one pixel during the click (which is most clicks with some mice). This is interpreted as a drag operation instead.
Is there a way to tell AWT/Swing to be a bit more permissive in its definition of a click?
Manually implementing a workaround is quite cumbersome if you want a full solution (if your components must also handle drag operations for instance):
Adding a unique MouseListener and MouseMotionListener to every component
Doing some sort of calculation of a click in mouseMoved, mouseDragged and mouseReleased (which requires measuring time elapsed, distance traveled...)
Forwarding the "fixed" mouse events to a new mouse input interface implemented by the components
There's got to be a better way! I'm hoping for a global setting but I just can't find one...
Related posts
https://stackoverflow.com/questions/20493509/drag-threshold-for-some-components-too-low
Making a component less sensitive to Dragging in Swing
Try setting the sensitivity of the D&D using:
System.setProperty("awt.dnd.drag.threshold", "5");
Note: this is only honored by List, Table, Text and Tree components!
See usages of javax.swing.plaf.basic.DragRecognitionSupport#mouseDragged() method.

Java - Send mouse events to Applet

I'm trying to make a program which can send mouse input to a Runescape applet. Before you ask what I want this for, it isn't a bot. I'm making a "Twitch Plays Pokemon" program for Runescape which has been confirmed to be allowed.
Anyway, I have created a loader which will pull the game jar from the website and open it in a JFrame, meaning that I have an Applet instance which contains the game. I need to somehow dispatch mouse events to this applet. I've looked everywhere but whenever I search for this, I just find pages about listening for mouse clicks instead of dispatching them...
I should note that the Robot class isn't what I'm looking for; the mouse actions must be virtual and run within the application. I know this is possible but I'm struggling to find out how it's done.
How can I accomplish this? I want to be able to send mouse hover events as well as right/left click events.
I've found my answer, guys. It was quite simple. This is what I did to perform a mouse click on the applet:
applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
MouseEvent.MOUSE_PRESSED,
System.currentTimeMillis() + 10,
MouseEvent.BUTTON1,
x,y,
0,
false));
applet.getComponent(0).dispatchEvent(new MouseEvent(applet,
MouseEvent.MOUSE_RELEASED,
System.currentTimeMillis() + 10,
MouseEvent.BUTTON1,
x,y,
0,
false));
The thing to note here is the applet.getComponent(0) part which was actually directed at the game canvas.
You can probably do this with the java.awt.Robot class. I've never done it but it seems like it would work.
You could use JNI and the Windows API (assuming this is all running on windows, other platforms probably have similar corollaries) to send simulated mouse events to just that window.
You can use Spy++ to monitor the messages being sent to that window. You can use FindWindow to get the window's hWnd, and then use SendMessage or PostMessage to send the simulated mouse events.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx

regain focus after java.awt.Robot.keyPress()/mousePress()

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off

Categories

Resources