I have an application in which you can do a right mouse button press and drag (as well as a left press and drag for different operations). However, when running this on linux, it seems that popup menus are triggered by a mousePressed and not a mouseReleased. This is resulting in every time I press the right mouse button to perform a drag, the popup menus are triggered (unlike windows, where it is mouseReleased).
Any thoughts on how to work around this?
thanks.
EDIT: Posting code
Code for popup menu
// this is called from mousePressed and mouseReleased
if (e.isPopupTrigger() && !e.isConsumed()) {
// show the popup menu
}
This code is what is called on the right mouse press/drag (this is 3rd party code, but it is open source so I can change as needed)
// this is called on all mouse events
if (buttonAction.mouseButton != 0)
{
// handle the event
}
Yes, use isPopupTrigger(), as shown here.
Addendum:
it appears isPopupTrigger is triggered on mousePressed in linux.
Yes, it's the same on Mac OS X. You have to call isPopupTrigger() from both mousePressed() and mouseReleased(). There's a related example in GraphPanel.
MouseEvent.isPopupTrigger(). Returns whether or not this mouse event is the popup menu trigger event for the platform.
edit - : You need to make the check in both mousePressed for linux, and mouseReleased for windows.
I think the correct procedure in your case should be to unify where and when to show the popup. As a drag event, if exist, follows a press event you should avoid writting logic for showing the popup in the press event (and then also write logic in the press event for showing the popup). Some users feel good navigating the popup while holding the popup button, and some other users just don't care or don't know. But in your case, you won't be able to navigate the popup menu while dragging without adding extra code.
My way would we to manage the logic to always show the popup on the release event. Entering a release event after a drag should be enough information to know that the popup shouldn't be visible. And of course, always if you can change and modify the source.
Related
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
In my JavaFX application I need to detect when the user canceled a drag action (like right clicking while dragging or pressing ESC button while dragging). I tried to listen on every DragEvent type on my main window, like DRAG_EXITED_TARGET or DRAG_EXITED but those are fired even if the cursor moves to another Node... I'm unable to narrow it to the single event of canceling the drag action.
I read that event.getGestureTarget() == null could mean that the user canceled the action, but it's also null when it leaves to another Node.
I managed to get this working for me by adding a PropertyChangeListener to a window's focusedProperty. When the user cancels the drag action, my window is losing the focus so this works for me.
I would like to know how can I create a mouse move after pressing a button?
I am a fish in coding starting with a simple project and would love to create a step by step clicking process where the mouse will be scrolling and pointing in a certain point, img, or maybe to a class of my project?
Currently, there is no way to force a mouse movement via Javascript mainly due to its security implications.
In your case, you can use focus to guide the user to the specific portion of the page that you want.
You cannot move the mouse in JavaScript for the moment. What you can do however is scrolling the page so that a elements goes under the mouse cursor.
My idea is to move the element you want with JavaScript under the mouse cursor. When the user clicks your button, you can, in the event handler capture the mouse coordinates and use those to place any element exactly under the mouse cursor.
The result of the mouse cursor is above this element can be achieved that way.
try to Create COM, then client will ask to accept some installation.
see here ; http://febru.soluvas.com/2015/02/javascript-move-mouse.html
I want my program to behave diffrently when the user presses a component, and when a user drags the mouse over a component, the problem is that on mouse click both of these methods are being called (and it seems like mouseDragged is called after mousePressed), so how will i know if the user dragged his mouse or just pressed it?
The correct answer is to use mouseClicked instead of combination of mousePressed+mouseReleased in case you want to distinguish between a click and a drag.
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