I want to use Robot to click mouse button 4, side button.
The InputEvent only have the 3 standard left, middle (scroll) and right buttons.
InputEvent.BUTTON1_DOWN_MASK = 1024
InputEvent.BUTTON2_DOWN_MASK = 2048
InputEvent.BUTTON3_DOWN_MASK = 4096
So I tried to flow the formula and send to the Robot the number 8192
public static void main(String[] args)
{
try
{
Robot mouseHandler = new Robot();
mouseHandler.mousePress(8192);
mouseHandler.mouseRelease(8192);
} catch (AWTException e)
{
e.printStackTrace();
}
}
but it didn't work (as expected) and throws an exception:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid combination of button flags
at java.awt.Robot.checkButtonsArgument(Robot.java:320)
at java.awt.Robot.mousePress(Robot.java:256)
at controller_client.MainClass.main(MainClass.java:30)
Is it possible to create a mouse click with button 4?
Ok so after searching more I found this function that return any mouse button mask from 1 to 20 MouseEvent.getMaskForButton(int button).
After trying it the Robot class did manage to click the button4 and button5, side buttons, like so:
try
{
Robot mouseHandler = new Robot();
int mouseButtonNum = 4; // 1 - 20
// but only buttons from 1 to 5 did work with Robot
mouseHandler.mousePress(MouseEvent.getMaskForButton(mouseButtonNum));
mouseHandler.mouseRelease(MouseEvent.getMaskForButton(mouseButtonNum));
} catch (AWTException e)
{
e.printStackTrace();
}
I used a mouse with 3 buttons and Robot did manage to click 4 and 5 buttons. But it seems like that Robot only can click buttons from 1 to 5, so
probably Hovercraft Full Of Eels's explanation is correct:
I also have to wonder if your issue is not only OS-specific, but also vendor-specific, since I don't know if handling of extra and perhaps unusual mouse buttons has been fully addressed by most common OS's.
If he does right then the OS that I use is Windows 10. If someone have Linux and he knows how to address more mouse buttons to Linux and tried to make Robot click mouse button above 5 so please note me if it works or not.
Related
So, I dont know if I am missing something but any way I work it or look into it I am unable to click on any buttons on the screens.
I made a dialog box with a button essentially as shown in the sample projects but when ever I try to click the button nothing happens at all.
I tried firing the onClick programmatically and it works fine, I also tested that the mouse inputs were being registered okay and they were.
I am at a total loss for any reason why this wouldnt work.
My class code is below:
public class Interactable : UICanvas
{
public void interact()
{
var skin = Skin.createDefaultSkin();
var table = stage.addElement(new Table());
table.center();
table.setFillParent(true);
table.add(talk(this.entity.name, "Stay a while and glisten", "Bye!"));
}
public Dialog talk(string title, string messageText, string closeButtonText)
{
var skin = Skin.createDefaultSkin();
var style = new WindowStyle
{
background = new PrimitiveDrawable(new Color(50, 50, 50)),
//Dims the background
stageBackground = new PrimitiveDrawable(new Color(0, 0, 0, 150))
};
var dialog = new Dialog(title, style);
dialog.getTitleLabel().getStyle().background = new PrimitiveDrawable(new Color(55, 100, 100));
dialog.pad(20, 5, 5, 5);
dialog.addText(messageText);
var exitButton = new TextButton(closeButtonText, skin);
exitButton.onClicked += butt => dialog.hide();
dialog.add(exitButton);
return dialog;
}
}
}
The interact() is called when running up to another entity and pressing "E".
Which causes everything to render properly but I can't click on the button.
Additionally:
When i try to view exitButton co-ordinates theyre always 0 no matter what although the dialog appears in the middle of the window
Monogame version: 3.7
Nez version: 0.9.2
UPDATE:
So it seems like buttons are clickable but their click box is not even nearly aligning with where the buttons are truely rendered.
UPDATE2:
It seems that the issue is that where the button is being rendered and where the actual click box is are not the same.
I have Zoomed in the camera by 2 and the camera also follows my little character around. The dialog will then appear at the X,Y in relation to the current camera view but the actual click box appears at the X,Y in terms of the TiledMap (which is not always on screen).
Not too sure how to work around this.
So! The issue I was having was I was using one renderer for the entire this (The RenderLayerRenderer.) What I have done to fix this is start another renderer (A ScreenSpaceRenderer). This allows me to use it to render UI and its XY variables do not change but are just static to the visual area.
So i ended up with two renders like so:
addRenderer(new RenderLayerRenderer(0,new int[] { (int)RenderLayerIds.First, (int)RenderLayerIds.Second, (int)RenderLayerIds.Third}));
addRenderer(new ScreenSpaceRenderer(1, new int[] { (int)RenderLayerIds.UILayer }));
Using the first for my game rendering and the bottom on just for HUD things!
I want my app to detect mouse clicks anywhere on the screen without having to have the app focused. I want it to detect mouse events universally even if its minimized. So far I've only been able to detect mouse events within a swing gui.
Autohotkey can detect mouse clicks and get the mouse's position at any time, how can I do this with java?
It is possible with a little trick. Should be 100% cross-platform (tested on Linux & Windows). Basically, you create a small JWindow, make it "alwaysOnTop" and move it around with the mouse using a timer.
Then, you can record the click, dismiss the window and forward the click to the actual receiver using the Robot class.
Short left and right clicks work completely fine in my tests.
You could also simulate dragging and click-and-hold, just forwarding that seems harder.
I have code for this, but it is in my Java extension (JavaX). JavaX does translate into Java source code, so you can check out the example here.
The code in JavaX:
static int windowSize = 11; // odd should look nice. Set to 1 for an invisible window
static int clickDelay = 0; // Delay in ms between closing window and forwarding click. 0 seems to work fine.
static int trackingSpeed = 10; // How often to move the window (ms)
p {
final new JWindow window;
window.setSize(windowSize, windowSize);
window.setVisible(true);
window.setAlwaysOnTop(true);
JPanel panel = singleColorPanel(Color.red);
window.setContentPane(panel);
revalidate(window);
final new Robot robot;
panel.addMouseListener(new MouseAdapter {
// public void mousePressed(final MouseEvent e) {}
public void mouseReleased(final MouseEvent e) {
print("release! " + e);
window.setVisible(false);
int b = e.getButton();
final int mod =
b == 1 ? InputEvent.BUTTON1_DOWN_MASK
: b == 2 ? InputEvent.BUTTON2_DOWN_MASK
: InputEvent.BUTTON3_DOWN_MASK;
swingLater(clickDelay, r {
print("clicking " + mod);
robot.mousePress(mod);
robot.mouseRelease(mod);
});
}
});
swingEvery(window, trackingSpeed, r {
Point p = getMouseLocation();
window.setLocation(p.x-windowSize/2, p.y-windowSize/2);
//print("moving");
});
}
I'd like to simulate a Mouse click on a Graphic. I added a Mouselistener, and some action when the mouseclick is done, but I really need to simulate that the user clicked on my Graphic in my programm... How can I say something like "" MouseEvent e is performed!"" ?
Actually I'd like to clean a "Graphics 2D canvas" when you click on a Jbutton called "Clean". But the thing is that the cleaning action would be done only if the user click on my "Graphics 2D canvas". I'd like to make the illusion that the "Graphics 2D canvas" was cleaned by clicking on the JButton..
Thanks.
addMouseListener(this);
addMouseMotionListener(this);
public void mousePressed(MouseEvent e) {
e.consume();
x1=e.getX();
y1=e.getY();
if(figure==1 || figure==3 ) {x2=x1; y2=y1;}
; }
PS : I can't use robot because I have to run my programm on every OS, and someone told me I can't run this on every programm :
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// SET THE MOUSE X Y POSITION
robot.mouseMove(65*Fond_noir.pourcent_largeur, 16*Fond_noir.pourcent_hauteur);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
Well, you're right about Robot. It's platform dependent, and there are no guarantees that it will support all features on all platforms, from the JavaDoc:
Note that some platforms require special privileges or extensions to
access low-level input control. If the current platform configuration
does not allow input control, an AWTException will be thrown when
trying to construct Robot objects. For example, X-Window systems will
throw the exception if the XTEST 2.2 standard extension is not
supported (or not enabled) by the X server.
To simulate the click, you can simply do this:
JButton buttonToSimulateClicking = new JButton(...);
buttonToSimulateClicking.doClick(); // As simple as that !
If you have to simulate the click "the hard way", i.e. to simulate a mouse click, you can always do the following:
MouseEvent clickEvent = new MouseEvent(buttonToSimulateClicking, MouseEvent.MOUSE_CLICKED, ...);
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
eventQueue.dispatchEvent(clickEvent);
I'm making a small game project in Java. In it you have a character that shoots in the direction of the mouse when "pressed" or "dragged" (you know, in Java's terms). The only problem is that if you stop dragging but you still hold the left mouse button down you stop shooting.
Is there a way to detect if the mouse button is down after a drag?
NOTE: the mouse is not sensed as "pressed" after the drag.
You will get the information when a mouse button is pressed and when it is released again. If you want to know the state in between, you need to use a boolean to store that information.
Example:
final boolean[] buttonStates = new boolean[3];
public void mousePressed(MouseEvent e) {
buttonStates[e.getButton()] = true;
}
public void mouseReleased(MouseEvent e) {
buttonStates[e.getButton()] = false;
}
You would do the same for keyboard input by the way.
Is there any way to get mouse coordinates on a click of desktop screen, i dont want to click inside the java frame, want to click the mouse pointer straight away on desktop and have to know the x,y coordinates ? please help me out? (windows)
Rectangle rectScreenSize = new Rectangle(x1,y1,x2,y2);
BufferedImage biScreen = robot.createScreenCapture (rectScreenSize);
finally want to pass the coordinates for rectangle, to determine the screen size for robot class?
You can make a transparent, undecorated JFrame on top of everything, and pass the click on with the Robot class.
By the way, the following does not work outside your own window (I had hoped so):
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
System.out.println("event: " + event);
if (event.toString().contains("MOUSE_EXITED")) {
System.out.println("mouse_exited");
}
}
}, AWTEvent.MOUSE_EVENT_MASK);